Example usage for java.sql Statement getFetchSize

List of usage examples for java.sql Statement getFetchSize

Introduction

In this page you can find the example usage for java.sql Statement getFetchSize.

Prototype

int getFetchSize() throws SQLException;

Source Link

Document

Retrieves the number of result set rows that is the default fetch size for ResultSet objects generated from this Statement object.

Usage

From source file:org.apache.hive.jdbc.TestJdbcWithMiniHS2.java

@Test
public void testFetchSize() throws Exception {
    // Test setting fetch size below max
    Connection fsConn = getConnection(miniHS2.getJdbcURL("default", "fetchSize=50", ""),
            System.getProperty("user.name"), "bar");
    Statement stmt = fsConn.createStatement();
    stmt.execute("set hive.server2.thrift.resultset.serialize.in.tasks=true");
    int fetchSize = stmt.getFetchSize();
    assertEquals(50, fetchSize);//ww  w.  java2  s  .  c  o m
    stmt.close();
    fsConn.close();
    // Test setting fetch size above max
    fsConn = getConnection(miniHS2.getJdbcURL("default",
            "fetchSize=" + (miniHS2.getHiveConf()
                    .getIntVar(HiveConf.ConfVars.HIVE_SERVER2_THRIFT_RESULTSET_MAX_FETCH_SIZE) + 1),
            ""), System.getProperty("user.name"), "bar");
    stmt = fsConn.createStatement();
    stmt.execute("set hive.server2.thrift.resultset.serialize.in.tasks=true");
    fetchSize = stmt.getFetchSize();
    assertEquals(
            miniHS2.getHiveConf().getIntVar(HiveConf.ConfVars.HIVE_SERVER2_THRIFT_RESULTSET_MAX_FETCH_SIZE),
            fetchSize);
    stmt.close();
    fsConn.close();
}

From source file:org.apache.metamodel.jdbc.JdbcDataContextTest.java

public void testMaxRows() throws Exception {
    final Connection realCon = getTestDbConnection();
    final Statement realStatement = realCon.createStatement();

    final Connection mockCon = EasyMock.createMock(Connection.class);
    final Statement mockStatement = EasyMock.createMock(Statement.class);

    EasyMock.expect(mockCon.getMetaData()).andReturn(realCon.getMetaData()).anyTimes();

    EasyMock.expect(mockCon.getAutoCommit()).andReturn(true);

    EasyMock.expect(mockCon.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY))
            .andReturn(mockStatement);/*from   w w w . j  a v  a  2 s  . co  m*/

    EasyMock.expect(mockStatement.getFetchSize()).andReturn(10);
    mockStatement.setFetchSize(EasyMock.anyInt());
    mockStatement.setMaxRows(3);
    EasyMock.expectLastCall().andThrow(new SQLException("I wont allow max rows"));

    EasyMock.expect(mockStatement.executeQuery(
            "SELECT a.\"CUSTOMERNUMBER\", a.\"CUSTOMERNAME\", a.\"CONTACTLASTNAME\", a.\"CONTACTFIRSTNAME\", "
                    + "a.\"PHONE\", a.\"ADDRESSLINE1\", a.\"ADDRESSLINE2\", a.\"CITY\", a.\"STATE\", "
                    + "a.\"POSTALCODE\", a.\"COUNTRY\", a.\"SALESREPEMPLOYEENUMBER\", "
                    + "a.\"CREDITLIMIT\" FROM PUBLIC.\"CUSTOMERS\" a"))
            .andReturn(realStatement
                    .executeQuery("SELECT a.\"CUSTOMERNUMBER\", a.\"CUSTOMERNAME\", a.\"CONTACTLASTNAME\", "
                            + "a.\"CONTACTFIRSTNAME\", a.\"PHONE\", a.\"ADDRESSLINE1\", a.\"ADDRESSLINE2\", a.\"CITY\", "
                            + "a.\"STATE\", a.\"POSTALCODE\", a.\"COUNTRY\", a.\"SALESREPEMPLOYEENUMBER\", "
                            + "a.\"CREDITLIMIT\" FROM PUBLIC.\"CUSTOMERS\" a"));

    mockStatement.close();

    EasyMock.replay(mockCon, mockStatement);

    JdbcDataContext dc = new JdbcDataContext(mockCon, new TableType[] { TableType.TABLE, TableType.VIEW },
            null);
    dc.setQueryRewriter(new DefaultQueryRewriter(dc));
    Schema schema = dc.getDefaultSchema();

    Query q = new Query().setMaxRows(3);
    Table table = schema.getTables()[0];
    q.from(table, "a");
    q.select(table.getColumns());
    assertEquals(
            "SELECT a.\"CUSTOMERNUMBER\", a.\"CUSTOMERNAME\", a.\"CONTACTLASTNAME\", a.\"CONTACTFIRSTNAME\", "
                    + "a.\"PHONE\", a.\"ADDRESSLINE1\", a.\"ADDRESSLINE2\", a.\"CITY\", a.\"STATE\", a.\"POSTALCODE\", "
                    + "a.\"COUNTRY\", a.\"SALESREPEMPLOYEENUMBER\", a.\"CREDITLIMIT\" FROM PUBLIC.\"CUSTOMERS\" a",
            q.toString());
    DataSet result = dc.executeQuery(q);
    assertTrue(result.next());
    assertEquals(
            "Row[values=[103, Atelier graphique, Schmitt, Carine, 40.32.2555, 54, rue Royale, null, Nantes, null, "
                    + "44000, France, 1370, 21000.0]]",
            result.getRow().toString());
    assertTrue(result.next());
    assertTrue(result.next());
    assertFalse(result.next());

    result.close();

    EasyMock.verify(mockCon, mockStatement);
    realStatement.close();
}

From source file:org.kuali.rice.kew.docsearch.dao.impl.DocumentSearchDAOJdbcImpl.java

@Override
public DocumentSearchResults.Builder findDocuments(final DocumentSearchGenerator documentSearchGenerator,
        final DocumentSearchCriteria criteria, final boolean criteriaModified,
        final List<RemotableAttributeField> searchFields) {
    final int maxResultCap = getMaxResultCap(criteria);
    try {//from  w ww .  ja v a2 s . co  m
        final JdbcTemplate template = new JdbcTemplate(dataSource);

        return template.execute(new ConnectionCallback<DocumentSearchResults.Builder>() {
            @Override
            public DocumentSearchResults.Builder doInConnection(final Connection con) throws SQLException {
                final Statement statement = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
                        ResultSet.CONCUR_READ_ONLY);
                try {
                    final int fetchIterationLimit = getFetchMoreIterationLimit();
                    final int fetchLimit = fetchIterationLimit * maxResultCap;
                    statement.setFetchSize(maxResultCap + 1);
                    statement.setMaxRows(fetchLimit + 1);

                    PerformanceLogger perfLog = new PerformanceLogger();
                    String sql = documentSearchGenerator.generateSearchSql(criteria, searchFields);
                    perfLog.log("Time to generate search sql from documentSearchGenerator class: "
                            + documentSearchGenerator.getClass().getName(), true);
                    LOG.info("Executing document search with statement max rows: " + statement.getMaxRows());
                    LOG.info(
                            "Executing document search with statement fetch size: " + statement.getFetchSize());
                    perfLog = new PerformanceLogger();
                    final ResultSet rs = statement.executeQuery(sql);
                    try {
                        perfLog.log("Time to execute doc search database query.", true);
                        final Statement searchAttributeStatement = con
                                .createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
                        try {
                            return documentSearchGenerator.processResultSet(criteria, criteriaModified,
                                    searchAttributeStatement, rs, maxResultCap, fetchLimit);
                        } finally {
                            try {
                                searchAttributeStatement.close();
                            } catch (SQLException e) {
                                LOG.warn("Could not close search attribute statement.");
                            }
                        }
                    } finally {
                        try {
                            rs.close();
                        } catch (SQLException e) {
                            LOG.warn("Could not close result set.");
                        }
                    }
                } finally {
                    try {
                        statement.close();
                    } catch (SQLException e) {
                        LOG.warn("Could not close statement.");
                    }
                }
            }
        });

    } catch (DataAccessException dae) {
        String errorMsg = "DataAccessException: " + dae.getMessage();
        LOG.error("getList() " + errorMsg, dae);
        throw new RuntimeException(errorMsg, dae);
    } catch (Exception e) {
        String errorMsg = "LookupException: " + e.getMessage();
        LOG.error("getList() " + errorMsg, e);
        throw new RuntimeException(errorMsg, e);
    }
}