Skip to content

Commit 7cb73ff

Browse files
SNOW-1213117: Wrap connection, statement and result set in try with resources(2/4) (#1722)
1 parent 8dcd217 commit 7cb73ff

16 files changed

+2224
-2288
lines changed

src/test/java/net/snowflake/client/jdbc/DellBoomiCloudIT.java

+8-10
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,15 @@ public void setup() {
2626

2727
@Test
2828
public void testSelectLargeResultSet() throws SQLException {
29-
Connection connection = getConnection();
30-
Statement statement = connection.createStatement();
31-
ResultSet resultSet =
32-
statement.executeQuery("select seq4() from table" + "(generator" + "(rowcount=>10000))");
29+
try (Connection connection = getConnection();
30+
Statement statement = connection.createStatement();
31+
ResultSet resultSet =
32+
statement.executeQuery(
33+
"select seq4() from table" + "(generator" + "(rowcount=>10000))")) {
3334

34-
while (resultSet.next()) {
35-
resultSet.getString(1);
35+
while (resultSet.next()) {
36+
resultSet.getString(1);
37+
}
3638
}
37-
38-
resultSet.close();
39-
statement.close();
40-
connection.close();
4139
}
4240
}

src/test/java/net/snowflake/client/jdbc/FileUploaderLatestIT.java

+561-616
Large diffs are not rendered by default.

src/test/java/net/snowflake/client/jdbc/GCPLargeResult.java

+10-9
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,18 @@ Connection init() throws SQLException {
3737

3838
@Test
3939
public void testLargeResultSetGCP() throws Throwable {
40-
try (Connection con = init()) {
41-
PreparedStatement stmt =
42-
con.prepareStatement(
43-
"select seq8(), randstr(1000, random()) from table(generator(rowcount=>1000))");
40+
try (Connection con = init();
41+
PreparedStatement stmt =
42+
con.prepareStatement(
43+
"select seq8(), randstr(1000, random()) from table(generator(rowcount=>1000))")) {
4444
stmt.setMaxRows(999);
45-
ResultSet rset = stmt.executeQuery();
46-
int cnt = 0;
47-
while (rset.next()) {
48-
++cnt;
45+
try (ResultSet rset = stmt.executeQuery()) {
46+
int cnt = 0;
47+
while (rset.next()) {
48+
++cnt;
49+
}
50+
assertEquals(cnt, 999);
4951
}
50-
assertEquals(cnt, 999);
5152
}
5253
}
5354
}

src/test/java/net/snowflake/client/jdbc/HeartbeatAsyncLatestIT.java

+11-23
Original file line numberDiff line numberDiff line change
@@ -39,21 +39,17 @@ public class HeartbeatAsyncLatestIT extends HeartbeatIT {
3939
@Override
4040
protected void submitQuery(boolean useKeepAliveSession, int queryIdx)
4141
throws SQLException, InterruptedException {
42-
Connection connection = null;
43-
ResultSet resultSet = null;
44-
try {
45-
Properties sessionParams = new Properties();
46-
sessionParams.put(
47-
"CLIENT_SESSION_KEEP_ALIVE",
48-
useKeepAliveSession ? Boolean.TRUE.toString() : Boolean.FALSE.toString());
42+
Properties sessionParams = new Properties();
43+
sessionParams.put(
44+
"CLIENT_SESSION_KEEP_ALIVE",
45+
useKeepAliveSession ? Boolean.TRUE.toString() : Boolean.FALSE.toString());
4946

50-
connection = getConnection(sessionParams);
51-
52-
Statement stmt = connection.createStatement();
53-
// Query will take 5 seconds to run, but ResultSet will be returned immediately
54-
resultSet =
55-
stmt.unwrap(SnowflakeStatement.class)
56-
.executeAsyncQuery("SELECT count(*) FROM TABLE(generator(timeLimit => 5))");
47+
try (Connection connection = getConnection(sessionParams);
48+
Statement stmt = connection.createStatement();
49+
// Query will take 5 seconds to run, but ResultSet will be returned immediately
50+
ResultSet resultSet =
51+
stmt.unwrap(SnowflakeStatement.class)
52+
.executeAsyncQuery("SELECT count(*) FROM TABLE(generator(timeLimit => 5))")) {
5753
Thread.sleep(61000); // sleep 61 seconds to await original session expiration time
5854
QueryStatus qs = resultSet.unwrap(SnowflakeResultSet.class).getStatus();
5955
// Ensure query succeeded. Avoid flaky test failure by waiting until query is complete to
@@ -69,10 +65,6 @@ protected void submitQuery(boolean useKeepAliveSession, int queryIdx)
6965
assertTrue(resultSet.next());
7066
assertFalse(resultSet.next());
7167
logger.fine("Query " + queryIdx + " passed ");
72-
73-
} finally {
74-
resultSet.close();
75-
connection.close();
7668
}
7769
}
7870

@@ -92,16 +84,12 @@ public void testAsynchronousQueryFailure() throws Exception {
9284
@Test
9385
@ConditionalIgnoreRule.ConditionalIgnore(condition = RunningOnGithubAction.class)
9486
public void testIsValidWithInvalidSession() throws Exception {
95-
Connection connection = null;
96-
try {
97-
connection = getConnection();
87+
try (Connection connection = getConnection()) {
9888
// assert that connection starts out valid
9989
assertTrue(connection.isValid(5));
10090
Thread.sleep(61000); // sleep 61 seconds to await session expiration time
10191
// assert that connection is no longer valid after session has expired
10292
assertFalse(connection.isValid(5));
103-
} finally {
104-
connection.close();
10593
}
10694
}
10795
}

src/test/java/net/snowflake/client/jdbc/HeartbeatIT.java

+30-38
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,14 @@ public class HeartbeatIT extends AbstractDriverIT {
4646
@BeforeClass
4747
public static void setUpClass() throws Exception {
4848
if (!RunningOnGithubAction.isRunningOnGithubAction()) {
49-
Connection connection = getSnowflakeAdminConnection();
50-
connection
51-
.createStatement()
52-
.execute(
53-
"alter system set"
54-
+ " master_token_validity=60"
55-
+ ",session_token_validity=20"
56-
+ ",SESSION_RECORD_ACCESS_INTERVAL_SECS=1");
57-
connection.close();
49+
try (Connection connection = getSnowflakeAdminConnection();
50+
Statement statement = connection.createStatement()) {
51+
statement.execute(
52+
"alter system set"
53+
+ " master_token_validity=60"
54+
+ ",session_token_validity=20"
55+
+ ",SESSION_RECORD_ACCESS_INTERVAL_SECS=1");
56+
}
5857
}
5958
}
6059

@@ -65,15 +64,14 @@ public static void setUpClass() throws Exception {
6564
@AfterClass
6665
public static void tearDownClass() throws Exception {
6766
if (!RunningOnGithubAction.isRunningOnGithubAction()) {
68-
Connection connection = getSnowflakeAdminConnection();
69-
connection
70-
.createStatement()
71-
.execute(
72-
"alter system set"
73-
+ " master_token_validity=default"
74-
+ ",session_token_validity=default"
75-
+ ",SESSION_RECORD_ACCESS_INTERVAL_SECS=default");
76-
connection.close();
67+
try (Connection connection = getSnowflakeAdminConnection();
68+
Statement statement = connection.createStatement()) {
69+
statement.execute(
70+
"alter system set"
71+
+ " master_token_validity=default"
72+
+ ",session_token_validity=default"
73+
+ ",SESSION_RECORD_ACCESS_INTERVAL_SECS=default");
74+
}
7775
}
7876
}
7977

@@ -87,34 +85,28 @@ public static void tearDownClass() throws Exception {
8785
*/
8886
protected void submitQuery(boolean useKeepAliveSession, int queryIdx)
8987
throws SQLException, InterruptedException {
90-
Connection connection = null;
91-
Statement statement = null;
92-
ResultSet resultSet = null;
9388
ResultSetMetaData resultSetMetaData;
9489

95-
try {
96-
Properties sessionParams = new Properties();
97-
sessionParams.put(
98-
"CLIENT_SESSION_KEEP_ALIVE",
99-
useKeepAliveSession ? Boolean.TRUE.toString() : Boolean.FALSE.toString());
90+
Properties sessionParams = new Properties();
91+
sessionParams.put(
92+
"CLIENT_SESSION_KEEP_ALIVE",
93+
useKeepAliveSession ? Boolean.TRUE.toString() : Boolean.FALSE.toString());
10094

101-
connection = getConnection(sessionParams);
102-
statement = connection.createStatement();
95+
try (Connection connection = getConnection(sessionParams);
96+
Statement statement = connection.createStatement()) {
10397

10498
Thread.sleep(61000); // sleep 61 seconds
105-
resultSet = statement.executeQuery("SELECT 1");
106-
resultSetMetaData = resultSet.getMetaData();
99+
try (ResultSet resultSet = statement.executeQuery("SELECT 1")) {
100+
resultSetMetaData = resultSet.getMetaData();
107101

108-
// assert column count
109-
assertEquals(1, resultSetMetaData.getColumnCount());
102+
// assert column count
103+
assertEquals(1, resultSetMetaData.getColumnCount());
110104

111-
// assert we get 1 row
112-
assertTrue(resultSet.next());
105+
// assert we get 1 row
106+
assertTrue(resultSet.next());
113107

114-
logger.fine("Query " + queryIdx + " passed ");
115-
statement.close();
116-
} finally {
117-
closeSQLObjects(resultSet, statement, connection);
108+
logger.fine("Query " + queryIdx + " passed ");
109+
}
118110
}
119111
}
120112

0 commit comments

Comments
 (0)