Search in sources :

Example 1 with PgStatement

use of org.postgresql.jdbc.PgStatement in project openGauss-connector-jdbc by opengauss-mirror.

the class PreparedStatementTest method testSelectPrepareThreshold0AutoCommitFalseFetchSizeNonZero.

@Test
public void testSelectPrepareThreshold0AutoCommitFalseFetchSizeNonZero() throws SQLException {
    assumeBinaryModeRegular();
    Assume.assumeTrue("simple protocol only does not support prepared statement requests", preferQueryMode != PreferQueryMode.SIMPLE);
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    try {
        pstmt = con.prepareStatement("SELECT 42");
        ((PgStatement) pstmt).setPrepareThreshold(0);
        pstmt.setFetchSize(1);
        rs = pstmt.executeQuery();
        rs.next();
        assertEquals(42, rs.getInt(1));
    } finally {
        TestUtil.closeQuietly(rs);
        TestUtil.closeQuietly(pstmt);
    }
    assertFalse("prepareThreshold=0, so the statement should not be server-prepared", ((PGStatement) pstmt).isUseServerPrepare());
    assertEquals("prepareThreshold=0, so the statement should not be server-prepared", 0, getNumberOfServerPreparedStatements("SELECT 42"));
}
Also used : PgStatement(org.postgresql.jdbc.PgStatement) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) Test(org.junit.Test)

Example 2 with PgStatement

use of org.postgresql.jdbc.PgStatement in project openGauss-connector-jdbc by opengauss-mirror.

the class PreparedStatementTest method testBatchWithPrepareThreshold5.

@Test
public void testBatchWithPrepareThreshold5() throws SQLException {
    assumeBinaryModeRegular();
    Assume.assumeTrue("simple protocol only does not support prepared statement requests", preferQueryMode != PreferQueryMode.SIMPLE);
    PreparedStatement pstmt = con.prepareStatement("CREATE temp TABLE batch_tab_threshold5 (id bigint, val bigint)");
    pstmt.executeUpdate();
    pstmt.close();
    // When using a prepareThreshold of 5, a batch update should use server-side prepare
    pstmt = con.prepareStatement("INSERT INTO batch_tab_threshold5 (id, val) VALUES (?,?)");
    ((PgStatement) pstmt).setPrepareThreshold(5);
    for (int p = 0; p < 5; p++) {
        for (int i = 0; i <= 5; i++) {
            pstmt.setLong(1, i);
            pstmt.setLong(2, i);
            pstmt.addBatch();
        }
        pstmt.executeBatch();
    }
    pstmt.close();
    assertTrue("prepareThreshold=5, so the statement should be server-prepared", ((PGStatement) pstmt).isUseServerPrepare());
    assertEquals("prepareThreshold=5, so the statement should be server-prepared", 1, getNumberOfServerPreparedStatements("INSERT INTO batch_tab_threshold5 (id, val) VALUES ($1,$2)"));
}
Also used : PgStatement(org.postgresql.jdbc.PgStatement) PreparedStatement(java.sql.PreparedStatement) Test(org.junit.Test)

Example 3 with PgStatement

use of org.postgresql.jdbc.PgStatement in project pgjdbc by pgjdbc.

the class PreparedStatementTest method testBatchWithPrepareThreshold0.

@Test
public void testBatchWithPrepareThreshold0() throws SQLException {
    assumeBinaryModeRegular();
    Assume.assumeTrue("simple protocol only does not support prepared statement requests", preferQueryMode != PreferQueryMode.SIMPLE);
    PreparedStatement pstmt = con.prepareStatement("CREATE temp TABLE batch_tab_threshold0 (id bigint, val bigint)");
    pstmt.executeUpdate();
    pstmt.close();
    // When using a prepareThreshold of 0, a batch update should not use server-side prepare
    pstmt = con.prepareStatement("INSERT INTO batch_tab_threshold0 (id, val) VALUES (?,?)");
    ((PgStatement) pstmt).setPrepareThreshold(0);
    for (int p = 0; p < 5; p++) {
        for (int i = 0; i <= 5; i++) {
            pstmt.setLong(1, i);
            pstmt.setLong(2, i);
            pstmt.addBatch();
        }
        pstmt.executeBatch();
    }
    pstmt.close();
    assertFalse("prepareThreshold=0, so the statement should not be server-prepared", ((PGStatement) pstmt).isUseServerPrepare());
    assertEquals("prepareThreshold=0, so the statement should not be server-prepared", 0, getNumberOfServerPreparedStatements("INSERT INTO batch_tab_threshold0 (id, val) VALUES ($1,$2)"));
}
Also used : PgStatement(org.postgresql.jdbc.PgStatement) PreparedStatement(java.sql.PreparedStatement) Test(org.junit.Test)

Example 4 with PgStatement

use of org.postgresql.jdbc.PgStatement in project pgjdbc by pgjdbc.

the class StatementTest method testShortQueryTimeout.

/**
 * Test executes two queries one after another. The first one has timeout of 1ms, and the second
 * one does not. The timeout of the first query should not impact the second one.
 */
@Test
public void testShortQueryTimeout() throws SQLException {
    assumeLongTest();
    long deadLine = System.nanoTime() + TimeUnit.SECONDS.toNanos(10);
    Statement stmt = con.createStatement();
    ((PgStatement) stmt).setQueryTimeoutMs(1);
    Statement stmt2 = con.createStatement();
    while (System.nanoTime() < deadLine) {
        try {
            // This usually won't time out but scheduler jitter, server load
            // etc can cause a timeout.
            stmt.executeQuery("select 1;");
        } catch (SQLException e) {
            // Expect "57014 query_canceled" (en-msg is "canceling statement due to statement timeout")
            // but anything else is fatal. We can't differentiate other causes of statement cancel like
            // "canceling statement due to user request" without error message matching though, and we
            // don't want to do that.
            Assert.assertEquals("Query is expected to be cancelled via st.close(), got " + e.getMessage(), PSQLState.QUERY_CANCELED.getState(), e.getSQLState());
        }
        // Must never time out.
        stmt2.executeQuery("select 1;");
    }
}
Also used : PgStatement(org.postgresql.jdbc.PgStatement) SQLException(java.sql.SQLException) PgStatement(org.postgresql.jdbc.PgStatement) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Test(org.junit.Test)

Example 5 with PgStatement

use of org.postgresql.jdbc.PgStatement in project pgjdbc by pgjdbc.

the class PreparedStatementTest method testBatchWithPrepareThreshold5.

@Test
public void testBatchWithPrepareThreshold5() throws SQLException {
    assumeBinaryModeRegular();
    Assume.assumeTrue("simple protocol only does not support prepared statement requests", preferQueryMode != PreferQueryMode.SIMPLE);
    PreparedStatement pstmt = con.prepareStatement("CREATE temp TABLE batch_tab_threshold5 (id bigint, val bigint)");
    pstmt.executeUpdate();
    pstmt.close();
    // When using a prepareThreshold of 5, a batch update should use server-side prepare
    pstmt = con.prepareStatement("INSERT INTO batch_tab_threshold5 (id, val) VALUES (?,?)");
    ((PgStatement) pstmt).setPrepareThreshold(5);
    for (int p = 0; p < 5; p++) {
        for (int i = 0; i <= 5; i++) {
            pstmt.setLong(1, i);
            pstmt.setLong(2, i);
            pstmt.addBatch();
        }
        pstmt.executeBatch();
    }
    pstmt.close();
    assertTrue("prepareThreshold=5, so the statement should be server-prepared", ((PGStatement) pstmt).isUseServerPrepare());
    assertEquals("prepareThreshold=5, so the statement should be server-prepared", 1, getNumberOfServerPreparedStatements("INSERT INTO batch_tab_threshold5 (id, val) VALUES ($1,$2)"));
}
Also used : PgStatement(org.postgresql.jdbc.PgStatement) PreparedStatement(java.sql.PreparedStatement) Test(org.junit.Test)

Aggregations

PreparedStatement (java.sql.PreparedStatement)8 Test (org.junit.Test)8 PgStatement (org.postgresql.jdbc.PgStatement)8 ResultSet (java.sql.ResultSet)2 SQLException (java.sql.SQLException)2 Statement (java.sql.Statement)2