Re: questions regarding transactions

From: Heikki Linnakangas <heikki(at)enterprisedb(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 21:07:55
Message-ID: 469546AB.5060905@enterprisedb.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-jdbc

Fabian Zeindl wrote:
> 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).

Yes. The transaction will stay open until you end it with commit,
rollback, or disconnect the connection.

> 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?

Sequences are not transactional in that sense. If a transaction is
rolled back, the sequence won't be decremented. If you really need to
guarantee that there's no gaps in a series of numbers, you'll need to
use other means.

I'm not sure what you mean by wrong numbers, but two transactions will
never get the same number if that's what you mean. Sequences are safe to
use concurrently from multiple transactions, that's what they're for.

> 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.)

setAutocommit is the right way to do it. You shouldn't issue BEGIN or
any other transaction-related statements yourself.

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

In response to

Browse pgsql-jdbc by date

  From Date Subject
Next Message Dave Cramer 2007-07-11 23:16:10 Re: questions regarding transactions
Previous Message Fabian Zeindl 2007-07-11 20:32:57 questions regarding transactions