## Transactions and batch processes

## Managing transactions

The special functions we need to do this are defined in the GdaTransaction, GdaConnection and GdaCommand classes, and they are:

- gda_transaction_new ()

- gda_connection_begin_transaction ()

- gda_connection_commit_transaction ()

- gda_connection_rollback_transaction ()

- gda_command_set_transaction ()

Things you have to do to manage transactions are:

1. Create transaction

2. Change, if needed, the isolation level

3. Link transaction to a connection

4. For each command you want to execute:

   1. Create command

   2. Link transaction to command

   3. Execute command

   4. Free command

5. Commit or rollback transaction

6. Free transaction

Here you can see an example:

            void process_accounts(GdaConnection *connection)
            {
              GdaTransaction *transaction_one, *transaction_two;
              GdaCommand *command;

              transaction_one=gda_transaction_new("accounts1");
              gda_transaction_set_isolation_level(transaction_one,
                       GDA_TRANSACTION_ISOLATION_SERIALIZABLE);
              gda_connection_begin_transaction(connection,transaction_one);

              command=gda_command_new (
                                       "UPDATE accounts SET balance=balance+50"
                                       "WHERE account_code=456",
                                       GDA_COMMAND_TYPE_SQL,
                                       GDA_COMMAND_OPTION_STOP_ON_ERRORS);
              gda_command_set_transaction(command,transaction_one);
              gda_connection_execute_non_query(connection,command,NULL);
              gda_command_free(command);

              command=gda_command_new (
                                       "UPDATE accounts SET balance=balance-50"
                                       "WHERE account_code=12",
                                       GDA_COMMAND_TYPE_SQL,
                                       GDA_COMMAND_OPTION_STOP_ON_ERRORS);
              gda_command_set_transaction(command,transaction_one);
              gda_connection_execute_non_query(connection,command,NULL);
              gda_command_free(command);

              gda_connection_commit_transaction(connection,transaction_one);
              g_object_unref(transaction_one);

              transaction_two=gda_transaction_new("accounts2");
              gda_transaction_set_isolation_level(transaction_two,
                       GDA_TRANSACTION_ISOLATION_SERIALIZABLE);
              gda_connection_begin_transaction(connection,transaction_two);

              command=gda_command_new (
                                       "UPDATE accounts SET balance=balance+400"
                                       "WHERE account_code=456",
                                       GDA_COMMAND_TYPE_SQL,
                                       GDA_COMMAND_OPTION_STOP_ON_ERRORS);
              gda_command_set_transaction(command,transaction_two);
              gda_connection_execute_non_query(connection,command,NULL);
              gda_command_free(command);

              command=gda_command_new (
                                       "UPDATE accounts SET balance=balance-400"
                                       "WHERE account_code=12",
                                       GDA_COMMAND_TYPE_SQL,
                                       GDA_COMMAND_OPTION_STOP_ON_ERRORS);
              gda_command_set_transaction(command,transaction_two);
              gda_connection_execute_non_query(connection,command,NULL);
              gda_command_free(command);

              gda_connection_rollback_transaction(connection,transaction_two);
              g_object_unref(transaction_one);

              execute_sql_command(connection,"SELECT * FROM accounts");
            }

| Creates first transaction.
| Changes the isolation level.
| Links it to connection.
| Links command to transaction.
| Makes commit on transaction.
| Frees transaction.
| Makes rollback on second transaction.