I have a jdbc code in which I have used transaction management in code.
Following is the code.I am using Mysql Database.
public class JdbcConn {
public static void main(String[] args){
Savepoint spt1 = null;
Connection con = null;
try{
Class.forName("org.gjt.mm.mysql.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost","root","tany");
con.setAutoCommit(false);
spt1= con.setSavepoint("svpt1");
PreparedStatement psmt;
String query1 = "select city, countryid from querytest.city;";
psmt=con.prepareStatement(query1);
ResultSet rs=psmt.executeQuery();
while(rs.next()){
String query2 = "insert into sun.city (city,countryid) values('"+rs.getString(1)+"',"+rs.getInt(2)+");";
psmt=con.prepareStatement(query2);
psmt.executeUpdate();
}
String query3 = "create database `transtest`;";
psmt=con.prepareStatement(query3);
psmt.executeUpdate();
String query4 = "CREATE TABLE `transtest`.`trans` (`id` tinyint(4) NOT NULL auto_increment,`val` int(5) NOT NULL default 0, PRIMARY KEY (`id`)) ENGINE=MyISAM;";
psmt=con.prepareStatement(query4);
psmt.executeUpdate();
String query5 = "CREATE TABLE `transtest`.`transone` (`id` tinyint(4) NOT NULL auto_increment,`val` int(5) NOT NULL default 0, PRIMARY KEY (`id`)) ENGINE=MyISAM;";
psmt=con.prepareStatement(query5);
psmt.executeUpdate();
String query6 = "CREATE TABLE `transtest`.`transtwo` (`id` tinyint(4) NOT NULL auto_increment,`val` int(5) NOT NULL default 0, PRIMARY KEY (`id`)) ENGINE=MyISAM;";
psmt=con.prepareStatement(query6);
psmt.executeUpdate();
for(int i=1;i<=10;i++){
String query7 = "insert into `transtest`.`transtwo` (`val`) values ("+i*2+");";
psmt=con.prepareStatement(query7);
psmt.executeUpdate();
}
String query8 = "insertd into `transtest`.`trans` (`val`) values (500);";
psmt=con.prepareStatement(query8);
psmt.executeUpdate();
JOptionPane.showMessageDialog(null, "Process completed!");
con.commit();
con.setAutoCommit(true);
}catch(SQLException sqle){
try {
con.rollback(spt1);
JOptionPane.showMessageDialog(null, "Rollback1!");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
sqle.getMessage();
sqle.printStackTrace();
}catch (ClassNotFoundException cnfe) {
// TODO Auto-generated catch block
try {
con.rollback(spt1);
JOptionPane.showMessageDialog(null, "Rollback2!");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
cnfe.getMessage();
cnfe.printStackTrace();
}catch (Exception e) {
// TODO Auto-generated catch block
try {
con.rollback(spt1);
JOptionPane.showMessageDialog(null, "Rollback3!");
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
e.getMessage();
e.printStackTrace();
}
}
}
This above code is not rolling back when sql exception comes.
The schema of the table in query1 and the schema of the table in query2 is same but as you see the databases are different.
I just dont know if any exception comes why its not rolling back the changes made by the querys from query2 to query7.
I had intensionally made a syntactical mistake in query8 for an exception.
Please guide me friends in this issue and plaease lent me know my mistakes in code.
Thank You!
Here’s how I would do it:
In other words, do the connection close and rollback in the finally block … and don’t repeat the code.
And DON”T catch
Exception… see below.A comment on the Question says this:
Catching
Exceptionand especiallyThrowableis a bad idea UNLESS the next action after handling the exception is to exit the application. There are any number of potential unchecked exceptions / errors that might occur. You’ve no way of knowing what the cause of the unexpected exception was, or whether the application can safely recover.Perhaps it is because some of those statements are non-transactional (e.g. CREATE TABLE), and executing a non-transactional statement causes the current transaction to commit automatically.
Perhaps it is a problem with the old version of MySQL and the JDBC drivers that you are using. The “org.gjt.mm.mysql” driver is REALLY old, and I know for a fact that early versions of MySQL did not support transactions at all.
As I suspected, you can’t rollback a CREATE TABLE in MySQL.
Source: