File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JDBC and Relational Databases and the fly likes How can I rollback when connection close? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "How can I rollback when connection close?" Watch "How can I rollback when connection close?" New topic

How can I rollback when connection close?

marie-france huot

Joined: Aug 05, 2002
Posts: 1
I work with Oracle (jbdc) in web developpment.
Presently with java, when we close the Oracle Connection, a commit is made automatically.
I want to change it to an automatic rollback when I close the connection (myConn.lose() ).
The reason is when a user submits a form (in the browser) and he clicks the stop button before the process is terminated, the connection is closed automatically and a commit is done. I don't want a commit because my source code is doing several inserts in several tables and if the process is interrupted before all is done, I want a rollback otherwise the data will be corrupted.
Can I do it and Who ? Maybe overload the close() method ? How ?
Thank You,
Praveen Kumar Jha

Joined: Jul 27, 2002
Posts: 10
Implement fa´┐Żade pattern - wrap all the method of Connection inside other class - say OraConnection with java.sql.Connection as its data member. Your application will make call to OraConnection which will have a method close(). close() will first rollback on Connection and the close the connection.

Praveen Kumar<br />Sun Certified Programmer for Java 2 Platform
sim sim
Ranch Hand

Joined: Jun 05, 2002
Posts: 55
Not sure about this, why dont u try JTA. Using transactions u can put a bunch of JDBC code in a block. It is either execute all or execute none situation. I mean to say if there are three statements, 2 are executed and 1 fails to execute, then the other 2 gets rolledback automatically.
Sim Sim.
Jamie Robertson
Ranch Hand

Joined: Jul 09, 2001
Posts: 1879

Why can't you just explicitly call conn.rollback() at every possible close point?
I agree. Here's the link:
subject: How can I rollback when connection close?
It's not a secret anymore!