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:

Things you have to do to manage transactions are:

  1. Create transaction

  2. Link transaction to a connection

  3. For each command you want to execute:

    1. Create command

    2. Link transaction to command

    3. Execute command

    4. Free command

  4. Commit o rollback transaction

  5. Free transaction

Here you can see an example:

        void process_accounts(GdaConnection *connection)
        {
          GdaTransaction *transaction_one, *transaction_two;
          GdaCommand *command;
        
(1)          transaction_one=gda_transaction_new("accounts1");
(2)          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);
(3)          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);
        
(4)          gda_connection_commit_transaction(connection,transaction_one);
(5)          g_object_unref(transaction_one);
        
          transaction_two=gda_transaction_new("accounts2");
          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);
        
(6)          gda_connection_rollback_transaction(connection,transaction_two);
          g_object_unref(transaction_one);
        
          execute_sql_command(connection,"SELECT * FROM accounts");
        }
        
(1)
Creates first transaction.
(2)
Links it to connection.
(3)
Links command to transaction.
(4)
Makes commit on transaction.
(5)
Frees transaction.
(6)
Makes rollback on second transaction.