• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to rollback in java

 
Tom Keaton
Greenhorn
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ali Gohar
Ranch Hand
Posts: 572
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 63
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 572
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See JTA(Java Transaction API) for that. I think you can do it through JTA.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 33713
316
Eclipse IDE Java VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tom,
If you are using two databases, look into two phase commit.
 
santhoshkumar samala
Ranch Hand
Posts: 156
Eclipse IDE Hibernate Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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();
 
rasit fidanov
Greenhorn
Posts: 15
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is the goal of executeBatch?
Is it faster?
is it faster than preparedStatement too?
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 33713
316
Eclipse IDE Java VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic