Skip to content

Commit

Permalink
SNOW-977206 Fix testPreparedStatement, fix resource management in Log…
Browse files Browse the repository at this point in the history
…icalConnectionLatestIT (#1569)
  • Loading branch information
sfc-gh-pfus authored Nov 28, 2023
1 parent ca8ddbe commit 4db5434
Showing 1 changed file with 102 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package net.snowflake.client.pooling;

import static org.junit.Assert.*;
import static org.junit.Assert.assertTrue;

import java.sql.*;
import java.util.Collections;
Expand All @@ -28,16 +27,17 @@ public void testLogicalConnection() throws SQLException {
poolDataSource = setProperties(poolDataSource);
PooledConnection pooledConnection = poolDataSource.getPooledConnection();
Connection logicalConnection = pooledConnection.getConnection();
Statement statement =
try (Statement statement =
logicalConnection.createStatement(
ResultSet.TYPE_FORWARD_ONLY,
ResultSet.CONCUR_READ_ONLY,
ResultSet.CLOSE_CURSORS_AT_COMMIT);
ResultSet resultSet = statement.executeQuery("show parameters");
assertTrue(resultSet.next());
assertFalse(logicalConnection.isClosed());
assertEquals(ResultSet.CLOSE_CURSORS_AT_COMMIT, logicalConnection.getHoldability());
statement.close();
ResultSet.CLOSE_CURSORS_AT_COMMIT)) {
try (ResultSet resultSet = statement.executeQuery("show parameters")) {
assertTrue(resultSet.next());
assertFalse(logicalConnection.isClosed());
assertEquals(ResultSet.CLOSE_CURSORS_AT_COMMIT, logicalConnection.getHoldability());
}
}
logicalConnection.close();
assertTrue(logicalConnection.isClosed());
pooledConnection.close();
Expand Down Expand Up @@ -154,27 +154,32 @@ public void testTransactionStatement() throws SQLException {
logicalConnection.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
assertEquals(2, logicalConnection.getTransactionIsolation());

Statement statement = logicalConnection.createStatement();
statement.executeUpdate("create or replace table test_transaction (colA int, colB string)");
// start a transaction
statement.executeUpdate("insert into test_transaction values (1, 'abc')");

// commit
logicalConnection.commit();
ResultSet resultSet = statement.executeQuery("select count(*) from test_transaction");
assertTrue(resultSet.next());
assertEquals(1, resultSet.getInt(1));
resultSet.close();

// rollback
statement.executeUpdate("delete from test_transaction");
logicalConnection.rollback();
resultSet = statement.executeQuery("select count(*) from test_transaction");
assertTrue(resultSet.next());
assertEquals(1, resultSet.getInt(1));
try (Statement statement = logicalConnection.createStatement()) {
statement.executeUpdate("create or replace table test_transaction (colA int, colB string)");
// start a transaction
statement.executeUpdate("insert into test_transaction values (1, 'abc')");

// commit
logicalConnection.commit();
try (ResultSet resultSet =
statement.executeQuery("select count(*) from test_transaction")) {
assertTrue(resultSet.next());
assertEquals(1, resultSet.getInt(1));
}

statement.execute("drop table if exists test_transaction");
resultSet.close();
// rollback
statement.executeUpdate("delete from test_transaction");
logicalConnection.rollback();
try (ResultSet resultSet =
statement.executeQuery("select count(*) from test_transaction")) {
assertTrue(resultSet.next());
assertEquals(1, resultSet.getInt(1));
}
} finally {
try (Statement statement = logicalConnection.createStatement()) {
statement.execute("drop table if exists test_transaction");
}
}
}
pooledConnection.close();
}
Expand Down Expand Up @@ -212,20 +217,20 @@ public void testPreparedStatement() throws SQLException {
PooledConnection pooledConnection = poolDataSource.getPooledConnection();
try (Connection logicalConnection = pooledConnection.getConnection()) {
try (Statement statement = logicalConnection.createStatement()) {
statement.execute("use database " + logicalConnection.getCatalog());
statement.execute("use schema " + logicalConnection.getSchema());
statement.execute("create or replace table test_prep (colA int, colB varchar)");
try (PreparedStatement preparedStatement =
logicalConnection.prepareStatement("insert into test_prep values (?, ?)")) {
preparedStatement.setInt(1, 25);
preparedStatement.setString(2, "hello world");
preparedStatement.execute();
ResultSet resultSet = statement.executeQuery("select * from test_prep");
int count = 0;
while (resultSet.next()) {
count++;
try (ResultSet resultSet = statement.executeQuery("select * from test_prep")) {
while (resultSet.next()) {
count++;
}
}
assertEquals(1, count);
} finally {
statement.execute("drop table if exists test_prep");
}
}
Expand All @@ -241,15 +246,20 @@ public void testSetSchema() throws SQLException {
try (Connection logicalConnection = pooledConnection.getConnection()) {
String schema = logicalConnection.getSchema();
// get the current schema
ResultSet rst = logicalConnection.createStatement().executeQuery("select current_schema()");
assertTrue(rst.next());
assertEquals(schema, rst.getString(1));
try (ResultSet rst =
logicalConnection.createStatement().executeQuery("select current_schema()")) {
assertTrue(rst.next());
assertEquals(schema, rst.getString(1));
}

logicalConnection.setSchema("PUBLIC");

// get the current schema
rst = logicalConnection.createStatement().executeQuery("select current_schema()");
assertTrue(rst.next());
assertEquals("PUBLIC", rst.getString(1));
try (ResultSet rst =
logicalConnection.createStatement().executeQuery("select current_schema()")) {
assertTrue(rst.next());
assertEquals("PUBLIC", rst.getString(1));
}
}
pooledConnection.close();
}
Expand All @@ -268,40 +278,44 @@ public void testPrepareCall() throws SQLException {
poolDataSource = setProperties(poolDataSource);
PooledConnection pooledConnection = poolDataSource.getPooledConnection();
try (Connection logicalConnection = pooledConnection.getConnection()) {
Statement statement = logicalConnection.createStatement();
statement.execute(procedure);

CallableStatement callableStatement = logicalConnection.prepareCall("call output_message(?)");
callableStatement.setString(1, "hello world");
ResultSet resultSet = callableStatement.executeQuery();
resultSet.next();
assertEquals("hello world", resultSet.getString(1));

callableStatement =
logicalConnection.prepareCall(
"call output_message('hello world')",
ResultSet.TYPE_FORWARD_ONLY,
ResultSet.CONCUR_READ_ONLY);
resultSet = callableStatement.executeQuery();
resultSet.next();
assertEquals("hello world", resultSet.getString(1));
assertEquals(1003, callableStatement.getResultSetType());
assertEquals(1007, callableStatement.getResultSetConcurrency());
try (Statement statement = logicalConnection.createStatement()) {
statement.execute(procedure);

try (CallableStatement callableStatement =
logicalConnection.prepareCall("call output_message(?)")) {
callableStatement.setString(1, "hello world");
try (ResultSet resultSet = callableStatement.executeQuery()) {
resultSet.next();
assertEquals("hello world", resultSet.getString(1));
}
}

callableStatement =
logicalConnection.prepareCall(
"call output_message('hello world')",
ResultSet.TYPE_FORWARD_ONLY,
ResultSet.CONCUR_READ_ONLY,
ResultSet.CLOSE_CURSORS_AT_COMMIT);
resultSet = callableStatement.executeQuery();
resultSet.next();
assertEquals(2, callableStatement.getResultSetHoldability());
try (CallableStatement callableStatement =
logicalConnection.prepareCall(
"call output_message('hello world')",
ResultSet.TYPE_FORWARD_ONLY,
ResultSet.CONCUR_READ_ONLY)) {
try (ResultSet resultSet = callableStatement.executeQuery()) {
resultSet.next();
assertEquals("hello world", resultSet.getString(1));
assertEquals(1003, callableStatement.getResultSetType());
assertEquals(1007, callableStatement.getResultSetConcurrency());
}
}

statement.execute("drop procedure if exists output_message(varchar)");
statement.close();
resultSet.close();
callableStatement.close();
try (CallableStatement callableStatement =
logicalConnection.prepareCall(
"call output_message('hello world')",
ResultSet.TYPE_FORWARD_ONLY,
ResultSet.CONCUR_READ_ONLY,
ResultSet.CLOSE_CURSORS_AT_COMMIT)) {
try (ResultSet resultSet = callableStatement.executeQuery()) {
resultSet.next();
assertEquals(2, callableStatement.getResultSetHoldability());
}
}
statement.execute("drop procedure if exists output_message(varchar)");
}
}
}

Expand All @@ -311,20 +325,25 @@ public void testClob() throws SQLException {
poolDataSource = setProperties(poolDataSource);
PooledConnection pooledConnection = poolDataSource.getPooledConnection();
try (Connection logicalConnection = pooledConnection.getConnection()) {
Statement statement = logicalConnection.createStatement();
statement.execute("create or replace table test_clob (colA text)");
try (Statement statement = logicalConnection.createStatement()) {
statement.execute("create or replace table test_clob (colA text)");
}

Clob clob = logicalConnection.createClob();
clob.setString(1, "hello world");
PreparedStatement preparedStatement =
logicalConnection.prepareStatement("insert into test_clob values (?)");
preparedStatement.setClob(1, clob);
preparedStatement.execute();
try (PreparedStatement preparedStatement =
logicalConnection.prepareStatement("insert into test_clob values (?)")) {
Clob clob = logicalConnection.createClob();
clob.setString(1, "hello world");
preparedStatement.setClob(1, clob);
preparedStatement.execute();
}

statement.execute("select * from test_clob");
ResultSet resultSet = statement.getResultSet();
resultSet.next();
assertEquals("hello world", resultSet.getString("COLA"));
try (Statement statement = logicalConnection.createStatement()) {
statement.execute("select * from test_clob");
try (ResultSet resultSet = statement.getResultSet()) {
resultSet.next();
assertEquals("hello world", resultSet.getString("COLA"));
}
}
}
}

Expand Down

0 comments on commit 4db5434

Please sign in to comment.