Received: from localhost (maia-4.hub.org [200.46.204.183]) by postgresql.org (Postfix) with ESMTP id 731269FB771 for ; Wed, 11 Jul 2007 20:16:10 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 10525-04 for ; Wed, 11 Jul 2007 20:16:05 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.5 Received: from beaver.fastcrypt.com (beaver.fastcrypt.com [64.7.141.17]) by postgresql.org (Postfix) with ESMTP id 25B0F9FBEAE for ; Wed, 11 Jul 2007 20:16:07 -0300 (ADT) Received: from [192.168.1.14] (dcdsl.ebox.com [64.7.143.116]) by beaver.fastcrypt.com (Postfix) with ESMTP id 22B5F1580683; Wed, 11 Jul 2007 19:29:45 -0400 (EDT) In-Reply-To: <46953E79.5010204@xover.htu.tuwien.ac.at> References: <46953E79.5010204@xover.htu.tuwien.ac.at> Mime-Version: 1.0 (Apple Message framework v752.3) Content-Type: text/plain; charset=US-ASCII; delsp=yes; format=flowed Message-Id: Cc: pgsql-jdbc@postgresql.org Content-Transfer-Encoding: 7bit From: Dave Cramer Subject: Re: questions regarding transactions Date: Wed, 11 Jul 2007 19:16:10 -0400 To: Fabian Zeindl X-Mailer: Apple Mail (2.752.3) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200707/45 X-Sequence-Number: 17219 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 connection. 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 number. 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