A transaction ends either successfully through an explicit commit command, or unsuccessfully in any of a number of ways. The following are the ways to end a transaction:
You can commit the transaction by calling its
commit
member function:
Xaction.commit();
This sends the commit operation to the backend. If the transaction
throws an error, this is the last point where that can happen. The
only exceptions that may be generated by Xaction
beyond this point are related to incorrect handling of the
transaction object, eg. if you first commit
Xaction
and then abort it; or runtime errors
such as memory running out.
Any streams or cursors nested within the transaction must have been
closed before the commit()
. To do otherwise
could possibly allow a transaction to be committed before all
related actions had completed.
A transaction is aborted if it gets destroyed without having been explicitly committed:
{ work Xaction(Conn, "DemoTransaction"); // (Queries) } // Xaction destroyed here
work *XactionP = new work(Conn, "DemoTransaction"); // (Queries) delete XactionP; // Xaction destroyed here
try { work Xaction(Conn, "DemoTransaction"); // (Queries) Xaction.commit(); // If we get here, Xaction is committed } catch (...) { // If we get here, Xaction has been rolled back }
No matter where exactly the decision to abort is made, the actual abort operation is sent to the backend when the transaction's destructor is called. If the abort fails, eg. because the network connection has been lost, no error is reported and the transaction will die of natural causes.
If a database error occurs during the transaction, such as an SQL syntax error or lost connection to the backend, the transaction is aborted.
work Xaction(Conn, "DemoTransaction"); try { // (Queries) Xaction.exec("SELECT !?^H^H^H^H"); // Fails: SQL syntax error } catch (...) { } Xaction.commit(); // ERROR: Xaction has already aborted!
After a commit or after an abort, the transaction will no longer accept any queries to execute. Ending a transaction more than once is also an error, except that aborting it multiple times is tolerated to facilitate error handling.