Skip to content

Commit

Permalink
SNOW-1213117: Wrap connection, statement and result set in try with r…
Browse files Browse the repository at this point in the history
…esources(1/4) (#1720)
  • Loading branch information
sfc-gh-ext-simba-jy authored Apr 24, 2024
1 parent d96641e commit a8a3006
Show file tree
Hide file tree
Showing 16 changed files with 3,350 additions and 3,120 deletions.
587 changes: 307 additions & 280 deletions src/test/java/net/snowflake/client/jdbc/BindingDataIT.java

Large diffs are not rendered by default.

82 changes: 42 additions & 40 deletions src/test/java/net/snowflake/client/jdbc/CallableStatementIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.math.BigDecimal;
import java.net.URL;
Expand Down Expand Up @@ -45,9 +46,9 @@ public CallableStatementIT(String format) {

public static Connection getConnection() throws SQLException {
Connection conn = BaseJDBCTest.getConnection();
Statement stmt = conn.createStatement();
stmt.execute("alter session set jdbc_query_result_format = '" + queryResultFormat + "'");
stmt.close();
try (Statement stmt = conn.createStatement()) {
stmt.execute("alter session set jdbc_query_result_format = '" + queryResultFormat + "'");
}
return conn;
}

Expand All @@ -60,54 +61,55 @@ public static Connection getConnection() throws SQLException {
private final String deleteStoredProcedure = "drop procedure if exists square_it(FLOAT)";
private final String deleteSecondStoredProcedure = "drop procedure if exists add_nums(INT, INT)";

protected Connection connection = null;
protected Statement statement = null;

@Before
public void setUp() throws SQLException {
Connection con = getConnection();
statement = con.createStatement();
statement.execute(createStoredProcedure);
statement.execute(createSecondStoredProcedure);
con.close();
try (Connection con = getConnection();
Statement statement = con.createStatement()) {
statement.execute(createStoredProcedure);
statement.execute(createSecondStoredProcedure);
}
}

@After
public void tearDown() throws SQLException {
Connection con = getConnection();
statement = con.createStatement();
statement.execute(deleteStoredProcedure);
statement.execute(deleteSecondStoredProcedure);
con.close();
try (Connection con = getConnection();
Statement statement = con.createStatement()) {
statement.execute(deleteStoredProcedure);
statement.execute(deleteSecondStoredProcedure);
}
}

@Test
public void testPrepareCall() throws SQLException {
// test CallableStatement with no binding parameters
connection = getConnection();
statement = connection.createStatement();
CallableStatement callableStatement = connection.prepareCall("call square_it(5)");
assertThat(callableStatement.getParameterMetaData().getParameterCount(), is(0));

// test CallableStatement with 1 binding parameter
callableStatement = connection.prepareCall("call square_it(?)");
// test that getParameterMetaData works with CallableStatement. At this point, it always returns
// the type as "text."
assertThat(callableStatement.getParameterMetaData().getParameterType(1), is(Types.VARCHAR));
callableStatement.getParameterMetaData().getParameterTypeName(1);
assertThat(callableStatement.getParameterMetaData().getParameterTypeName(1), is("text"));
callableStatement.setFloat(1, 7.0f);
ResultSet rs = callableStatement.executeQuery();
rs.next();
assertEquals(49.0f, rs.getFloat(1), 1.0f);

// test CallableStatement with 2 binding parameters
callableStatement = connection.prepareCall("call add_nums(?,?)");
callableStatement.setDouble(1, 32);
callableStatement.setDouble(2, 15);
rs = callableStatement.executeQuery();
rs.next();
assertEquals(47, rs.getDouble(1), .5);
try (Connection connection = getConnection()) {
try (CallableStatement callableStatement = connection.prepareCall("call square_it(5)")) {
assertThat(callableStatement.getParameterMetaData().getParameterCount(), is(0));
}
// test CallableStatement with 1 binding parameter
try (CallableStatement callableStatement = connection.prepareCall("call square_it(?)")) {
// test that getParameterMetaData works with CallableStatement. At this point, it always
// returns
// the type as "text."
assertThat(callableStatement.getParameterMetaData().getParameterType(1), is(Types.VARCHAR));
callableStatement.getParameterMetaData().getParameterTypeName(1);
assertThat(callableStatement.getParameterMetaData().getParameterTypeName(1), is("text"));
callableStatement.setFloat(1, 7.0f);
try (ResultSet rs = callableStatement.executeQuery()) {
assertTrue(rs.next());
assertEquals(49.0f, rs.getFloat(1), 1.0f);
}
}
// test CallableStatement with 2 binding parameters
try (CallableStatement callableStatement = connection.prepareCall("call add_nums(?,?)")) {
callableStatement.setDouble(1, 32);
callableStatement.setDouble(2, 15);
try (ResultSet rs = callableStatement.executeQuery()) {
assertTrue(rs.next());
assertEquals(47, rs.getDouble(1), .5);
}
}
}
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
Expand Down Expand Up @@ -45,29 +47,33 @@ public void testParseSqlEscapeSyntaxFunction() {
@Test
public void testPrepareCallWithCurlyBracketSyntax() throws SQLException {
// test CallableStatement with no binding parameters
connection = getConnection();
statement = connection.createStatement();
CallableStatement callableStatement = connection.prepareCall("{call square_it(5)}");
assertThat(callableStatement.getParameterMetaData().getParameterCount(), is(0));

// test CallableStatement with 1 binding parameter
callableStatement = connection.prepareCall("{call square_it(?)}");
// test that getParameterMetaData works with CallableStatement. At this point, it always returns
// the type as "text."
assertThat(callableStatement.getParameterMetaData().getParameterType(1), is(Types.VARCHAR));
callableStatement.getParameterMetaData().getParameterTypeName(1);
assertThat(callableStatement.getParameterMetaData().getParameterTypeName(1), is("text"));
callableStatement.setFloat(1, 7.0f);
ResultSet rs = callableStatement.executeQuery();
rs.next();
assertEquals(49.0f, rs.getFloat(1), 1.0f);

// test CallableStatement with 2 binding parameters
callableStatement = connection.prepareCall("{call add_nums(?,?)}");
callableStatement.setDouble(1, 32);
callableStatement.setDouble(2, 15);
rs = callableStatement.executeQuery();
rs.next();
assertEquals(47, rs.getDouble(1), .5);
try (Connection connection = getConnection()) {
try (CallableStatement callableStatement = connection.prepareCall("{call square_it(5)}")) {
assertThat(callableStatement.getParameterMetaData().getParameterCount(), is(0));
}
// test CallableStatement with 1 binding parameter
try (CallableStatement callableStatement = connection.prepareCall("{call square_it(?)}")) {
// test that getParameterMetaData works with CallableStatement. At this point, it always
// returns
// the type as "text."
assertThat(callableStatement.getParameterMetaData().getParameterType(1), is(Types.VARCHAR));
callableStatement.getParameterMetaData().getParameterTypeName(1);
assertThat(callableStatement.getParameterMetaData().getParameterTypeName(1), is("text"));
callableStatement.setFloat(1, 7.0f);
try (ResultSet rs = callableStatement.executeQuery()) {
assertTrue(rs.next());
assertEquals(49.0f, rs.getFloat(1), 1.0f);
}
}
// test CallableStatement with 2 binding parameters
try (CallableStatement callableStatement = connection.prepareCall("{call add_nums(?,?)}")) {
callableStatement.setDouble(1, 32);
callableStatement.setDouble(2, 15);
try (ResultSet rs = callableStatement.executeQuery()) {
assertTrue(rs.next());
assertEquals(47, rs.getDouble(1), .5);
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,33 +27,36 @@
@Category(TestCategoryOthers.class)
public class ChunkDownloaderS3RetryUrlLatestIT extends AbstractDriverIT {

private Connection connection;
private SFStatement sfStatement;
private SFBaseSession sfBaseSession;
private ChunkDownloadContext sfContext;

@Before
public void setup() throws SQLException, InterruptedException {
connection = getConnection();
sfBaseSession = connection.unwrap(SnowflakeConnectionV1.class).getSFBaseSession();
Statement statement = connection.createStatement();
sfStatement = statement.unwrap(SnowflakeStatementV1.class).getSfStatement();
int rowCount = 170000;
ResultSet rs =
statement.executeQuery(
"select randstr(100, random()) from table(generator(rowcount => " + rowCount + "))");
List<SnowflakeResultSetSerializable> resultSetSerializables =
((SnowflakeResultSet) rs).getResultSetSerializables(100 * 1024 * 1024);
SnowflakeResultSetSerializable resultSetSerializable = resultSetSerializables.get(0);
SnowflakeChunkDownloader downloader =
new SnowflakeChunkDownloader((SnowflakeResultSetSerializableV1) resultSetSerializable);
SnowflakeResultChunk chunk = downloader.getNextChunkToConsume();
String qrmk = ((SnowflakeResultSetSerializableV1) resultSetSerializable).getQrmk();
Map<String, String> chunkHeadersMap =
((SnowflakeResultSetSerializableV1) resultSetSerializable).getChunkHeadersMap();
sfContext =
new ChunkDownloadContext(
downloader, chunk, qrmk, 0, chunkHeadersMap, 0, 0, 0, 7, sfBaseSession);
try (Connection connection = getConnection();
Statement statement = connection.createStatement()) {
sfBaseSession = connection.unwrap(SnowflakeConnectionV1.class).getSFBaseSession();
sfStatement = statement.unwrap(SnowflakeStatementV1.class).getSfStatement();
int rowCount = 170000;
try (ResultSet rs =
statement.executeQuery(
"select randstr(100, random()) from table(generator(rowcount => "
+ rowCount
+ "))")) {
List<SnowflakeResultSetSerializable> resultSetSerializables =
((SnowflakeResultSet) rs).getResultSetSerializables(100 * 1024 * 1024);
SnowflakeResultSetSerializable resultSetSerializable = resultSetSerializables.get(0);
SnowflakeChunkDownloader downloader =
new SnowflakeChunkDownloader((SnowflakeResultSetSerializableV1) resultSetSerializable);
SnowflakeResultChunk chunk = downloader.getNextChunkToConsume();
String qrmk = ((SnowflakeResultSetSerializableV1) resultSetSerializable).getQrmk();
Map<String, String> chunkHeadersMap =
((SnowflakeResultSetSerializableV1) resultSetSerializable).getChunkHeadersMap();
sfContext =
new ChunkDownloadContext(
downloader, chunk, qrmk, 0, chunkHeadersMap, 0, 0, 0, 7, sfBaseSession);
}
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,18 @@ public class ClientMemoryLimitParallelIT {

@Before
public void setUp() throws SQLException {
Connection con = getConnection();
con.createStatement().execute(createTestTableSQL);
con.close();
try (Connection con = getConnection();
Statement statement = con.createStatement()) {
statement.execute(createTestTableSQL);
}
}

@After
public void tearDown() throws SQLException {
Connection con = getConnection();
con.createStatement().execute("drop table if exists testtable_cml");
con.close();
try (Connection con = getConnection();
Statement statement = con.createStatement()) {
statement.execute("drop table if exists testtable_cml");
}
}

/**
Expand All @@ -88,14 +90,10 @@ public void testParallelQueries() throws Exception {
public void run() {
try {
Properties paramProperties = new Properties();
Connection connection = getConnection(paramProperties);
// create statement
Statement statement = connection.createStatement();

queryRows(statement, 100, 48);
// close
statement.close();
connection.close();
try (Connection connection = getConnection(paramProperties);
Statement statement = connection.createStatement()) {
queryRows(statement, 100, 48);
}
} catch (SQLException e) {
// do not expect exception in test
assertEquals(null, e);
Expand Down Expand Up @@ -128,34 +126,29 @@ public void run() {
@Test
public void testQueryNotHanging() throws SQLException {
Properties paramProperties = new Properties();
Connection connection = getConnection(paramProperties);
// create statement
Statement statement = connection.createStatement();

queryRows(statement, 100, 160);
// close
statement.close();
connection.close();
try (Connection connection = getConnection(paramProperties);
Statement statement = connection.createStatement()) {
queryRows(statement, 100, 160);
}
}

private static void queryRows(Statement stmt, int limit, int chunkSize) throws SQLException {
stmt.execute("alter session set CLIENT_MEMORY_LIMIT=" + limit);
stmt.execute("alter session set CLIENT_RESULT_CHUNK_SIZE=" + chunkSize);
ResultSet resultSet;
String query;
query = "select * from testtable_cml";

resultSet = stmt.executeQuery(query);
try (ResultSet resultSet = stmt.executeQuery(query)) {

// fetch data
int rowIdx = 0;
while (resultSet.next()) {
rowIdx++;
if (rowIdx % 1000 == 0) {
LOGGER.info(Thread.currentThread().getName() + ": processedRows: " + rowIdx);
// fetch data
int rowIdx = 0;
while (resultSet.next()) {
rowIdx++;
if (rowIdx % 1000 == 0) {
LOGGER.info(Thread.currentThread().getName() + ": processedRows: " + rowIdx);
}
}
assertEquals(rowIdx, rowCount);
}
assertEquals(rowIdx, rowCount);
resultSet.close();
}
}
Loading

0 comments on commit a8a3006

Please sign in to comment.