SavePoint Interface

Advertisements

Prev Tutorial Next Tutorial

SavePoint Interface in JDBC

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

Example of Savepoint in JDBC

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

Google Advertisment

Buy This Ad Space @$20 per Month, Ad Size 600X200 Contact on: hitesh.xc@gmail.com or 9999595223

Magenet is best Adsense Alternative here we earn $2 for single link, Here we get links ads. Magenet

For Projects 9999595223

Google Advertisements


Buy Websites 9999595223

Buy College Projects with Documentation Contact on whatsapp 9999595223. Contact on: hitesh.xc@gmail.com or 9999595223 Try this Keyword C++ Programs

Advertisements