Transactions can be controlled by setting transaction isolation levels and by using transaction control methods. These methods are described in the following two sections.
You can use the isolation() method of RWDBConnection to set the isolation level of the connection. Table 5 shows the mapping between the argument you pass for IsolationType, and the isolation level set by the MySQL statement SET SESSION TRANSACTION ISOLATION LEVEL.
RWDBConnection:: IsolationType | MySQLIsolation Level |
Unknown | READ_UNCOMMITTED |
ANSILevel1 | READ_COMMITTED |
ANSILevel2 | REPEATABLE_READ |
ANSILevel3 | SERIALIZABLE |
To determine the current isolation level, call RWDBConnection::isolation() without an argument.
You can explicitly control transactions through the following methods:
RWDBConnection::beginTransaction() RWDBConnection::rollbackTransaction(const RWCString& name = RWCString()) RWDBConnection::commitTransaction() RWDBConnection::setSavepoint(const RWCString& name) |
These methods have straightforward implementations that correspond to the following MySQL calls:
START TRANSACTION ROLLBACK ROLLBACK TO SAVEPOINT name COMMIT SAVEPOINT name |
These transaction calls will have the desired affect only when used with transaction-safe tables such as those created with the InnoDB storage engine.
An application can add the DB Interface Module transaction methods to its code to take explicit control of its transaction blocks. The following code demonstrates how these methods can be used to commit or to rollback transactions.
// Assume we have a table myTable(c int) with no rows in it. RWDBInserter ins = myTable.inserter(); cn.beginTransaction(); // Begin transaction.. (ins << 1).execute(cn); // First insertion cn.setSavepoint("svpt"); // Save first insertion (ins << 2).execute(cn); // Second insertion (ins << 3).execute(cn); // Third insertion cn.rollbackTransaction("svpt"); // Rollback transaction erasing // second and third insertions. cn.commitTransaction(); // commit transaction on part // that is not rolled back // The above program results in myTable holding one row of data. // Its value is 1. |
DB Access Module for MySQL does not support nested transactions.
Copyright © Rogue Wave Software, Inc. All Rights Reserved.
The Rogue Wave name and logo, and SourcePro, are registered trademarks of Rogue Wave Software. All other trademarks are the property of their respective owners.
Provide feedback to Rogue Wave about its documentation.