Re: Unable to commit: transaction marked for rollback

From: Craig Ringer <craig(at)postnewspapers(dot)com(dot)au>
To: Radosław Smogura <mail(at)smogura(dot)eu>
Cc: pgsql-jdbc(at)postgresql(dot)org
Subject: Re: Unable to commit: transaction marked for rollback
Date: 2010-07-04 02:20:22
Message-ID: 4C2FEFE6.7010306@postnewspapers.com.au
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-jdbc

On 03/07/10 16:36, Radosław Smogura wrote:

> I've looked at error from your example, because I didn't belived such
> behavior. Unfortunatly it's true. Postgresql disallow to execute next
> statement if error occured - this is realy bad approach, because error can be
> from dupliacte keys etc. :(

If you want to handle errors and continue, use a subtransaction with
BEGIN SAVEPOINT and ROLLBACK TO SAVEPOINT.

Most of the time it's a performance advantage - and perfectly suitable
for apps - to avoid keeping a rollback point for each statement, and
instead chuck the whole transaction away if something goes wrong. Most
apps expect to retry the whole transaction if anything goes wrong
anyway, since it's often hard to tell if the issue can be resolved by
retrying just one (possibly altered) statement or not.

It's also safer. If Pg allowed a transaction to continue after an INSERT
failed, it'd end up committing an incomplete set of the requested
changes and hoping that the user/app noticed. I don't like that - I'd
much rather have to explicitly handle the issue, and otherwise have an
error in a transaction be an error that aborts the transaction, not just
the particular statement that caused the error.

For bulk insertion, you're almost always better off copying the data
into a temporary table, cleaning it up if necessary, then using INSERT
INTO ... SELECT to insert it duplicate-free.

I guess an optional mode that issued an implicit savepoint before each
transaction and allowed "ROLLBACK TO LAST STATEMENT" would be nice,
though, as a convenience to avoid all the savepoint management on the
rare occasions when you do want statement-level rollback.

--
Craig Ringer

In response to

Responses

Browse pgsql-jdbc by date

  From Date Subject
Next Message Radosław Smogura 2010-07-04 09:59:48 Re: Unable to commit: transaction marked for rollback
Previous Message Radosław Smogura 2010-07-03 08:36:46 Re: Unable to commit: transaction marked for rollback