PEAR MDB2 default to auto commiting all queries. However using the beginTransaction() one can open a new transaction and with the commit() and rollback() a transaction is finished. All of these three methods optionally accept a string name of a savepoint to set, release or rollback to respectively. The method inTransaction() may be used to check if a transaction is currently open.
Example 33-1. Doing a transaction
|
Warning |
PEAR MDB2 does not emulate transactions or savepoints. This means that it depends on the underlying RDBMS (and in the case of MySQL the storage engines used) if transactions will be available in MDB2. Also note that some RDBMS implicitly commit transactions when executing DDL statements (notably exceptions are Oracle and PostgreSQL). |
MDB2 also supports "nested" transactions using the beginNestedTransaction() method. Actually these are not true nested transactions as they are natively supported in Interbase for example. MDB2 maintains a counter of opened nested transactions. The transaction is finished once that counter is decremented back to 1 with completeNestedTransaction() calls. If the RDBMS supports savepoints then MDB2 will automatically set a savepoint on every call of the beginNestedTransaction() method after the initial call and will return the name. These savepoints are automatically released by the completeNestedTransaction() method. The name of these automatic savepoints are determined by the "savepoint_format" (default: 'MDB2_SAVEPOINT_%s') option and the nested transaction counter.
If after initial opening of the nested transaction an unexpected PEAR error is raised on the MDB2 instance the transaction is rolled back, otherwise it is commited at this point. Using the getNestedTransactionError() method it is possible to check if there has been an error inside the transaction. Alternatively an rollback can be forced using the the failNestedTransaction(). This method optionally accepts a mixed parameter which will set the error to return if the getNestedTransactionError() method is called as well as a second boolean parameter that optionally forces an immidiate rollback.
Example 33-2. Using emulated nested transactions
|
Finally MDB2 supports setting of the transaction isolation level as per the SQL 92 standard using the setTransactionIsolation() method. If a given RDBMS does not support a given isolation level but supports a higher more strict isolation level, then MDB2 silently uses that higher transaction level. Some RDBMS support additional options which are silently ignored if they are not supported.
Example 33-3. Setting the transaction isolation level
|