Re: Transaction processing from a Perl script....

From: Geraint Jones <geraint(dot)jones(at)meirion-dwyfor(dot)ac(dot)uk>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: Transaction processing from a Perl script....
Date: 2002-08-23 10:43:37
Message-ID: 200208231043.37169.geraint.jones@meirion-dwyfor.ac.uk
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Thursday 22 August 2002 8:36 pm, Björn Lundin wrote:
> Issue the exec procedure with like
>
> Exec("Begin Work")
> Exec("insert into child table 1 ")
> Exec("insert into child table 2 ")
> Exec("insert into the master table")
> Exec("Commit")
> check result of commit,
> if not everything is ok
> Exec("Rollback")
>
> I dont know the exact Perl syntax, but this is how I would go about to do
> it...
>
> /Björn
>
> Greg Patnude wrote:
> > Ok.... I can connect to my postgreSQL database with Autocommit=>0 to set
> > up for a transaction-based process...I'm using Perl 5.6 and Pg 1.9 on a
> > FreeBSD 4.x box with postgreSQL 7.2.
> >
> > The question is HOW should I go about managing the transaction itself
> > from within my Perl script... I need to {pseudo}:
> >
> > --begin work
> > --insert into child table 1 (and return the new primary key pk1)
> > --insert into child table 2 (and retrun the new primary key pk2)
> > --insert into the master table (child 1 pk, child 2 pk) and return the
> > master primary key pkm
> > --commit work if everything is ok
> > --else rollback if anything failed.
> >
> > Usually, this kind of thing is handled by the connect method (I've
> > typically used Oracle, Sybase, or SQL Server with VB, C, or Powerbuilder)
> > and the
> > {begin {process}{commit or rollback}} is managed by the connect method
> > defined in the application. Pg has no such mechanism that I could find in
> > the Pg docs...
> >
> > Any ideas anyone ???
> >
> > TIA
> >
> > GP
>
> ---------------------------(end of broadcast)---------------------------
> TIP 5: Have you checked our extensive FAQ?
>
> http://www.postgresql.org/users-lounge/docs/faq.html

Have a look at Chapter 3 of the docs (Advanced Features), there's a bit about
transactions there which says PostgreSQL automatically supports them.

Excerpt:
"PostgreSQL actually treats every SQL statement as being executed within a
transaction. If you don't issue a BEGIN command, then each individual
statement has an implicit BEGIN and (if successful) COMMIT wrapped around it.
A group of statements surrounded by BEGIN and COMMIT is sometimes called a
transaction block. "
--
Geraint Jones

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Tourtounis Sotiris 2002-08-23 10:46:40 Why index scan doesn't work ???
Previous Message Richard Huxton 2002-08-23 09:16:45 Re: trigger fired on changes in specific column