SavePoint Interface


Prev Tutorial Next Tutorial

SavePoint Interface

SavePoint is an interface of java.sql package. It is introduced in JDBC 3.0 version. A savepoint is a way of implementing subtransactions (also known as nested transactions) within a relational database management system.

Need of Save Point ?

By usning SavePoint interface we can divide a large transaction into logically different transaction.If we want to produce one part of operations of a transaction from another part of operations of a transaction then in the middle we put a SavePoint.

Advantage of SavePoint

In operation of a transaction are executed upto the SavePoint successfully then the operations can be protected even if any failure is occurred in operations after the SavePoint.

Create SavePoint

To create a SavePoint in the middle of a transaction, we need to call setSavePoint() method of Connection interface.

Check SavePoint supported by driver or not

All Jdbc drivers does not provides SavePoint feature, because it is an optional feature. To check whether a jdbc driver has the implementation of this feature or not, we call supportSavePoint() method of DatabaseMetaData interface.

Syntax

DatabaseMetaData dbmd=con.getMetaData();
if(dbmd.supportSavePoint())
{
System.out.println("This driver support SavePoint");
}
else
{
System.out.println("This driver does not support SavePoint");
}

Note: Multiple savepoints can exist within a single transaction. Savepoints are useful for implementing complex error recovery in database applications

Syntax

import java.sql.*;
class SavepointDemo
{
public static void main(String[] args) throws Exception
{
Class.forName("oracle.jdbc.OracleDriver");
Connection con=DriverManager.getConnection("jdbc:oracle:thin:@rama-pc:1521:xe","system","system");
Statement stmt=con.createStatement();
//disable auto commit
con.setAutoCommit(false);
DatabaseMetaData dbmd=con.getMetaData();
if(dbmd.supportsSavepoints())
{
//savepoint is supported
try
{
int i1=stmt.executeUpdate("insert into emp_info values(222,'johon','Mumbai',69000)");
Savepoint point1=con.setSavepoint("spoint1");
try
{
int i2=stmt.executeUpdate("delete from student where sid=111");
}
catch (Exception e1)
{
try
{
con.rollback(point1);
}
catch (Exception ee)
{
}
}
con.commit();
System.out.println("this driver is supported successfully");
}//end of outer try
catch ( Exception e2)
{
try
{
con.rollback();
}
catch ( Exception eee)
{
}
}//end of catch
}//end of if
else
{
System.out.println("this driver dosen't support savepoints");
}//end of else
stmt.close();
con.close();
}
}

Prev Tutorial Next Tutorial

Advertisements




Facebook Likes

Advertisements