RE: [INTERFACES] JDBC (executing transactions coding style)

From: Peter Mount <petermount(at)it(dot)maidstone(dot)gov(dot)uk>
To: Constantin Teodorescu <teo(at)flex(dot)ro>, PostgreSQL Interfaces <pgsql-interfaces(at)postgresql(dot)org>
Subject: RE: [INTERFACES] JDBC (executing transactions coding style)
Date: 1999-04-15 13:49:01
Message-ID: A9DCBD548069D211924000C00D001C441DD458@exchange.maidstone.gov.uk
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-interfaces

Don't use BEGIN and COMMIT within JDBC. To do this, you use the
setAutoCommit() method in the Connection to enable transactions.

Although your code works at the moment, technically we should wrap all
statements with their own BEGIN ... COMMIT statements while AutoCommit
is true (which is the default).

As for the catch() block, the Connection interface has the method that
needs to be called to ABORT the transaction.

Peter

--
Peter T Mount, IT Section
petermount(at)it(dot)maidstone(dot)gov(dot)uk
Anything I write here are my own views, and cannot be taken as the
official words of Maidstone Borough Council

-----Original Message-----
From: Constantin Teodorescu [mailto:teo(at)flex(dot)ro]
Sent: Thursday, April 15, 1999 11:45 AM
To: PostgreSQL Interfaces
Subject: [INTERFACES] JDBC (executing transactions coding style)

I want to execute multiple SQL commands (also insert,updates and selects
) in a transaction block.

Is the following coding style correct ?

Statement st;
ResultSet rs;

try {
st.executeUpdate("BEGIN");
st.executeUpdate("INSERT INTO ...");
st.executeUpdate("DELETE FROM ...");
rs = st.executeQuery("SELECT FROM ...");
if (rs != null) {
while ( rs.next() ) {
// do different things
}
}
rs.close();
st.executeUpdate("UPDATE ...");
st.executeUpdate("COMMIT TRANSACTION");
} catch (SQLException sqle) {
sqle.printStackTrace();
// ABORT TRANSACTION NEEDED ?
}

What I want to know : is there necessary to do a st.executeUpdate("ABORT
TRANSACTION") in the catch instruction block ?
I recall that someone says that an error inside a transaction block
automatically aborts the transaction.
Is it true ? It works here ?

For other databases it might be necessary to do that.
Then, the st.executeUpdate("ABORT"); must be included also in another
try..catch block, true ?

Thanks a lot,
--
Constantin Teodorescu
FLEX Consulting Braila, ROMANIA

Browse pgsql-interfaces by date

  From Date Subject
Next Message Thomas Lockhart 1999-04-15 14:07:32 Re: [INTERFACES] JDBC (executing transactions coding style)
Previous Message Peter Mount 1999-04-15 13:42:49 RE: [INTERFACES] JDBC and Postgres