Java Connection setHoldability Method with Example



ResultSet holdability determines whether the ResultSet objects (cursors) should be closed or held open when a transaction (that contains the said cursor/ ResultSet object) is committed using the commit() method of the Connection interface.

The setHoldability() method of the Connection interface is used to set the holdability of the ResultSet objects in this connection (created using this connection) to a desired value.

Syntax

Following is the syntax of the Java Connection setHoldability() method ?

void setHoldability(int holdability) throws SQLException;

Parameters

This method accepts an integer value named "holdability", which is described below ?

  • Representing the ResultSet holdability value you want to set.

The ResultSet interface provides two values to specify the holdability of a ResultSet namely ?

  • CLOSE_CURSORS_AT_COMMIT ? If the holdability of the ResultSet object is set to this value. Whenever you commit/save a transaction using the commit() method of the Connection interface, the ResultSet objects created in the current transaction (that are already opened) will be closed.
  • HOLD_CURSORS_OVER_COMMIT ? If the holdability of the ResultSet object is set to this value. Whenever you commit/save a transaction using the commit() method of the Connection interface, the ResultSet objects created in the current transaction (that are already opened) will be held open

To change or set the holdability to a desired value, follow the steps below ?

Step 1: Register the driver using the registerDriver() method of the DriverManager class as ?

//Registering the Driver
DriverManager.registerDriver(new com.mysql.jdbc.Driver());

Step 2: Get the connection using the getConnection() method of the DriverManager class as ?

//Getting the connection
String url = "jdbc:mysql://localhost/mydatabase";
Connection con = DriverManager.getConnection(url, "root", "password");

Step 3: Set the ResultSet holdability to the required value using the setHoldability() method of the Connection interface as ?

con.setHoldability(ResultSet.CLOSE_CURSORS_AT_COMMIT);

Example

The following JDBC program establishes connection with the database and sets the holdability value to CLOSE_CURSORS_AT_COMMIT ?

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
public class Connection_setHoldability {
   public static void main(String args[]) throws SQLException {
      //Registering the Driver
      DriverManager.registerDriver(new com.mysql.jdbc.Driver());
      //Getting the connection
      String url = "jdbc:mysql://localhost/mydatabase";
      Connection con = DriverManager.getConnection(url, "root", "password");
      System.out.println("Connection established......");
      //Setting the auto commit false
      con.setAutoCommit(false);
      //Setting the holdability to CLOSE_CURSORS_AT_COMMIT
      con.setHoldability(ResultSet.CLOSE_CURSORS_AT_COMMIT);
      System.out.println("ResultSet holdability value has been changed to "+con.getHoldability());
   }
}

Output

The above program displayed the following output ?

Connection established......
ResultSet object is open
Revathi Satya Kondra
Revathi Satya Kondra

Technical Content Writer, Tutorialspoint

Updated on: 2024-12-30T19:20:39+05:30

425 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements