It is a feature of JDBC Transactions to Preserve Data Integrity. Savepoint set the initialization position and the rollback unsaved all the change made starting from the savepoint where it is set.
It is a feature of JDBC Transactions to Preserve Data Integrity. Savepoint set the initialization position and the rollback unsaved all the change made starting from the savepoint where it is set.Description:
It is a feature of JDBC Transactions to Preserve Data Integrity. Savepoint set the initialization position and the rollback unsaved all the change made starting from the savepoint where it is set.
Here in this code you will see that Savepoint savep1 = con.setSavepoint("SAVEPOINT1"); sets its position.
Insertion operation is done but no permanently changes because it rollback the insertion operation.
Code:
package package1;
import java.sql.*;
public class prog1{
public static void main(String[] args) {
System.out.println("Demonstrating the savepoint and rollback in java!");
Connection con = null;
String url = "jdbc:mysql://localhost:3306/";
String db = "mydb";
String driver = "com.mysql.jdbc.Driver";
try{
Class.forName(driver);
con = DriverManager.getConnection(url+db,"root","root");
try{
con.setAutoCommit(false);
Statement st = con.createStatement();
Savepoint savep1 = con.setSavepoint("SAVEPOINT1");
st.executeUpdate("INSERT into employee VALUES("+1+","+"'raman'"+")");
System.out.println("1 row affected");
con.rollback(savep1);
System.out.println("Insertion done but rollback so no row affected");
con.commit();
}
catch (SQLException s){
System.out.println("SQL statement is not executed!");
}
}
catch (Exception e){
e.getMessage();
}
}
}
Output
Demonstrating the savepoint and rollback in java!
1 row affected
Insertion done but rollback so no row affected