Example usage for java.sql ResultSet CLOSE_CURSORS_AT_COMMIT

List of usage examples for java.sql ResultSet CLOSE_CURSORS_AT_COMMIT

Introduction

In this page you can find the example usage for java.sql ResultSet CLOSE_CURSORS_AT_COMMIT.

Prototype

int CLOSE_CURSORS_AT_COMMIT

To view the source code for java.sql ResultSet CLOSE_CURSORS_AT_COMMIT.

Click Source Link

Document

The constant indicating that open ResultSet objects with this holdability will be closed when the current transaction is committed.

Usage

From source file:Main.java

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");
    }/*from   w ww  .  j a v  a  2s.c o  m*/
    conn.close();

}

From source file:com.alibaba.wasp.jdbc.TestJdbcStatement.java

@Test
public void testStatement() throws SQLException, IOException, InterruptedException {
    Statement stat = conn.createStatement();

    assertEquals(ResultSet.HOLD_CURSORS_OVER_COMMIT, conn.getHoldability());
    conn.setHoldability(ResultSet.CLOSE_CURSORS_AT_COMMIT);
    assertEquals(ResultSet.CLOSE_CURSORS_AT_COMMIT, conn.getHoldability());
    // ignored/*from   w w  w.j  ava2 s.c  o  m*/
    stat.setCursorName("x");
    // fixed return value
    assertEquals(stat.getFetchDirection(), ResultSet.FETCH_FORWARD);
    // ignored
    stat.setFetchDirection(ResultSet.FETCH_REVERSE);
    // ignored
    stat.setMaxFieldSize(100);

    assertEquals(conf.getInt(FConstants.WASP_JDBC_FETCHSIZE, FConstants.DEFAULT_WASP_JDBC_FETCHSIZE),
            stat.getFetchSize());
    stat.setFetchSize(10);
    assertEquals(10, stat.getFetchSize());
    stat.setFetchSize(0);
    assertEquals(conf.getInt(FConstants.WASP_JDBC_FETCHSIZE, FConstants.DEFAULT_WASP_JDBC_FETCHSIZE),
            stat.getFetchSize());
    assertEquals(ResultSet.TYPE_FORWARD_ONLY, stat.getResultSetType());
    Statement stat2 = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY,
            ResultSet.HOLD_CURSORS_OVER_COMMIT);
    assertEquals(ResultSet.TYPE_SCROLL_SENSITIVE, stat2.getResultSetType());
    assertEquals(ResultSet.HOLD_CURSORS_OVER_COMMIT, stat2.getResultSetHoldability());
    assertEquals(ResultSet.CONCUR_READ_ONLY, stat2.getResultSetConcurrency());
    assertEquals(0, stat.getMaxFieldSize());
    assertTrue(!((JdbcStatement) stat2).isClosed());
    stat2.close();
    assertTrue(((JdbcStatement) stat2).isClosed());

    ResultSet rs;
    int count;
    boolean result;

    stat.execute("CREATE TABLE TEST {REQUIRED INT64 ID;" + "REQUIRED STRING VALUE; }PRIMARY KEY(ID), "
            + "ENTITY GROUP ROOT,ENTITY GROUP KEY(ID);");

    TEST_UTIL.waitTableEnabled(Bytes.toBytes("TEST"), 5000);

    ResultInHBasePrinter.printMETA(conf, LOG);
    ResultInHBasePrinter.printFMETA(conf, LOG);
    ResultInHBasePrinter.printTable("test", "WASP_ENTITY_TEST", conf, LOG);

    conn.getTypeMap();

    // this method should not throw an exception - if not supported, this
    // calls are ignored

    assertEquals(ResultSet.CONCUR_READ_ONLY, stat.getResultSetConcurrency());

    // stat.cancel();
    stat.setQueryTimeout(10);
    assertTrue(stat.getQueryTimeout() == 10);
    stat.setQueryTimeout(0);
    assertTrue(stat.getQueryTimeout() == 0);
    // assertThrows(SQLErrorCode.INVALID_VALUE_2, stat).setQueryTimeout(-1);
    assertTrue(stat.getQueryTimeout() == 0);
    trace("executeUpdate");
    count = stat.executeUpdate("INSERT INTO TEST (ID,VALUE) VALUES (1,'Hello')");
    assertEquals(1, count);
    count = stat.executeUpdate("INSERT INTO TEST (VALUE,ID) VALUES ('JDBC',2)");
    assertEquals(1, count);
    count = stat.executeUpdate("UPDATE TEST SET VALUE='LDBC' WHERE ID=1");
    assertEquals(1, count);

    count = stat.executeUpdate("DELETE FROM TEST WHERE ID=-1");
    assertEquals(0, count);
    count = stat.executeUpdate("DELETE FROM TEST WHERE ID=1");
    assertEquals(1, count);
    count = stat.executeUpdate("DELETE FROM TEST WHERE ID=2");
    assertEquals(1, count);

    result = stat.execute("INSERT INTO TEST(ID,VALUE) VALUES(1,'Hello')");
    assertTrue(!result);
    result = stat.execute("INSERT INTO TEST(VALUE,ID) VALUES('JDBC',2)");
    assertTrue(!result);
    result = stat.execute("UPDATE TEST SET VALUE='LDBC' WHERE ID=2");
    assertTrue(!result);
    result = stat.execute("DELETE FROM TEST WHERE ID=1");
    assertTrue(!result);
    result = stat.execute("DELETE FROM TEST WHERE ID=2");
    assertTrue(!result);
    result = stat.execute("DELETE FROM TEST WHERE ID=3");
    assertTrue(!result);

    // getMoreResults
    rs = stat.executeQuery("SELECT ID,VALUE FROM TEST WHERE ID=1");
    assertFalse(stat.getMoreResults());
    assertThrows(SQLErrorCode.OBJECT_CLOSED, rs).next();
    assertTrue(stat.getUpdateCount() == -1);
    count = stat.executeUpdate("DELETE FROM TEST WHERE ID=1");
    assertFalse(stat.getMoreResults());
    assertTrue(stat.getUpdateCount() == -1);

    WaspAdmin admin = new WaspAdmin(TEST_UTIL.getConfiguration());
    admin.disableTable("TEST");
    stat.execute("DROP TABLE TEST");
    admin.waitTableNotLocked("TEST".getBytes());
    stat.executeUpdate("DROP TABLE IF EXISTS TEST");

    assertTrue(stat.getWarnings() == null);
    stat.clearWarnings();
    assertTrue(stat.getWarnings() == null);
    assertTrue(conn == stat.getConnection());

    admin.close();
    stat.close();
}

From source file:com.oracle.tutorial.jdbc.JDBCTutorialUtilities.java

public static void cursorHoldabilitySupport(Connection conn) throws SQLException {
    DatabaseMetaData dbMetaData = conn.getMetaData();
    System.out.println("ResultSet.HOLD_CURSORS_OVER_COMMIT = " + ResultSet.HOLD_CURSORS_OVER_COMMIT);
    System.out.println("ResultSet.CLOSE_CURSORS_AT_COMMIT = " + ResultSet.CLOSE_CURSORS_AT_COMMIT);
    System.out.println("Default cursor holdability: " + dbMetaData.getResultSetHoldability());
    System.out.println("Supports HOLD_CURSORS_OVER_COMMIT? "
            + dbMetaData.supportsResultSetHoldability(ResultSet.HOLD_CURSORS_OVER_COMMIT));
    System.out.println("Supports CLOSE_CURSORS_AT_COMMIT? "
            + dbMetaData.supportsResultSetHoldability(ResultSet.CLOSE_CURSORS_AT_COMMIT));
}

From source file:com.github.adejanovski.cassandra.jdbc.CassandraStatement.java

CassandraStatement(CassandraConnection con, String cql, int resultSetType, int resultSetConcurrency,
        int resultSetHoldability) throws SQLException {
    this.connection = con;
    this.cql = cql;
    this.batchQueries = Lists.newArrayList();

    this.consistencyLevel = con.defaultConsistencyLevel;

    if (!(resultSetType == ResultSet.TYPE_FORWARD_ONLY || resultSetType == ResultSet.TYPE_SCROLL_INSENSITIVE
            || resultSetType == ResultSet.TYPE_SCROLL_SENSITIVE))
        throw new SQLSyntaxErrorException(BAD_TYPE_RSET);
    this.resultSetType = resultSetType;

    if (!(resultSetConcurrency == ResultSet.CONCUR_READ_ONLY
            || resultSetConcurrency == ResultSet.CONCUR_UPDATABLE))
        throw new SQLSyntaxErrorException(BAD_TYPE_RSET);
    this.resultSetConcurrency = resultSetConcurrency;

    if (!(resultSetHoldability == ResultSet.HOLD_CURSORS_OVER_COMMIT
            || resultSetHoldability == ResultSet.CLOSE_CURSORS_AT_COMMIT))
        throw new SQLSyntaxErrorException(BAD_HOLD_RSET);
    this.resultSetHoldability = resultSetHoldability;
}

From source file:net.solarnetwork.node.dao.jdbc.AbstractBatchableJdbcDao.java

private BatchResult batchProcessInternal(final BatchCallback<T> callback, final BatchOptions options) {
    final String querySql = getBatchJdbcStatement(options);
    final AtomicInteger rowCount = new AtomicInteger(0);
    getJdbcTemplate().execute(new ConnectionCallback<Object>() {

        @Override/*from w w w.  ja v a 2s. com*/
        public net.solarnetwork.node.dao.BatchableDao.BatchResult doInConnection(Connection con)
                throws SQLException, DataAccessException {
            PreparedStatement queryStmt = null;
            ResultSet queryResult = null;
            try {
                queryStmt = con.prepareStatement(querySql,
                        (options.isUpdatable() ? ResultSet.TYPE_SCROLL_SENSITIVE : ResultSet.TYPE_FORWARD_ONLY),
                        (options.isUpdatable() ? ResultSet.CONCUR_UPDATABLE : ResultSet.CONCUR_READ_ONLY),
                        ResultSet.CLOSE_CURSORS_AT_COMMIT);
                queryResult = queryStmt.executeQuery();
                while (queryResult.next()) {
                    T entity = getBatchRowEntity(options, queryResult, rowCount.incrementAndGet());
                    BatchCallbackResult rowResult = callback.handle(entity);
                    switch (rowResult) {
                    case CONTINUE:
                        break;
                    case STOP:
                        return null;
                    case DELETE:
                        queryResult.deleteRow();
                        break;
                    case UPDATE:
                    case UPDATE_STOP:
                        updateBatchRowEntity(options, queryResult, rowCount.intValue(), entity);
                        queryResult.updateRow();
                        if (rowResult == BatchCallbackResult.UPDATE_STOP) {
                            return null;
                        }
                        break;
                    }
                }
            } finally {
                if (queryResult != null) {
                    queryResult.close();
                }
                if (queryStmt != null) {
                    queryStmt.close();
                }
            }

            return null;
        }
    });
    return new BasicBatchResult(rowCount.intValue());
}

From source file:com.adaptris.jdbc.connection.FailoverDatasourceTest.java

@Test
public void testStatements() throws Exception {
    Connection conn = new MyProxy();
    try {/*from   www  . ja  v  a  2 s  .  co  m*/
        try {
            conn.nativeSQL("SELECT * FROM SEQUENCES");
        } catch (Exception e) {

        }
        try {
            conn.createStatement();
        } catch (Exception e) {

        }
        try {
            conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE);
        } catch (Exception e) {

        }
        try {
            conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE);
        } catch (Exception e) {

        }
        try {
            conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE,
                    ResultSet.CLOSE_CURSORS_AT_COMMIT);
        } catch (Exception e) {

        }
        try {
            conn.prepareStatement("SELECT * FROM SEQUENCES");
        } catch (Exception e) {

        }
        try {
            conn.prepareStatement("SELECT * FROM SEQUENCES", ResultSet.TYPE_FORWARD_ONLY,
                    ResultSet.CONCUR_READ_ONLY);
        } catch (Exception e) {

        }
        try {
            conn.prepareStatement("SELECT * FROM SEQUENCES", ResultSet.TYPE_FORWARD_ONLY,
                    ResultSet.CONCUR_READ_ONLY, ResultSet.CLOSE_CURSORS_AT_COMMIT);
        } catch (Exception e) {

        }
        try {
            conn.prepareStatement("INSERT INTO sequences (id, seq_number) values ('id', 2)",
                    Statement.NO_GENERATED_KEYS);
        } catch (Exception e) {
        }
        try {
            conn.prepareStatement("INSERT INTO sequences (id, seq_number) values ('id', 2)", new int[0]);
        } catch (Exception e) {
        }
        try {
            conn.prepareStatement("INSERT INTO sequences (id, seq_number) values ('id', 2)", new String[0]);
        } catch (Exception e) {

        }

        try {
            conn.prepareCall("SELECT * FROM SEQUENCES");
        } catch (Exception e) {

        }
        try {
            conn.prepareCall("SELECT * FROM SEQUENCES", ResultSet.TYPE_FORWARD_ONLY,
                    ResultSet.CONCUR_READ_ONLY);
        } catch (Exception e) {

        }
        try {
            conn.prepareCall("SELECT * FROM SEQUENCES", ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY,
                    ResultSet.CLOSE_CURSORS_AT_COMMIT);
        } catch (Exception e) {

        }
    } finally {
        JdbcUtil.closeQuietly(conn);

    }
}

From source file:net.solarnetwork.node.dao.jdbc.JdbcSettingDao.java

private void storeSettingInternal(final String key, final String ttype, final String value, final int flags) {
    final String type = (ttype == null ? "" : ttype);
    final Timestamp now = new Timestamp(System.currentTimeMillis());
    // to avoid bumping modified date column when values haven't changed, we are careful here
    // to compare before actually updating
    getJdbcTemplate().query(new PreparedStatementCreator() {

        @Override//from  w w  w .  java 2s. c  o  m
        public PreparedStatement createPreparedStatement(Connection con) throws SQLException {
            PreparedStatement queryStmt = con.prepareStatement(sqlGet, ResultSet.TYPE_SCROLL_SENSITIVE,
                    ResultSet.CONCUR_UPDATABLE, ResultSet.CLOSE_CURSORS_AT_COMMIT);
            queryStmt.setString(1, key);
            queryStmt.setString(2, type);
            return queryStmt;
        }
    }, new ResultSetExtractor<Object>() {

        @Override
        public Object extractData(ResultSet rs) throws SQLException, DataAccessException {
            if (rs.next()) {
                String oldValue = rs.getString(1);
                if (!value.equals(oldValue)) {
                    rs.updateString(1, value);
                    rs.updateTimestamp(2, now);
                    rs.updateRow();
                }
            } else {
                rs.moveToInsertRow();
                rs.updateString(1, value);
                rs.updateTimestamp(2, now);
                rs.updateString(3, key);
                rs.updateString(4, type);
                rs.updateInt(5, flags);
                rs.insertRow();
            }
            return null;
        }
    });
}

From source file:org.pentaho.di.trans.dataservice.jdbc.ThinConnection.java

@Override
public int getHoldability() throws SQLException {
    return ResultSet.CLOSE_CURSORS_AT_COMMIT;
}

From source file:com.taobao.tddl.jdbc.group.TGroupConnection.java

public int getHoldability() throws SQLException {
    return ResultSet.CLOSE_CURSORS_AT_COMMIT;
}

From source file:com.alibaba.wasp.jdbc.JdbcConnection.java

private void checkHoldability(int resultSetHoldability) {
    if (resultSetHoldability != ResultSet.HOLD_CURSORS_OVER_COMMIT
            && resultSetHoldability != ResultSet.CLOSE_CURSORS_AT_COMMIT) {
        throw JdbcException.getInvalidValueException("resultSetHoldability", resultSetHoldability);
    }//from ww  w. jav a 2s .c om
}