| GNOME Data Access manual |
|---|
Table of Contents
The special functions we need to do this are defined in the <LINK>GdaTransaction</LINK>, <LINK>GdaConnection</LINK> and <LINK>GdaCommand</LINK> classes, and they are:
<ITEMIZEDLIST> <LISTITEM><LINK>gda_transaction_new ()</LINK>
</LISTITEM> <LISTITEM><LINK>gda_connection_begin_transaction ()</LINK>
</LISTITEM> <LISTITEM><LINK>gda_connection_commit_transaction ()</LINK>
</LISTITEM> <LISTITEM><LINK>gda_connection_rollback_transaction ()</LINK>
</LISTITEM> <LISTITEM><LINK>gda_command_set_transaction ()</LINK>
</LISTITEM> </ITEMIZEDLIST>Things you have to do to manage transactions are:
<ORDEREDLIST> <LISTITEM>Create transaction
</LISTITEM> <LISTITEM>Change, if needed, the isolation level
</LISTITEM> <LISTITEM>Link transaction to a connection
</LISTITEM> <LISTITEM>For each command you want to execute:
<ORDEREDLIST> <LISTITEM>Create command
</LISTITEM> <LISTITEM>Link transaction to command
</LISTITEM> <LISTITEM>Execute command
</LISTITEM> <LISTITEM>Free command
</LISTITEM> </ORDEREDLIST> </LISTITEM> <LISTITEM>Commit or rollback transaction
</LISTITEM> <LISTITEM>Free transaction
</LISTITEM> </ORDEREDLIST>Here you can see an example:
<PROGRAMLISTINGCO> <AREASPEC> <AREA></AREA> <AREA></AREA> <AREA></AREA> <AREA></AREA> <AREA></AREA> <AREA></AREA> <AREA></AREA> </AREASPEC>
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");
}
<CALLOUTLIST>
<CALLOUT>
<PARA>
Creates first transaction.
</PARA>
</CALLOUT>
<CALLOUT>
<PARA>
Changes the isolation level.
</PARA>
</CALLOUT>
<CALLOUT>
<PARA>
Links it to connection.
</PARA>
</CALLOUT>
<CALLOUT>
<PARA>
Links command to transaction.
</PARA>
</CALLOUT>
<CALLOUT>
<PARA>
Makes commit on transaction.
</PARA>
</CALLOUT>
<CALLOUT>
<PARA>
Frees transaction.
</PARA>
</CALLOUT>
<CALLOUT>
<PARA>
Makes rollback on second transaction.
</PARA>
</CALLOUT>
</CALLOUTLIST>
</PROGRAMLISTINGCO>| << Managing values | Managing errors >> |