Re: how to continue using a connection after an error with autocommit=false

Lists: pgsql-jdbc
From: "James Im" <im-james(at)hotmail(dot)com>
To: pgsql-jdbc(at)postgresql(dot)org
Subject: how to continue using a connection after an error with autocommit=false
Date: 2007-02-20 10:41:37
Message-ID: BAY7-F2720A1254B9177F763D58796890@phx.gbl
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-jdbc

Hi,

I just find out that I cannot continue using a connection when I use
autocommit=false and that an sql insert failed.

In pseudo code this is what I'd like to do:

1) create connection
2) set autocommit = false
3) do an insert that succeed
4) do an insert that fails
5) catch the SQLException (I don't do a rollback on purpose)
6) do a select with the same connection
7) do another insert
8) commit

Everything goes well until step (6). where I receive the following error:

ERROR: current transaction is aborted, commands ignored until end of
transaction block
Exception: org.postgresql.util.PSQLException
org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:1525)
org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1309)
org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:188)
org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:452)
org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:354)
org.postgresql.jdbc2.AbstractJdbc2Statement.executeQuery(AbstractJdbc2Statement.java:258)

Can I do something is step (5) so that I don't get the error in step (6) ?
I need to continue using the same connection.

Thanks you your help.

_________________________________________________________________
Vlg selv hvordan du vil kommunikere - skrift, tale, video eller billeder
med MSN Messenger: http://messenger.msn.dk/ - her kan du det hele


From: Heikki Linnakangas <heikki(at)enterprisedb(dot)com>
To: James Im <im-james(at)hotmail(dot)com>
Cc: pgsql-jdbc(at)postgresql(dot)org
Subject: Re: how to continue using a connection after an error with autocommit=false
Date: 2007-02-20 10:50:50
Message-ID: 45DAD28A.2010907@enterprisedb.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-jdbc

James Im wrote:
> I just find out that I cannot continue using a connection when I use
> autocommit=false and that an sql insert failed.

That's intended behavior in PostgreSQL. I just wrote this in another
thread last week:

If you have a statement in your transaction that you know might fail,
you can use savepoints to avoid having to restart the whole transaction:

Savepoint sp = conn.setSavepoint();
try {
stmt.executeQuery("SELECT 1 FROM table_that_might_not_exist");
} catch(SQLException ex)
{
sp.rollback(sp);
}
stmt.executeQuery("SELECT * FROM table_that_exists");
...

--
Heikki Linnakangas
EnterpriseDB http://www.enterprisedb.com


From: Altaf Malik <mmalik_altaf(at)yahoo(dot)com>
To: James Im <im-james(at)hotmail(dot)com>, pgsql-jdbc(at)postgresql(dot)org
Subject: Re: how to continue using a connection after an error with autocommit=false
Date: 2007-02-20 10:53:46
Message-ID: 305445.70270.qm@web39109.mail.mud.yahoo.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-jdbc

You can either commit or rollback the transaction when you encounter an SQL error. This will alow the next statements to execute successfully.

--Altaf Malik
EnterpriseDB
www.enterprisedb.com

James Im <im-james(at)hotmail(dot)com> wrote:
Hi,

I just find out that I cannot continue using a connection when I use
autocommit=false and that an sql insert failed.

In pseudo code this is what I'd like to do:

1) create connection
2) set autocommit = false
3) do an insert that succeed
4) do an insert that fails
5) catch the SQLException (I don't do a rollback on purpose)
6) do a select with the same connection
7) do another insert
8) commit

Everything goes well until step (6). where I receive the following error:

ERROR: current transaction is aborted, commands ignored until end of
transaction block
Exception: org.postgresql.util.PSQLException
org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:1525)
org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1309)
org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:188)
org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:452)
org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:354)
org.postgresql.jdbc2.AbstractJdbc2Statement.executeQuery(AbstractJdbc2Statement.java:258)

Can I do something is step (5) so that I don't get the error in step (6) ?
I need to continue using the same connection.

Thanks you your help.

_________________________________________________________________
Vælg selv hvordan du vil kommunikere - skrift, tale, video eller billeder
med MSN Messenger: http://messenger.msn.dk/ - her kan du det hele

---------------------------(end of broadcast)---------------------------
TIP 9: In versions below 8.0, the planner will ignore your desire to
choose an index scan if your joining column's datatypes do not
match


---------------------------------
Don't get soaked. Take a quick peak at the forecast
with theYahoo! Search weather shortcut.


From: Scott Marlowe <smarlowe(at)g2switchworks(dot)com>
To: Altaf Malik <mmalik_altaf(at)yahoo(dot)com>
Cc: James Im <im-james(at)hotmail(dot)com>, pgsql-jdbc(at)postgresql(dot)org
Subject: Re: how to continue using a connection after an error with autocommit=false
Date: 2007-02-20 15:39:41
Message-ID: 1171985981.25338.44.camel@state.g2switchworks.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-jdbc

On Tue, 2007-02-20 at 04:53, Altaf Malik wrote:
> You can either commit or rollback the transaction when you encounter
> an SQL error. This will alow the next statements to execute
> successfully.

You can't commit after an error in PostgreSQL unless you've rolled back
to a savepoint from before the error.


From: Tim Pizey <timp(at)melati(dot)org>
To: pgsql-jdbc(at)postgresql(dot)org
Subject: Re: how to continue using a connection after an error with autocommit=false
Date: 2007-02-28 20:41:12
Message-ID: 200702282041.13013.timp@melati.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-jdbc

On Tuesday 20 February 2007 10:50, Heikki Linnakangas wrote:
> James Im wrote:
> > I just find out that I cannot continue using a connection when I use
> > autocommit=false and that an sql insert failed.
>
> That's intended behavior in PostgreSQL.
[snip]
This is new behaviour in version 8, I believe.

I think this behaviour is unique to Postgresql?

Melati was developed with Postgresql version 6 as the target dbms
back in 2000, since then it has been extended to work with the other common
dbmsen, but now it looks like it won't work with V8, without quite a bit of
rework.

Is this behaviour specified by a standard?

What are the advantages?

yours
Tim Pizey

--
http://melati.org/ - the flower of Java.


From: Dave Cramer <pg(at)fastcrypt(dot)com>
To: Tim Pizey <timp(at)melati(dot)org>
Cc: pgsql-jdbc(at)postgresql(dot)org
Subject: Re: how to continue using a connection after an error with autocommit=false
Date: 2007-02-28 22:33:40
Message-ID: DE290AFE-67A3-4B0E-BFD1-B83533ED76A8@fastcrypt.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-jdbc


On 28-Feb-07, at 3:41 PM, Tim Pizey wrote:

> On Tuesday 20 February 2007 10:50, Heikki Linnakangas wrote:
>> James Im wrote:
>>> I just find out that I cannot continue using a connection when I use
>>> autocommit=false and that an sql insert failed.
>>
>> That's intended behavior in PostgreSQL.
> [snip]
> This is new behaviour in version 8, I believe.
>
No, this has been around since 7.x
> I think this behaviour is unique to Postgresql?
>
Possibly
> Melati was developed with Postgresql version 6 as the target dbms
> back in 2000, since then it has been extended to work with the
> other common
> dbmsen, but now it looks like it won't work with V8, without quite
> a bit of
> rework.
>
> Is this behaviour specified by a standard?
>
> What are the advantages?
>
The advantage is that a transaction is atomic.

You can continue to use the connection, you simply have to rollback
the transaction after the error and start over.
You can also use savepoints.
> yours
> Tim Pizey
>
> --
> http://melati.org/ - the flower of Java.
>
> ---------------------------(end of
> broadcast)---------------------------
> TIP 1: if posting/reading through Usenet, please send an appropriate
> subscribe-nomail command to majordomo(at)postgresql(dot)org so that
> your
> message can get through to the mailing list cleanly
>


From: Tim Pizey <timp(at)melati(dot)org>
To: pgsql-jdbc(at)postgresql(dot)org
Subject: Re: how to continue using a connection after an error with autocommit=false
Date: 2007-03-01 20:59:29
Message-ID: 200703012059.29908.timp@melati.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-jdbc

On Wednesday 28 February 2007 22:33, Dave Cramer wrote:
> On 28-Feb-07, at 3:41 PM, Tim Pizey wrote:
> > On Tuesday 20 February 2007 10:50, Heikki Linnakangas wrote:
> >> James Im wrote:
> >>> I just find out that I cannot continue using a connection when I use
> >>> autocommit=false and that an sql insert failed.
> >>
> >> That's intended behavior in PostgreSQL.
> >
> > [snip]
> > This is new behaviour in version 8, I believe.
>
> No, this has been around since 7.x
>
> > I think this behaviour is unique to Postgresql?
>
> Possibly
>
> > Melati was developed with Postgresql version 6 as the target dbms
> > back in 2000, since then it has been extended to work with the
> > other common
> > dbmsen, but now it looks like it won't work with V8, without quite
> > a bit of
> > rework.
> >

Sorry, my bad, all Melati's tests pass on 8.1.4, without modification.

yours
Tim Pizey

--
http://melati.org/ - the flower of Java.