Java Tutorial/Database/ResultSet Holdability

Материал из Java эксперт
Перейти к: навигация, поиск

Check ResultSet Holdability

You can get the result set holdability in three ways.

  1. int Statement.getResultSetHoldability()
  2. int DatabaseMetaData.getResultSetHoldability()
  3. int Connection.getHoldability()



import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
public class Main {
  public static void main(String[] args) throws Exception {
    Connection conn = getConnection();
    DatabaseMetaData dbMeta = conn.getMetaData();
    if (dbMeta.supportsResultSetHoldability(ResultSet.HOLD_CURSORS_OVER_COMMIT)) {
      System.out.println("this database hold cursors over commit");
    } else if (dbMeta.supportsResultSetHoldability(ResultSet.CLOSE_CURSORS_AT_COMMIT)) {
      System.out.println("this database close cursors at commit");
    }
    conn.close();
  }
  private static Connection getConnection() throws Exception {
    Class.forName("org.hsqldb.jdbcDriver");
    String url = "jdbc:hsqldb:mem:data/tutorial";
    return DriverManager.getConnection(url, "sa", "");
  }
}



this database hold cursors over commit


ResultSet Holdability

You may improve database performance by including the ResultSet holdability:

  1. java.sql.ResultSet.HOLD_CURSORS_OVER_COMMIT
  2. java.sql.ResultSet.CLOSE_CURSORS_AT_COMMIT

Set ResultSet Holdability Using the Connection Object



int holdability = java.sql.ResultSet.HOLD_CURSORS_OVER_COMMIT;
conn.setHoldability(holdability);