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

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 (view raw or flat)
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

pgsql-interfaces by date

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

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