DB Access Module for MySQL User’s Guide : Chapter 2 Technical Information : Transaction Processing
Transaction Processing
Transactions can be controlled by setting transaction isolation levels and by using transaction control methods. These methods are described in the following two sections.
Setting Isolation Levels
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.
Table 5 – Setting the isolation level
RWDBConnection:: IsolationType
MySQL Isolation Level
Unknown
READ_UNCOMMITTED
ANSILevel1
READ_COMMITTED
ANSILevel2
REPEATABLE_READ
ANSILevel3
SERIALIZABLE
To determine the current isolation level, call RWDBConnection::isolation() without an argument.
Using Transaction Control Methods
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
NOTE >> These transaction calls 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.
Example 3 – Using Transaction Control Methods
// 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.
 
NOTE >> DB Access Module for MySQL does not support nested transactions.