Commit modes in PHP applications (ibm_db2)
You can control how groups of SQL statements are committed by specifying a commit mode for a connection resource. The ibm_db2 extension supports two commit modes: autocommit and manual commit.
You must use a regular connection resource returned by the db2_connect function to control database transactions in PHP. Persistent connections always use autocommit mode.
- autocommit mode
- In autocommit mode, each SQL statement is a complete transaction,
which is automatically committed. Autocommit mode helps prevent locking
escalation issues that can impede the performance of highly scalable
Web applications. By default, the ibm_db2 extension opens every connection
in autocommit mode.
You can turn on autocommit mode after disabling it by calling
db2_autocommit($conn, DB2_AUTOCOMMIT_ON), where conn is a valid connection resource.Calling the db2_autocommit function might affect the performance of your PHP scripts because it requires additional communication between PHP and the database management system.
- manual commit mode
- In manual commit mode, the transaction ends when you call the db2_commit or db2_rollback function.
This means that all statements executed on the same connection between
the start of a transaction and the call to the commit or rollback
function are treated as a single transaction.
Manual commit mode is useful if you might have to roll back a transaction that contains one or more SQL statements. If you issue SQL statements in a transaction, and the script ends without explicitly committing or rolling back the transaction, the ibm_db2 extension automatically rolls back any work performed in the transaction.
You can turn off autocommit mode when you create a database connection by using the "AUTOCOMMIT" => DB2_AUTOCOMMIT_OFF setting in the db2_connect options array. You can also turn off autocommit mode for an existing connection resource by calling
db2_autocommit($conn, DB2_AUTOCOMMIT_OFF), where conn is a valid connection resource.
For more information about the ibm_db2 API, see http://www.php.net/docs.php.
Examples
End the transaction when db2_commit or db2_rollback is called.
$conn = db2_connect('SAMPLE', 'db2inst1', 'ibmdb2', array(
'AUTOCOMMIT' => DB2_AUTOCOMMIT_ON));
// Issue one or more SQL statements within the transaction
$result = db2_exec($conn, 'DELETE FROM TABLE employee');
if ($result === FALSE) {
print '<p>Unable to complete transaction!</p>';
db2_rollback($conn);
}
else {
print '<p>Successfully completed transaction!</p>';
db2_commit($conn);
}