Re: PROBLEMS WITH TRANSACTIONS IN JDBC
Posted in 1999
ASC/Art AFAIK, in Java/JDBC there is no concept of BEGIN WORK. AutoCommit is set to true by default (like mode ANSI databases) so JDBC will attempt to commit every transaction, unless specifically told to do otherwise. So ASC is right when he sets AutoCommit to false and goes for a manual commit with con.commit(). I haven't tried this specific situation myself, since my stored procedures are logical units of work by themself, and they either succeed or fail as a unit, but what may work is the following (pseudo-code): con.setAutoCommit(false) try { con.execute("EXECUTE PROCEDURE sp1(arg11,...)"); con.execute("EXECUTE PROCEDURE sp2(arg21,...)"); // and so on... con.commit(); } catch (SQLException sqle) // or just Exception e { System.out.println("An SQL Error has occured: " + sqle); con.rollback(); } so if any of the stored procedures fail for some reason, then the entire contents of the try block should be rolled back. I may be wrong on this however, since I dont do much JDBC programming myself, I mostly write SPs and support a database which is accessed exclusively through Stored Procedures which are called from Java/JDBC. HTH Sujit "Art S. Kagel" <kagel@bloomberg.net> on 03/03/99 01:37:53 PM Please respond to kagel@bloomberg.net To: informix-list@iiug.org cc: (bcc: Sujit Pal) Subject: Re: PROBLEMS WITH TRANSACTIONS IN JDBC ASC wrote: > > Hello, > This is my problem. I have to implement a transaction in some steps. > I mean, the aim is to save the state of the database (Informix Online > Dynamic Server 7.2). Then call some stored procedures that doesn?t > commit, and if everything was ok, commit the transaction. How can I > achieve this using JDBC? > I have tried with: > con.setAutoCommit(false); > > call ... > call... > call... > > con.commit(); > > but I receive a "Not in a transaction" exception while committing. > Any idea would be appreciated. Please, some pseudocode. Don't know the ODBC function but you forgot to begin the transaction with a BEGIN WORK statement equivalent (perhaps con.begin()?), unless the database is in ANSI logging mode you need to do this before the first SP call and if all is well call con.commit() else call con.rollback(). Art S. Kagel