pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Oliver Jowett <oliver@opencloud.com>
To: j.random.programmer <javadesigner@yahoo.com>
Cc: pgsql-jdbc@postgresql.org
Subject: Re: Weird behavior in transaction handling (Possible bug ?)
Date: Sat, 15 Jan 2005 10:38:41 +1300
Message-ID: <41E83BE1.2020909@opencloud.com> (raw)
In-Reply-To: <20050114161304.32600.qmail@web14223.mail.yahoo.com>
References: <20050114161304.32600.qmail@web14223.mail.yahoo.com>

j.random.programmer wrote:

> Here is the problem. The commit() will NEVER work and
> no data is ever saved to any table in the database.
> No error message is generated, the commit() SILENTLY
> fails to insert any data.
> 
> However, if I comment out the second insert into table
> #2
> (which was causing an error), then the inserts work
> and the transaction is committed().

When postgresql hits an error, the transaction is marked for rollback 
and all subsequent queries in that transaction will fail. A subsequent 
COMMIT will not actually commit; it will roll back. There are arguments 
both ways about whether this is a good idea (mostly correctness vs. 
compatibility with other systems), but that's the way it is and the way 
it has been for ages. Don't ignore errors from your queries!

...

It might be worthwhile having commit() throw an exception if the 
transaction did not actually commit, rather than only reporting 
server-generated errors. What do people think?

Pre-7.4 returns a COMMIT status for any COMMIT even if the transaction 
actually rolled back, and the v2 protocol has no mechanism to detect 
transactions that have failed. So the only way to detect this would be 
to track transaction state internally -- seems a bit ugly and unreliable.

7.4 returns COMMIT for rolled-back COMMITs, but does report transactions 
that have failed via the v3 protocol. 8.0 returns ROLLBACK for 
rolled-back COMMITs and also uses the v3 protocol. So it should be 
possible to detect this case for both 7.4 and 8.0 reasonably easily.

...

Also in 8.0 and later, there is savepoint support that helps with this 
case. The pattern to use is something like this:

   establish savepoint
   INSERT ....;
   if insert caused an error:
     rollback to savepoint
   else:
     release savepoint

See java.sql.Savepoint, and the Postgres docs on SAVEPOINT for more info.

That pattern will cause a subtransaction to be started for the INSERT. 
If the INSERT fails, and we ROLLBACK TO SAVEPOINT, then all the results 
of the INSERT (including the marking-txn-for-rollback) are discarded and 
your original transaction can continue.

There is a performance cost when using savepoints, but I don't know how 
large.

It'd be possible to have optional "automatic savepoint wrapping" in the 
driver, where every user query was transparently wrapped in 
subtransaction. You might prefer to write the code to make the driver do 
this, rather than change your application.

-O



view thread (22+ messages)  latest in thread

Message-ID: <41E83BE1.2020909@opencloud.com>
Permalink:  ../41E83BE1.2020909@opencloud.com/
Also on:    postgresql.org/message-id/41E83BE1.2020909@opencloud.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-jdbc@postgresql.org
  Cc: oliver@opencloud.com, javadesigner@yahoo.com
  Subject: Re: Weird behavior in transaction handling (Possible bug ?)
  In-Reply-To: <41E83BE1.2020909@opencloud.com>
  References: <20050114161304.32600.qmail@web14223.mail.yahoo.com> <41E83BE1.2020909@opencloud.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox