Reputation: 923
My intention is to try out SQL Rollback with the program exit halfway without finishing.I had the following connection in my program. However data still written to the database even when I did not set any con.commit upon exiting halfway. I did a debug mode, it fall into the exception and execute the con.rollback(); but in which is did not rollback. Which made me quite confuse.
con = DriverManager.getConnection(value);
con.setAutoCommit(false);
try{
pstmt = con.prepareStatement("CREATE TABLE IF NOT EXISTS Test (Number varchar(255))")
pstmt.execute();
pstmt = con.prepareStatement("INSERT INTO `Test` (Number) values (?)");
pstmt.setString(1,"1");
pstmt.executeUpdate();
}catch(Exception){
con.rollback();
}
Upvotes: 0
Views: 95
Reputation: 136022
It depends on DB, e.g. in Oracle Create table ... or any DDL execute implicit commit, all operations before this commit cannot be rollbacked
Upvotes: 1