Java Connection getHoldability() 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 getHoldability() method of the Connection interface is used to retrieves and returns the current holdability value of the ResultSet objects in this connection.

This method returns an integer value representing the current ResultSet holdability which will be either 1 or 2 where,

  • 1 indicates the value 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.

  • 2 indicates the value 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.

To retrieve the current holdability value −

Register the driver using the registerDriver() method of the DriverManager class as −

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

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");

Get the current ResultSet holdability value using the getHoldability() method of the Connection interface as −

con.getHoldability();

Following JDBC program establishes connection with the database and retrieves the current holdability value.

Example

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
public class Connection_getHoldability {
   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("HOLD_CURSORS_OVER_COMMIT: "+ResultSet.HOLD_CURSORS_OVER_COMMIT);
      System.out.println("CLOSE_CURSORS_AT_COMMIT: "+ResultSet.CLOSE_CURSORS_AT_COMMIT);
      System.out.println("Current ResultSet holdability value is: "+con.getHoldability());
   }
}

Output

Connection established......
HOLD_CURSORS_OVER_COMMIT: 1
CLOSE_CURSORS_AT_COMMIT: 2
Current ResultSet holdability value is: 2

Updated on: 30-Jul-2019

161 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements