![]() | ![]() | GNOME Data Access manual | ![]() |
---|
Table of Contents
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:
Create transaction
Change, if needed, the isolation level
Link transaction to a connection
For each command you want to execute:
Create command
Link transaction to command
Execute command
Free command
Commit or rollback transaction
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. |
<< Managing values | Managing errors >> |