473,799 Members | 2,683 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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****** ***********@pos tgresql.org] On Behalf Of Richard Huxton
Sent: Tuesday, January 13, 2004 4:32 AM
To: An************* @loteco.ru
Cc: pg***********@p ostgresql.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 YourEmailAddres sHere" to ma*******@postg resql.org)

Nov 22 '05 #1
6 3188
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****** ***********@pos tgresql.org] On Behalf Of Richard Huxton
Sent: Tuesday, January 13, 2004 4:32 AM
To: An************* @loteco.ru
Cc: pg***********@p ostgresql.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
transaction s 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@commandpromp t.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 YourEmailAddres sHere" to ma*******@postg resql.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_tim e,notes) VALUES
(1,'CASHOUT',no w(),'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
1889
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 you look at at bank statement, for each line item, you'll normally see at least date/time, description, amount (plus or minus value), and balance. In my inventory system, the amount and balance would be a count of the
0
4043
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 and MS Knowledge Base and found nothing helpful. This didn't seem to be acknowledged as a synchroization error. Investigations revealed data errors that resulted in a cascade of related errors. Resolving the data errors apparently cured whatever...
9
7620
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 dblink (into the same database)? Does the SQL statement executed within the dblink sit in its own isolated transaction/session? And would an error thrown in the dblink statement abort the enclosing session that initiated the call? What is the...
1
725
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 Cc: 'Richard Huxton'; Anton.Nikiforov@loteco.ru;
2
1913
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 <--- table-B <--- table-C <--- table-D
0
1442
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 Pavel Stehule
2
1957
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 ScopeIdentity();" try {
1
18876
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 another BeginTransaction() in a c# code on same connection object it throws exception as "SQLConnection doesnot support parallel transaction". Following is the code snippet that i have written.
1
2355
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
9689
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9550
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10495
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10248
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10032
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9085
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
4148
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3764
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2942
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.