kairog
05-23-2011, 10:31 AM
Hello PHP friends,
I am very new to OOP and transactional database and I'm just starting to apply them to some of my coding.
I appreciate if you could comment or tell me how I can improve my code below.
Here's my code....
class myTransaction {
var $connection;
function __construct($dbconnection) {
$this->connection = $dbconnection;
@mysql_query("SET AUTOCOMMIT=0",$this->connection);
}
function begin(){
@mysql_query("BEGIN", $this->connection);
}
function commit() {
@mysql_query("COMMIT", $this->connection);
@mysql_query("SET AUTOCOMMIT=1",$this->connection);
}
function rollback() {
@mysql_query("ROLLBACK", $this->connection);
@mysql_query("SET AUTOCOMMIT=1",$this->connection);
}
/*
* use FOR UPDATE at the end of SELECT statement
* or use LOCK IN SHARE MODE at the end of SELECT statement
*/
function query($query, $lock_mode=''){
print $query;
if ($result = @mysql_query($query . $lock_mode )){
return $result;
} else {
return false;
}
}
}
and here's how I apply it...
$connection = mysql_pconnect($dhost,$duser,$dpass) or die ('unable to connect');
mysql_select_db($dname) or die ('unable to select database');
$new_entry = new myTransaction($connection);
$error = 0;
$new_entry->begin();
// query #1
if ($result = @mysql_query('insert....')){
} else {
$error++;
}
// query #2
if ($result = @mysql_query('insert....')){
} else {
$error++;
}
// query #3
if ($result = @mysql_query('insert....')){
} else {
$error++;
}
if ($error){
$new_entry->rollback();
} else {
$new_entry->commit();
}
Any comment will be highly appreciated.
Cheers,
I am very new to OOP and transactional database and I'm just starting to apply them to some of my coding.
I appreciate if you could comment or tell me how I can improve my code below.
Here's my code....
class myTransaction {
var $connection;
function __construct($dbconnection) {
$this->connection = $dbconnection;
@mysql_query("SET AUTOCOMMIT=0",$this->connection);
}
function begin(){
@mysql_query("BEGIN", $this->connection);
}
function commit() {
@mysql_query("COMMIT", $this->connection);
@mysql_query("SET AUTOCOMMIT=1",$this->connection);
}
function rollback() {
@mysql_query("ROLLBACK", $this->connection);
@mysql_query("SET AUTOCOMMIT=1",$this->connection);
}
/*
* use FOR UPDATE at the end of SELECT statement
* or use LOCK IN SHARE MODE at the end of SELECT statement
*/
function query($query, $lock_mode=''){
print $query;
if ($result = @mysql_query($query . $lock_mode )){
return $result;
} else {
return false;
}
}
}
and here's how I apply it...
$connection = mysql_pconnect($dhost,$duser,$dpass) or die ('unable to connect');
mysql_select_db($dname) or die ('unable to select database');
$new_entry = new myTransaction($connection);
$error = 0;
$new_entry->begin();
// query #1
if ($result = @mysql_query('insert....')){
} else {
$error++;
}
// query #2
if ($result = @mysql_query('insert....')){
} else {
$error++;
}
// query #3
if ($result = @mysql_query('insert....')){
} else {
$error++;
}
if ($error){
$new_entry->rollback();
} else {
$new_entry->commit();
}
Any comment will be highly appreciated.
Cheers,