1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Transactions and batch processes</title>
<meta name="generator" content="DocBook XSL Stylesheets V1.73.2">
<link rel="start" href="index.html" title="GNOME Data Access manual">
<link rel="up" href="index.html" title="GNOME Data Access manual">
<link rel="prev" href="manage-gdavalue.html" title="Managing values">
<link rel="next" href="managing-errors.html" title="Managing errors">
<meta name="generator" content="GTK-Doc V1.10 (XML mode)">
<link rel="stylesheet" href="style.css" type="text/css">
<link rel="chapter" href="introduction.html" title="Introduction">
<link rel="chapter" href="architecture.html" title="libgda architecture">
<link rel="chapter" href="installation.html" title="Installation">
<link rel="chapter" href="connecting.html" title="Beginning">
<link rel="chapter" href="processing-queries.html" title="Processing queries">
<link rel="chapter" href="ch06.html" title="Transactions and batch processes">
<link rel="chapter" href="managing-errors.html" title="Managing errors">
<link rel="chapter" href="main_example.html" title="Full example">
<link rel="chapter" href="migration.html" title="Some formulae for migration from old version">
<link rel="chapter" href="libgda-api.html" title="Client API Reference">
<link rel="chapter" href="libgda-providers.html" title="GDA Providers">
<link rel="chapter" href="libgda-xql.html" title="XML Queries">
<link rel="chapter" href="libgda-reports.html" title="GDA Report Engine">
<link rel="appendix" href="fdl.html" title="Appendix A. GNU Free Documentation License">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table class="navigation" id="top" width="100%" summary="Navigation header" cellpadding="2" cellspacing="2"><tr valign="middle">
<td><a accesskey="p" href="manage-gdavalue.html"><img src="left.png" width="24" height="24" border="0" alt="Prev"></a></td>
<td> </td>
<td><a accesskey="h" href="index.html"><img src="home.png" width="24" height="24" border="0" alt="Home"></a></td>
<th width="100%" align="center">GNOME Data Access manual</th>
<td><a accesskey="n" href="managing-errors.html"><img src="right.png" width="24" height="24" border="0" alt="Next"></a></td>
</tr></table>
<div class="chapter" lang="en">
<div class="titlepage"><div><div><h2 class="title">
<a name="id447560"></a>Transactions and batch processes</h2></div></div></div>
<div class="toc"><dl><dt><span class="sect1"><a href="ch06.html#transactions">Managing transactions</a></span></dt></dl></div>
<div class="sect1" lang="en">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="transactions"></a>Managing transactions</h2></div></div></div>
<p>
The special functions we need to do this are defined in the
<a class="link" href="libgda-GdaTransaction.html" title="GdaTransaction">GdaTransaction</a>,
<a class="link" href="libgda-GdaConnection.html" title="GdaConnection">GdaConnection</a> and
<a class="link" href="libgda-gda-command.html" title="gda-command">GdaCommand</a>
classes, and they are:
</p>
<div class="itemizedlist"><ul type="disc">
<li><p>
<a class="link" href="libgda-GdaTransaction.html#gda-transaction-new" title="gda_transaction_new ()">gda_transaction_new ()</a></p></li>
<li><p>
<a class="link" href="libgda-GdaConnection.html#gda-connection-begin-transaction" title="gda_connection_begin_transaction ()">gda_connection_begin_transaction ()</a></p></li>
<li><p>
<a class="link" href="libgda-GdaConnection.html#gda-connection-commit-transaction" title="gda_connection_commit_transaction ()">gda_connection_commit_transaction ()</a></p></li>
<li><p>
<a class="link" href="libgda-GdaConnection.html#gda-connection-rollback-transaction" title="gda_connection_rollback_transaction ()">gda_connection_rollback_transaction ()</a></p></li>
<li><p>
<a class="link" href="libgda-gda-command.html#gda-command-set-transaction" title="gda_command_set_transaction ()">gda_command_set_transaction ()</a></p></li>
</ul></div>
<p>
Things you have to do to manage transactions are:
</p>
<div class="orderedlist"><ol type="1">
<li><p>Create transaction</p></li>
<li><p>Change, if needed, the isolation level</p></li>
<li><p>Link transaction to a connection</p></li>
<li>
<p>For each command you want to execute:</p>
<div class="orderedlist"><ol type="a">
<li><p>Create command</p></li>
<li><p>Link transaction to command</p></li>
<li><p>Execute command</p></li>
<li><p>Free command</p></li>
</ol></div>
</li>
<li><p>Commit or rollback transaction</p></li>
<li><p>Free transaction</p></li>
</ol></div>
<p>
Here you can see an example:
</p>
<div class="programlistingco">
<pre class="programlisting">
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");
}
</pre>
<div class="calloutlist"><table border="0" summary="Callout list">
<tr>
<td width="5%" valign="top" align="left"><p><img src="images/callouts/1.png" alt="1" border="0"></p></td>
<td valign="top" align="left"><p>
Creates first transaction.
</p></td>
</tr>
<tr>
<td width="5%" valign="top" align="left"><p><img src="images/callouts/2.png" alt="2" border="0"></p></td>
<td valign="top" align="left"><p>
Changes the isolation level.
</p></td>
</tr>
<tr>
<td width="5%" valign="top" align="left"><p><img src="images/callouts/3.png" alt="3" border="0"></p></td>
<td valign="top" align="left"><p>
Links it to connection.
</p></td>
</tr>
<tr>
<td width="5%" valign="top" align="left"><p><img src="images/callouts/4.png" alt="4" border="0"></p></td>
<td valign="top" align="left"><p>
Links command to transaction.
</p></td>
</tr>
<tr>
<td width="5%" valign="top" align="left"><p><img src="images/callouts/5.png" alt="5" border="0"></p></td>
<td valign="top" align="left"><p>
Makes commit on transaction.
</p></td>
</tr>
<tr>
<td width="5%" valign="top" align="left"><p><img src="images/callouts/6.png" alt="6" border="0"></p></td>
<td valign="top" align="left"><p>
Frees transaction.
</p></td>
</tr>
<tr>
<td width="5%" valign="top" align="left"><p><img src="images/callouts/7.png" alt="7" border="0"></p></td>
<td valign="top" align="left"><p>
Makes rollback on second transaction.
</p></td>
</tr>
</table></div>
</div>
</div>
</div>
<div class="footer">
<hr>
Generated by GTK-Doc V1.10</div>
</body>
</html>
|