Re: Cleaning up aborted transactions - Mailing list pgsql-php

From Adrian Tineo
Subject Re: Cleaning up aborted transactions
Date
Msg-id 000801c32f27$553013b0$926bd9d9@clairvo
Whole thread Raw
In response to Cleaning up aborted transactions  (Michael Glaesemann <grzm@myrealbox.com>)
Responses Re: Cleaning up aborted transactions  (Michael Glaesemann <grzm@myrealbox.com>)
List 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



pgsql-php by date:

Previous
From: Michael Glaesemann
Date:
Subject: Cleaning up aborted transactions
Next
From: Michael Glaesemann
Date:
Subject: Re: Cleaning up aborted transactions