How to call Transaction in MDB2 php abstraction package
Method 1
If your using MDB2 database package and you want to run transaction in your page here is solution
You may be facing difficult in calling Transaction in your MDB2 database package
Here is a solution to over come the difficulty
To call transaction in your php file first you should create db connection object as you do in like in your MDB2 connection
The following code shows how to connect your db
require_once 'MDB2.php';
$con = MDB2::connect('pgsql://usr:pw@localhost/dbnam');
if (PEAR::isError($mdb2)) {
die($mdb2->getMessage());
}
Assuming you have your db connected object in variable $con
The following procedure will help you in calling transaction
Before start weather its support are not
// check if transaction are supported by this driver
if (!$con->supports('transactions')) {
exit();
}
//to start the transaction in your page use following code
$con->query("BEGIN");//to d=start the transaction
//execute your queries here
If(check query condition)//if its works fine
{
$con->query("COMMIT");");//Complete transaction
}
Else
{
$con->query("ROLLBACK");");//Transaction is not complete rollback your query
}
If you fina any difficulties in execution here is one more method which you can use
Method 2
// Open a transaction
$res = $mdb2->beginTransaction();
..
// check if we are inside a transaction and if savepoints are supported
if ($mdb2->inTransaction() && $mdb2->supports('savepoints')) {
// Set a savepoint
$savepoint = 'MYSAVEPOINT';
$res = $mdb2->beginTransaction($savepoint);
..
// determine if the savepoint should be released or to rollback to the savepoint
if ($error_condition) {
$res = $mdb2->rollback($savepoint);
} else {
$res = $mdb2->commit($savepoint);
}
No comments:
Post a Comment