Skip site navigation (1) Skip section navigation (2)

Re: questions regarding transactions

From: Dave Cramer <pg(at)fastcrypt(dot)com>
To: Fabian Zeindl <fabian(at)xover(dot)htu(dot)tuwien(dot)ac(dot)at>
Cc: pgsql-jdbc(at)postgresql(dot)org
Subject: Re: questions regarding transactions
Date: 2007-07-11 23:16:10
Message-ID: (view raw, whole thread or download thread mbox)
Lists: pgsql-jdbc
On 11-Jul-07, at 4:32 PM, Fabian Zeindl wrote:

> Hi,
>  I'm using JDBC with transactions and I've got a couple of questions:
> 1) Am I supposed to do "myConn.rollback()" on error? What happens if I
> don't do it - will the transaction stay half-committed?! (I ask this,
> because rollback() can throw a SQLException, so it's not guaranteed to
> work, in my opinion).
> 2) When I use statements like (SELECT currval('somesequence')) in a
> jdbc-transaction, will it be atomic to? Will the sequence be  
> decremented
> again when the transaction fails? Can I get wrong numbers, when  
> there is
> another transaction which increments somesequence?
This is a very common question.
1) currval has no value without a preceding nextval within the same  
  In other words you open a connection, call nextval on the sequence  
and it is stored in the connections memory.
Another connection does the same thing and gets a totally different  
As Heikki pointed out sequences do not roll back so there is no  
chance to get the wrong number as long as you use nextval, and currval
> 3) Is it sufficient to do setAutocommit(false) and set the
> transaction-level or do I have to call a statement like "START
> TRANSACTION" to properly start an transaction? (I found
> the latter example somewhere on the net.)
> Many thanks in advance and thank you for the wonderful driver
> fabian zeindl

In response to

pgsql-jdbc by date

Next:From: Luca FerrariDate: 2007-07-12 08:41:39
Subject: Re: postgres jdbc into eclipse plugin (OID problems)
Previous:From: Heikki LinnakangasDate: 2007-07-11 21:07:55
Subject: Re: questions regarding transactions

Privacy Policy | About PostgreSQL
Copyright © 1996-2017 The PostgreSQL Global Development Group