SavePoint Interface

Google 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 Advertisements

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

Pure VPN Privide Lowest Price VPN Just @ $1.65. Per Month with Non Detected IP Lowest Price Non Detected IP VPN

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

Cloud computing is the on demand availability of computer system resources, especially data storage and computing power, without direct active management by the user. Cloud Computing Tutorial

College Projects Related to Java, AWT, C Projects for College, C++ Projects for College, Android Projects. Download Java C C++ Projects

Download Projects

Adsense Advertisements


Buy This ads Space 8076671483

Buy This Ad Space @$120 per Month, Ad Size 300X600 Contact on: hitesh.xc@gmail.com or 8076671483 Try this Keyword C++ Programs