Sr. Web Developer
mediabistro.com
US-NY-New York

Justtechjobs.com Post A Job | Post A Resume

Comments for: tim20010117

Message # 1012829:
Date: 08/09/02 22:36
By: Lonny L GRanstrom
Subject: PostgreSQL nested transactions

I have a solution to nesting transactions in PostgreSQL that is working for my application across multiple functions, records and tables. It consists of four functions placed in my PostgreSQL database connection class. This location provides all other functions access to them when working with the class instance.

The $name is usually very descriptive to avoid possible duplications: "revise_customer" or "deactivate_product".

Simply call them after initializing the class and check for return.

$db = new db_pgsql;
$result = $db->start_trans("my_transaction_name");
if ($result === true) {
//continue;
if ($error === true) {
$db->cancel_trans("my_transaction_name");
} else {
$db->end_trans("my_transaction_name");
}
}
//or
if ($result === false) {
//stop function from continuing;
}

The coding is as follows:

class db_pgsql {
var $in_trans = false;
var $trans_name = "";

function check_trans ($name) {
//check for error conditions
//and correct them
//or $name == ""
return 2;

if ($this->trans_name == $name &&
$this->in_trans === true ) {
//in transaction w/proper name sent
return 0;
} else {
//NO transaction in-progress
// &&
//NOT proper transaction name
return 1;
}
} //endfunction(check_trans)

//BEGIN WORK
function start_trans ($name) {
$chk = $this->check_trans($name);
if ($chk != 1) {
return false;
}
$this->trans_name = $name;
$this->in_trans = true;
//send "BEGIN WORK" to pgsql
return true;
} //endfunction(start_trans)

//COMMIT WORK
function end_trans ($name) {
$chk = $this->check_trans($name);
if ($chk != 0) { //Note the difference
return false;
}
$this->trans_name = "";
$this->in_trans = false;
//send "COMMIT WORK" to pgsql
return true;
} //endfunction(end_trans)

//ROLLBACK WORK
function cancel_trans ($name) {
$chk = $this->check_trans($name);
if ($chk != 0) { //same as COMMIT
return false;
}
$this->trans_name = "";
$this->in_trans = false;
//send "ROLLBACK WORK" to pgsql
return true;
} //endfunction(cancel_trans)

//remainder of class code
} //endclass

If the class was registered in the session then possible transactions could be carried across multiple pages also. I have not tried this though so I am not sure if will work.

Previous Message | Next Message


Comments:
full theory about database transactions ??Girish Agarwal01/27/05 00:36
RE: excellent articleLance Edusei07/14/04 08:30
RE: MySQL TransactionsRob Limbrey01/21/03 08:45
MySQL TransactionsImtiaz10/16/02 06:22
MULTIPLE TABLES OR HUGE TABLES-BETTER OPTION?Kapil10/06/02 10:48
please found out my code mistakesMary Pei10/04/02 05:44
project work please help usmike smith09/11/02 06:51
RE: Transaction in MySQLPedro Salgado08/13/02 07:12
PostgreSQL nested transactionsLonny L GRanstrom08/09/02 22:36
RE: How to trigger RollbackVinco07/07/02 01:55
How to trigger RollbackIgor04/25/02 23:46
What about batch updates?pedro03/10/02 20:41
LDAP "Rollback"Fraser02/06/02 20:57
nested transactions - my solutionShivers08/17/01 12:57
PHP, MSSQL7 and TransactionsAndrew Prior04/25/01 05:47
XML and pgSQLmoses03/06/01 14:09
RE: transactions...george02/07/01 06:29
transactions...michael kristopeit02/05/01 11:35
RE: MySQL TransactionsRyan Ayers01/30/01 22:41
Clean solution to nested transactionsJeffrey Greer01/28/01 16:43
RE: sessions and DB transactionsKai Meder01/27/01 18:37
RE: MySQL TransactionsKai Meder01/27/01 18:35
sessions and DB transactionsJohn Harnett01/26/01 23:17
nextval/currval in postgresDon Baccus01/26/01 20:54
Transactions in functions vs. PostgresDon Baccus01/26/01 20:46
Re: I hate duplicate functions.Kirk Parker01/24/01 19:20
RE: nested xactionsPeter Moulding01/24/01 17:24
RE: excellent articlePeter Moulding01/24/01 17:18
RE: Transaction in MySQLMattias01/23/01 05:28
RE: nested xactionsJBW01/22/01 23:37
Transaction in MySQLHabibi01/22/01 18:22
MySQL TransactionsRyan Ayers01/22/01 14:30
excellent articleRyan McGeary01/22/01 09:35
nested xactionsJim G01/22/01 08:48
IdeasAndrew Coldham01/22/01 03:49
 

If you are looking for help, please post on the appropriate forum here. Your questions will be answered much more quickly.

Add A Comment:

Name:

Email:

Subject:

Message:

To reduce spam posts, messages are now manually approved

You are not [logged in]. That means your account will not get credit for this post.