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(2/4) (#1722)
  • Loading branch information
sfc-gh-ext-simba-jy authored Apr 26, 2024
1 parent 8dcd217 commit 7cb73ff
Show file tree
Hide file tree
Showing 16 changed files with 2,224 additions and 2,288 deletions.
18 changes: 8 additions & 10 deletions src/test/java/net/snowflake/client/jdbc/DellBoomiCloudIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,15 @@ public void setup() {

@Test
public void testSelectLargeResultSet() throws SQLException {
Connection connection = getConnection();
Statement statement = connection.createStatement();
ResultSet resultSet =
statement.executeQuery("select seq4() from table" + "(generator" + "(rowcount=>10000))");
try (Connection connection = getConnection();
Statement statement = connection.createStatement();
ResultSet resultSet =
statement.executeQuery(
"select seq4() from table" + "(generator" + "(rowcount=>10000))")) {

while (resultSet.next()) {
resultSet.getString(1);
while (resultSet.next()) {
resultSet.getString(1);
}
}

resultSet.close();
statement.close();
connection.close();
}
}
1,177 changes: 561 additions & 616 deletions src/test/java/net/snowflake/client/jdbc/FileUploaderLatestIT.java

Large diffs are not rendered by default.

19 changes: 10 additions & 9 deletions src/test/java/net/snowflake/client/jdbc/GCPLargeResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,18 @@ Connection init() throws SQLException {

@Test
public void testLargeResultSetGCP() throws Throwable {
try (Connection con = init()) {
PreparedStatement stmt =
con.prepareStatement(
"select seq8(), randstr(1000, random()) from table(generator(rowcount=>1000))");
try (Connection con = init();
PreparedStatement stmt =
con.prepareStatement(
"select seq8(), randstr(1000, random()) from table(generator(rowcount=>1000))")) {
stmt.setMaxRows(999);
ResultSet rset = stmt.executeQuery();
int cnt = 0;
while (rset.next()) {
++cnt;
try (ResultSet rset = stmt.executeQuery()) {
int cnt = 0;
while (rset.next()) {
++cnt;
}
assertEquals(cnt, 999);
}
assertEquals(cnt, 999);
}
}
}
34 changes: 11 additions & 23 deletions src/test/java/net/snowflake/client/jdbc/HeartbeatAsyncLatestIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,17 @@ public class HeartbeatAsyncLatestIT extends HeartbeatIT {
@Override
protected void submitQuery(boolean useKeepAliveSession, int queryIdx)
throws SQLException, InterruptedException {
Connection connection = null;
ResultSet resultSet = null;
try {
Properties sessionParams = new Properties();
sessionParams.put(
"CLIENT_SESSION_KEEP_ALIVE",
useKeepAliveSession ? Boolean.TRUE.toString() : Boolean.FALSE.toString());
Properties sessionParams = new Properties();
sessionParams.put(
"CLIENT_SESSION_KEEP_ALIVE",
useKeepAliveSession ? Boolean.TRUE.toString() : Boolean.FALSE.toString());

connection = getConnection(sessionParams);

Statement stmt = connection.createStatement();
// Query will take 5 seconds to run, but ResultSet will be returned immediately
resultSet =
stmt.unwrap(SnowflakeStatement.class)
.executeAsyncQuery("SELECT count(*) FROM TABLE(generator(timeLimit => 5))");
try (Connection connection = getConnection(sessionParams);
Statement stmt = connection.createStatement();
// Query will take 5 seconds to run, but ResultSet will be returned immediately
ResultSet resultSet =
stmt.unwrap(SnowflakeStatement.class)
.executeAsyncQuery("SELECT count(*) FROM TABLE(generator(timeLimit => 5))")) {
Thread.sleep(61000); // sleep 61 seconds to await original session expiration time
QueryStatus qs = resultSet.unwrap(SnowflakeResultSet.class).getStatus();
// Ensure query succeeded. Avoid flaky test failure by waiting until query is complete to
Expand All @@ -69,10 +65,6 @@ protected void submitQuery(boolean useKeepAliveSession, int queryIdx)
assertTrue(resultSet.next());
assertFalse(resultSet.next());
logger.fine("Query " + queryIdx + " passed ");

} finally {
resultSet.close();
connection.close();
}
}

Expand All @@ -92,16 +84,12 @@ public void testAsynchronousQueryFailure() throws Exception {
@Test
@ConditionalIgnoreRule.ConditionalIgnore(condition = RunningOnGithubAction.class)
public void testIsValidWithInvalidSession() throws Exception {
Connection connection = null;
try {
connection = getConnection();
try (Connection connection = getConnection()) {
// assert that connection starts out valid
assertTrue(connection.isValid(5));
Thread.sleep(61000); // sleep 61 seconds to await session expiration time
// assert that connection is no longer valid after session has expired
assertFalse(connection.isValid(5));
} finally {
connection.close();
}
}
}
68 changes: 30 additions & 38 deletions src/test/java/net/snowflake/client/jdbc/HeartbeatIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,14 @@ public class HeartbeatIT extends AbstractDriverIT {
@BeforeClass
public static void setUpClass() throws Exception {
if (!RunningOnGithubAction.isRunningOnGithubAction()) {
Connection connection = getSnowflakeAdminConnection();
connection
.createStatement()
.execute(
"alter system set"
+ " master_token_validity=60"
+ ",session_token_validity=20"
+ ",SESSION_RECORD_ACCESS_INTERVAL_SECS=1");
connection.close();
try (Connection connection = getSnowflakeAdminConnection();
Statement statement = connection.createStatement()) {
statement.execute(
"alter system set"
+ " master_token_validity=60"
+ ",session_token_validity=20"
+ ",SESSION_RECORD_ACCESS_INTERVAL_SECS=1");
}
}
}

Expand All @@ -65,15 +64,14 @@ public static void setUpClass() throws Exception {
@AfterClass
public static void tearDownClass() throws Exception {
if (!RunningOnGithubAction.isRunningOnGithubAction()) {
Connection connection = getSnowflakeAdminConnection();
connection
.createStatement()
.execute(
"alter system set"
+ " master_token_validity=default"
+ ",session_token_validity=default"
+ ",SESSION_RECORD_ACCESS_INTERVAL_SECS=default");
connection.close();
try (Connection connection = getSnowflakeAdminConnection();
Statement statement = connection.createStatement()) {
statement.execute(
"alter system set"
+ " master_token_validity=default"
+ ",session_token_validity=default"
+ ",SESSION_RECORD_ACCESS_INTERVAL_SECS=default");
}
}
}

Expand All @@ -87,34 +85,28 @@ public static void tearDownClass() throws Exception {
*/
protected void submitQuery(boolean useKeepAliveSession, int queryIdx)
throws SQLException, InterruptedException {
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
ResultSetMetaData resultSetMetaData;

try {
Properties sessionParams = new Properties();
sessionParams.put(
"CLIENT_SESSION_KEEP_ALIVE",
useKeepAliveSession ? Boolean.TRUE.toString() : Boolean.FALSE.toString());
Properties sessionParams = new Properties();
sessionParams.put(
"CLIENT_SESSION_KEEP_ALIVE",
useKeepAliveSession ? Boolean.TRUE.toString() : Boolean.FALSE.toString());

connection = getConnection(sessionParams);
statement = connection.createStatement();
try (Connection connection = getConnection(sessionParams);
Statement statement = connection.createStatement()) {

Thread.sleep(61000); // sleep 61 seconds
resultSet = statement.executeQuery("SELECT 1");
resultSetMetaData = resultSet.getMetaData();
try (ResultSet resultSet = statement.executeQuery("SELECT 1")) {
resultSetMetaData = resultSet.getMetaData();

// assert column count
assertEquals(1, resultSetMetaData.getColumnCount());
// assert column count
assertEquals(1, resultSetMetaData.getColumnCount());

// assert we get 1 row
assertTrue(resultSet.next());
// assert we get 1 row
assertTrue(resultSet.next());

logger.fine("Query " + queryIdx + " passed ");
statement.close();
} finally {
closeSQLObjects(resultSet, statement, connection);
logger.fine("Query " + queryIdx + " passed ");
}
}
}

Expand Down
Loading

0 comments on commit 7cb73ff

Please sign in to comment.