Re: Nested transaction - I am a bank ?? - Mailing list pgsql-general

From Nigel J. Andrews
Subject Re: Nested transaction - I am a bank ??
Date
Msg-id Pine.LNX.4.21.0401132317540.7764-100000@ponder.fairway2k.co.uk
Whole thread Raw
In response to Nested transaction - I am a bank ??  ("Thapliyal, Deepak" <dthapliyal@soe.sony.com>)
List pgsql-general
On Tue, 13 Jan 2004, Thapliyal, Deepak wrote:

> Hi,
>
> Assume I have a bank app.. When customer withdraws $10 from his accouint I
> have to do following
>     --> update account_summary table [subtract $10 from his account]
>     --> update account detail_table [with other transaction details]
>
> Requirement:
>     either both transactions should succeed or both transactions should
> be rolled back in case of failure.
>
> Question:
>     if my first update succeeds and second fails (say due to space
> errors .. I have inconsistancy ..
>
> Per the thread below stored procedures/functions cannot have commits. I
> assume that means that they will be implicitly commited ??
>
> How do I approach this simple requirment using psql ?

I know others have answered this but I don't recall a specific answer to the
psql question. So...simple:

mydb=> begin;
mydb=> update [whatever];
mydb=> update [whatever];
mydb=> [whatever]
mydb=> [...etc.]
mydb=> commit;


As has been mentioned, put all the operations in a function you could replace
all those lines with one:

  select myfunc();

which will either complete and commit (if autocommit is on) or not commit. If
autocommit is off then it will still need the commit statement in order to
commit and also a rollback (or may be a commit works as well I can't
remember) in order to clear the aborted transaction in case of error (so that
more statements can be issued).


--
Nigel Andrews


pgsql-general by date:

Previous
From: "Nigel J. Andrews"
Date:
Subject: Re: Drawbacks of using BYTEA for PK?
Next
From: "Chris Ochs"
Date:
Subject: casting parameters to a function