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!