File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes how to rollback in java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "how to rollback in java" Watch "how to rollback in java" New topic
Author

how to rollback in java

Tom Keaton
Greenhorn

Joined: Feb 24, 2004
Posts: 17
I have two db operates to do:
insertIntoTableA();
insertIntoTableB();
I want do it at same time,two operates succeed or fail,if one of the operates fail the other operates will be cancel.Anyone can help me,
thanks in advance.


undefined
Ali Gohar
Ranch Hand

Joined: Mar 18, 2004
Posts: 572
You can do it using JDBC. When you create a Connection object it is set to autocommit by default. First of all after creating the Connection object set the autocommit to false.
Connection con = DriverManager.getConnection(params);
con.setAutoCommmit(false);
Now you insert the records in 1st table and then second table in try catch block.
Try{
Connection con = DriverManager.getConnection(params);
con.setAutoCommmit(false);
//insert records in table1
//insert records in table2
con.commit(); //records entered successfully
}catch(Exception ex){
con.rollback(); //It means there is some problem while entering records
}
Kunaal A Trehan
Ranch Hand

Joined: Feb 04, 2004
Posts: 63
Hi
You can use this mechanism
Connection conn;
................
In the method where u re doing the insert operations do this
try
{
conn.setAutoCommit(false);
Insert data in table1
Insert data in table2
conn.commit();
}
catch(SQLException e)
{
conn.rollback();
}
I hope this helps
Rgds
Kunal
Tom Keaton
Greenhorn

Joined: Feb 24, 2004
Posts: 17
thanks above!
it works properly for one connection
but what about two different connections?
one is Sybase and the other maybe Oracle.
I am afraid it is hard to do.
Ali Gohar
Ranch Hand

Joined: Mar 18, 2004
Posts: 572
See JTA(Java Transaction API) for that. I think you can do it through JTA.
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 31079
    
163

Tom,
If you are using two databases, look into two phase commit.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
santhoshkumar samala
Ranch Hand

Joined: Nov 12, 2003
Posts: 156

If you are using single database you can use executeBatch() command
like
Statement st=null;
String qry1="insert into table A";
String qry2="insert into table B";
st.addBatch(qry1);
st.addBatch(qry2);
st.executeBatch();


santhosh<br />SCJP,SCWCD
rasit fidanov
Greenhorn

Joined: Jun 30, 2005
Posts: 15
what is the goal of executeBatch?
Is it faster?
is it faster than preparedStatement too?
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 31079
    
163

Originally posted by rasit fidanov:
what is the goal of executeBatch?
Is it faster?
is it faster than preparedStatement too?

Rasit,
The goal of executeBatch is to make a bunch of insert/update/delete statements in one trip to the database. This saves network transit time, which can really add up. As a result, it is faster than sending the statements individually.

executeBatch can be used with PreparedStatements if you are executing the same statement many times with different parameters.

For the future, note that it is prefered to start a new thread for a new question.

Jeanne
JDBC Forum Bartender
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to rollback in java