RE: [INTERFACES] JDBC (executing transactions coding style) - Mailing list pgsql-interfaces

From Peter Mount
Subject RE: [INTERFACES] JDBC (executing transactions coding style)
Date
Msg-id A9DCBD548069D211924000C00D001C441DD458@exchange.maidstone.gov.uk
Whole thread Raw
List 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@it.maidstone.gov.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@flex.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

pgsql-interfaces by date:

Previous
From: Peter Mount
Date:
Subject: RE: [INTERFACES] JDBC and Postgres
Next
From: Thomas Lockhart
Date:
Subject: Re: [INTERFACES] JDBC (executing transactions coding style)