Wednesday, July 3, 2024
HomeLanguagesJavaHow to Create a Savepoint in JDBC?

How to Create a Savepoint in JDBC?

A Savepoint object is used to save the current state of the database which can be rolled-back afterwards to that state of the database. Savepoints are similar to the SQL Transactions and are generally to rollback if something goes wrong within the current transaction. The connection.setSavepoint() method of Connection interface in Java is used to create an object which references a current state of the database within the transaction. The following example shows the usage of Savepoint and Rollback in JDBC application.

syntax

connection.setSavepoint()

Returns: It returns a new Savepoint object.

Exceptions: SQLException is thrown if a database access error occurs, this method is called while participating in a distributed transaction, this method is called on a closed connection or this Connection object is currently in auto-commit mode. SQLFeatureNotSupportedException is thrown if the JDBC driver does not support this method.

Example

Java




// Java program to demonstrate how to make a save point
  
import java.io.*;
import java.sql.*;
  
class GFG {
    public static void main(String[] args)
    {
        // db credentials
        String jdbcEndpoint
            = "jdbc:mysql://localhost:3000/GEEKSFORGEEKS";
        String userid = "GFG";
        String password = "GEEKSFORGEEKS";
  
        // create a connection to db
        Connection connection = DriverManager.getConnection(
            jdbcEndpoint, userid, password);
  
        // construct a query
        Statement deleteStmt = connection.createStatement();
        String deleteQuery
            = "DELETE FROM USER WHERE AGE > 15";
  
        // Disable auto commit to connection
        connection.setAutoCommit(false);
  
        /*    Table USER
       +--------+---------+------------+
       | USR_ID | NAME    | AGE        |
       +--------+---------+------------+
       |      1 | GFG_1      | 10           |
       |      2 | GFG_2   | 20         |
       |      3 | GFG_3   | 25           |
       +--------+---------+------------+
       */
  
        // Create a savepoint object before executing the
        // deleteQuery
        Savepoint beforeDeleteSavepoint
            = connection.setSavepoint();
  
        // Executing the deleteQuery
        ResultSet res
            = deleteStmt.executeQuery(deleteQuery);
  
        /*    Table USER after executing deleteQuery
       +--------+---------+------------+
       | USR_ID | NAME    | AGE        |
       +--------+---------+------------+
       |      1 | GFG_1      | 10           |
       +--------+---------+------------+
       */
  
        // Rollback to our beforeDeleteSavepoint
        connection.rollback(beforeDeleteSavepoint);
        connection.commit();
  
        /*    Table USER after rollback
        +--------+---------+------------+
        | USR_ID | NAME    | AGE        |
        +--------+---------+------------+
        |      1 | GFG_1   | 10            |
        |      2 | GFG_2   | 20          |
        |      3 | GFG_3   | 25            |
        +--------+---------+------------+
        */
    }
}


savepoint in a table

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments