From: Adrian Tineo <adriantineo@softhome.net>
To: Michael Glaesemann <grzm@myrealbox.com>
To: pgsql-php@postgresql.org
Subject: Re: Cleaning up aborted transactions
Date: Tue, 10 Jun 2003 10:05:42 +0200
Message-ID: <000801c32f27$553013b0$926bd9d9@clairvo> (raw)
References: <ACBB242E-9A51-11D7-9E7B-0005029FC1A7@myrealbox.com>
The correct way of working with transactions is to use:
BEGIN
...sql commands
COMMIT or ROLLBACK
So I would use this for example:
pg_connect($connection);
pg_query($connection,"BEGIN;");
$insert="INSERT INTO table VALUES (2,7,5);
$result=pg_query($connection,$insert);
if(!$result){
pg_query($connection,"ROLLBACK");
//Something went wrong with the insert so we rollback and nothing
changes in the db
}else{
pg_query($connection,"COMMIT");
// If everything went all right, then we commit the changes
}
pg_close($connection);
Of course, the interesting thing comes when you have several operations
(inserts, deletes or updates) between begin and commit, this way either you
make all the changes or make none, that's the cool thing about transactions.
In each operation just check whether the result was valid or not. If ANY of
them was invalid, rollback and none of the changes will take effect.
I don't know if this answer your question...
Adrian Tineo
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-php@postgresql.org
Cc: adriantineo@softhome.net, grzm@myrealbox.com
Subject: Re: Cleaning up aborted transactions
In-Reply-To: <000801c32f27$553013b0$926bd9d9@clairvo>
References: <ACBB242E-9A51-11D7-9E7B-0005029FC1A7@myrealbox.com> <000801c32f27$553013b0$926bd9d9@clairvo>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox