Initiates a transaction and disables autocommit.
Transactions are a means to ensure a database's integrity when one or more queries fail. Queries are usually grouped together in logical blocks and are committed or rolled back when certain conditions are met. In general, any statement that alters a database record such as insert, delete and update is considered a transactional statement. Statements such as create database, create table, drop database, etc. should be executed outside of a transaction block.
See also: Nested Transactions
mixed obj_transaction ( void )
Transaction instance or false on failure.
<?php
try
{
$trans = $dbh->obj_transaction();
$data = array( "color" => "blue",
"type" => "leather",
"price" => 36.95 );
$rs = $dbh->obj_update( "products", $data, "prod_id=21" );
if ( $dbh->obj_error() )
$trans->obj_rollback();
$trans->obj_commit();
}
catch ( Exception $e )
{
//log error and/or redirect user to error page
}
?>
See also: obj_commit, obj_rollback, obj_savepoint
Nested transactions are possible with objSQL with the following restrictions:
<?php
$dbh_1 = new objsql( array("mysql","localhost","root","pass","test_mysql") );
$dbh_2 = new objsql( array("mysql","localhost","root","pass","test_mysql") );
try
{
$trans_1 = $dbh_1->obj_transaction();
$dbh_1->obj_query( "insert into my_table (f_name,l_name) values('Mother','Load')" );
if ( $dbh_1->obj_error() )
throw new Exception( $dbh_1->obj_error_message() );
$trans_2 = $dbh_2->obj_transaction();
$dbh_2->obj_query( "insert into my_table (f_name,l_name) values('Land','Mine')" );
if ( $dbh_2->obj_error() )
throw new Exception( $dbh_2->obj_error_message() );
if ( !$trans_2->obj_commit() )
{
$trans_2->obj_rollback();
throw new Exception( $dbh_2->obj_error_message() );
}
if ( !$trans_1->obj_commit() )
{
$trans_1->obj_rollback();
throw new Exception( $dbh_1->obj_error_message() );
}
}
catch ( Exception $e )
{
echo 'Error: ' . $e->getMessage();
}
?>