Performing Batch Updates

The GFXDCommand object provides AddBatch(), ExecuteBatch(), and ClearBatch() methods to explicitly perform batch updates. The result of ExecuteBatch() is the number of rows changed by each command in a batch.

This example uses GemFire XD batch operations to insert new table rows.
int numRecords = 100;   
int batchSize = 10;     // Limit number of statements per batch execution
string gfxdHost = "localhost";
int gfxdPort = 1527;
string connectionStr = string.Format(@"server={0}:{1}", gfxdHost, gfxdPort);

GFXDClientConnection connection = null;

try
{
    connection = new GFXDClientConnection(connectionStr);
    connection.Open();

    GFXDCommand command = connection.CreateCommand();
    command.CommandType = CommandType.Text;
    command.CommandText = @"INSERT INTO orders(order_id, order_date, ship_date, 
                          customer_id, subtotal) VALUES(?, ?, ?, ?, ?)";

    // Prepare batch statement
    command.Prepare();

    int stCount = 0; // batch statements count

    for (int i = 0; i < numRecords; i++)
    {
        command.Parameters[0] = <order_id>;
        command.Parameters[1] = <order_date>;
        command.Parameters[2] = <ship_date>;
        command.Parameters[3] = <customer_id>;
        command.Parameters[4] = <subtotal>;

        // Add statement to command's batch
        command.AddBatch();

        // Execute Batch statements when batch size is reached and reset
        if ((++stCount) == batchSize)
        {
            command.ExecuteBatch();
            stCount = 0; 
        }
    }

    // Execute the remaining statements in the batch
    command.ExecuteBatch();
}
catch (Exception e)
{
///
/// Log or re-throw exception
/// 
}
finally
{
    connection.Close();
}