What is save point ?

Save point is introduced in jdbc 3.0 is a virtual marker that defines the tasks at which the rollback stops.
Suppose in a bank transfer money from one account to another. Here three tasks inside Transaction
1-One account is debited
2-One account is credited
3-Sending conformation mail to both account holders.
Suppose the mail server is down. And could not send the mail. So is it necessary to rollback the whole transaction? The mail can be sent later but the transfer is necessary.
The J2EE component can control the number of tasks that are rolled back by using save points.
In the previous example the task before the email conformation notice is sent can be designed as a save point.
There can be many savepoints used in a transaction .Each savepoint is identified by a unique name. The savepoint name is then passed to the rollback() method to specify the point within the transaction where the rollback should stop.

Example:

Try{
Connection.setAutoCommit(false);
Statement.executeUpdate(query1);
Statement.executeUpdate(query2);
SavePoint s=Connection.setSavePoint(“sp”);
Statement.executeUpdate(query3);
Statement.executeUpdate(query4);
Connection.commit();
Connection.releaseSavePoint();
System.out.println(“successfully completed”);
}//end of try block
catch(Exception e){
Connection.rollback(sp);
System.out.println(“to sp success”);
}
Here we define one save point after two statements. And passed the name to the rollback() method.
Suppose there is an exception occears in the 4th statement. So the rollback method will rollback 3rd statement but not 1st and 2nd .

No comments:

Post a Comment