Database transactions are used to control data commitment to databases. For example, in standard account procedures, it is necessary to debit one account and credit another at the same time. Since computers fault on occasion (power outages, network outages, etc.) there would be a potential for one record to be updated/added, but not the other.
To avoid these situations we use transactions. Transactions in ADO+ are, just as in ADO, handled at the database level: your database must support transactions.
<p>
There are three basic commands for transactions: begin, commit and rollback. Begin marks the beginning of a transaction. Anything done between the begin and the next command (either rollback or commit) is considered part of the transaction. Consider the following pseudo T-SQL command:
<P>
<div class="code">
<BR> begin transaction
<BR>
<BR> insert into account (account,amount,debitcredit) values (100,100,'d')
<BR> insert into account (account,amount,debitcredit) values (300,100,'c')
<BR>
<BR> if (@@ERROR > 0)
<BlockQuote> rollback</BlockQuote>
<BR> else
<BlockQuote> commit</BlockQuote>
<P>
</div>
The example shows that if either of the inserts fail, both are rolled back to their original states. If each succeeds, then the transaction is committed.
<P>
Just as in classic ADO, you can control transactions through the connection object. In fact, when you are using the ADOConnection it is using the same underlying OLEDB transaction model. Thus, if you were able to do transactions against your database with ADO classic, you can still do them with ADO+. Below is sample code using the SQLConnection and SQLCommand to update a Customer record, and add an order. If either of them fail, then the records are rolled back.
<BR>The DataSet object also has a commit model (AcceptChanges,RejectChanges) but it doesn not affect the database. This commit model is for the cache of data in the DataSet alone.