Re: Cleaning up aborted transactions

From: "Adrian Tineo" <adriantineo(at)softhome(dot)net>
To: "Michael Glaesemann" <grzm(at)myrealbox(dot)com>, pgsql-php(at)postgresql(dot)org
Subject: Re: Cleaning up aborted transactions
Date: 2003-06-10 08:05:42
Message-ID: 000801c32f27$553013b0$926bd9d9@clairvo
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-php

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

In response to

Responses

Browse pgsql-php by date

  From Date Subject
Next Message Michael Glaesemann 2003-06-10 09:21:03 Re: Cleaning up aborted transactions
Previous Message Michael Glaesemann 2003-06-09 08:09:21 Cleaning up aborted transactions