This week's book giveaway is in the Java in General forum.
We're giving away four copies of Helidon Revealed: A Practical Guide to Oracle’s Microservices Framework and have Michael Redlich on-line!
See this thread for details.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

how to rollback in java

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 572
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
}
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See JTA(Java Transaction API) for that. I think you can do it through JTA.
 
author & internet detective
Posts: 42055
926
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tom,
If you are using two databases, look into two phase commit.
 
Ranch Hand
Posts: 156
Hibernate Eclipse IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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();
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 42055
926
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic