Tutorialspoint

1 Answer
Rishi Raj

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.

Parameters

This method accepts an integer value 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/set the holdability to a desired 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");

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

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

Connection established......
ResultSet object is open

Advertisements

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.