Re: Nested transaction - I am a bank ??

From: Richard Huxton <dev(at)archonet(dot)com>
To: "Thapliyal, Deepak" <dthapliyal(at)soe(dot)sony(dot)com>, Anton(dot)Nikiforov(at)loteco(dot)ru
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Nested transaction - I am a bank ??
Date: 2004-01-13 18:48:23
Message-ID: 200401131848.23299.dev@archonet.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Tuesday 13 January 2004 17:47, 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.

In database terms, the two operations together are one transaction. You do
something like:

BEGIN;
INSERT INTO detail (acct_num,trans_type,trans_time,notes) VALUES
(1,'CASHOUT',now(),'blah');
UPDATE account_summary SET amount=amount-10 WHERE acct_num = 1;
COMMIT;

Now, if one (or both) of those were written as a function, that function's
effects would still be bound by the transaction. All operations(*) take place
within a transaction in PG, either explicitly as above or implicitly with one
per statement.

What you can't do is have a function that does something like:

LOOP 1..10
BEGIN;
-- do something ten times, each time in its own transaction
COMMIT;
END LOOP

(*) except for a couple of bits like vacuum, truncate(?) and similar.
--
Richard Huxton
Archonet Ltd

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Richard Huxton 2004-01-13 18:53:31 Re: sql insert function
Previous Message Keith G. Murphy 2004-01-13 18:47:56 Re: Best practice? Web application: single PostgreSQL