473,326 Members | 2,108 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Nested transaction - I am a bank ??

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 ?

Thx
Deep

-----Original Message-----
From: pg*****************@postgresql.org
[mailto:pg*****************@postgresql.org] On Behalf Of Richard Huxton
Sent: Tuesday, January 13, 2004 4:32 AM
To: An*************@loteco.ru
Cc: pg***********@postgresql.org
Subject: Re: [GENERAL] Parse error help needed...
On Tuesday 13 January 2004 12:01, An*************@loteco.ru wrote:
RH> Remove the "commit" line - functions cannot define their own
transactions RH> anyway.
Do you know if it will be solved sometime? Or this is architecture
dependend problem? I mean that transactions are rulez and very helpful
rulez when working with large databases.


Nested transactions are on the todo list, but I don't know when they will
appear.

--
Richard Huxton
Archonet Ltd

---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings

---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddressHere" to ma*******@postgresql.org)

Nov 22 '05 #1
6 3141
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 ..


Not if you run the queries as a single transaction.

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 ?

Thx
Deep

-----Original Message-----
From: pg*****************@postgresql.org
[mailto:pg*****************@postgresql.org] On Behalf Of Richard Huxton
Sent: Tuesday, January 13, 2004 4:32 AM
To: An*************@loteco.ru
Cc: pg***********@postgresql.org
Subject: Re: [GENERAL] Parse error help needed...
On Tuesday 13 January 2004 12:01, An*************@loteco.ru wrote:

RH> Remove the "commit" line - functions cannot define their own
transactions RH> anyway.
Do you know if it will be solved sometime? Or this is architecture
dependend problem? I mean that transactions are rulez and very helpful
rulez when working with large databases.


Nested transactions are on the todo list, but I don't know when they will
appear.

--
Command Prompt, Inc., home of Mammoth PostgreSQL - S/ODBC and S/JDBC
Postgresql support, programming shared hosting and dedicated hosting.
+1-503-667-4564 - jd@commandprompt.com - http://www.commandprompt.com
Mammoth PostgreSQL Replicator. Integrated Replication for PostgreSQL
---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddressHere" to ma*******@postgresql.org)

Nov 22 '05 #2
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

---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend

Nov 22 '05 #3
On Tue, 13 Jan 2004, Joshua D. Drake wrote:
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 ..


Not if you run the queries as a single transaction.

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 ?


Joshua, aren't functions run within their own transactions if they don't
inherit one? Wouldn't that take care of this part as well?
---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

Nov 22 '05 #4
On Tue, 2004-01-13 at 13:34, scott.marlowe wrote:
On Tue, 13 Jan 2004, Joshua D. Drake wrote:
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 ..


Not if you run the queries as a single transaction.

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 ?


Joshua, aren't functions run within their own transactions if they don't
inherit one? Wouldn't that take care of this part as well?


sort of... in order to call a function you have to do "select foo()"
which creates an implicit transaction block based on the select
statement that commits when the "select statement" finishes and
everything within foo will be treated as a single transaction.

Robert Treat
--
Build A Brighter Lamp :: Linux Apache {middleware} PostgreSQL
---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend

Nov 22 '05 #5

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
---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html

Nov 22 '05 #6
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.


Both actions you mentioned are not [or are unlikely to be implemented
as] two separate transactions, but a single transaction (and thus the
subject "nested transaction" has nothing to do with this.

Nested transaction are usually used in complex operations.

Save points can be used to implement nested transaction.

Since we're using a bank as example, consider a bank with 1 million
accounts. At the end of the month, it needs to calculate and post
interest for each account. The whole operation takes 10 hours. If we use
a single transaction for this, then if the machine/database crashes, the
whole unfinished transaction will be rolled back. If the db is back up,
but then fails again in the middle of this giant transaction, it will be
rolled back again. And perhaps again... and again... and thus it will
never finishes.

With save points (and nested transactions) we can save in the middle of
transaction and later rolls back to the last save point instead of
beginning the transaction all over.

--
dave
---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

Nov 22 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Kenneth Courville | last post by:
Hello, I'm looking for a little input on this situation. I'm working on an inventory system and was thinking that I'd like to build it similiar to the way a bank keeps track of your funds. If...
0
by: P. Emigh | last post by:
A client that synchronizes over the internet encountered Error #3003: "Could not start transaction; too many transactions already nested" when attempting to synchronize. I checked user groups...
9
by: John Sidney-Woollett | last post by:
Is it possible to use the dblink and dblink_exec features from inside pl/pgsql functions to mimic the behaviour of nested transactions by calling another function or executing some SQL via the...
1
by: Thapliyal, Deepak | last post by:
Can I use a "set transaction" type mechanism within a function? thx Deep -----Original Message----- From: Joshua D. Drake Sent: Tuesday, January 13, 2004 10:17 AM To: Thapliyal, Deepak...
2
by: Andreas | last post by:
Hi, will I need "nested transactions" which - as I read - aren't implemented, yet ? I have some objects that rely on each other. Each has a status like proposal, working, canceled. table-A...
0
by: Pavel Stehule | last post by:
Hello I can't use nested transaction in perl DBI interface. I have CVS PostgreSQL. Propably DBI doesn't support last pg features. Can I set off DBI transaction control mechanism? Thank You...
2
by: Gerrit.Horeis | last post by:
Hi All, I have a problem with nested SQLQueries. I will give here an abstract sample of code which I wrote Method A { createSqlTransaction and call SQL Insert Statement including "Select...
1
by: Mana | last post by:
Hi, I want to implement nested transactions in C#. When I write BEGIN TRANSACTION inside another BEGIN TRANSACTION in an SQL Script it works fine. But when I call BeginTransaction() inside...
1
by: SMichal | last post by:
Is there some posibility how to bind a nested classes to GridView ? class BLZ { public string name; public string text; } class Konto {
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.