Skip to content

Commit 8bc1ae5

Browse files
Snow-1213120: Reuse-Connections-4 (#1817)
Co-authored-by: Piotr Bulawa <piotr.bulawa@snowflake.com>
1 parent 323fb54 commit 8bc1ae5

4 files changed

Lines changed: 444 additions & 495 deletions

File tree

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

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
* @author azhan attempts to test the CLIENT_MEMORY_LIMIT working in multi-threading
2222
*/
2323
@Category(TestCategoryOthers.class)
24-
public class ClientMemoryLimitParallelIT {
24+
public class ClientMemoryLimitParallelIT extends BaseJDBCWithSharedConnectionIT {
2525
private static Logger LOGGER =
2626
LoggerFactory.getLogger(ClientMemoryLimitParallelIT.class.getName());
2727

@@ -64,16 +64,14 @@ public class ClientMemoryLimitParallelIT {
6464

6565
@Before
6666
public void setUp() throws SQLException {
67-
try (Connection con = getConnection();
68-
Statement statement = con.createStatement()) {
67+
try (Statement statement = connection.createStatement()) {
6968
statement.execute(createTestTableSQL);
7069
}
7170
}
7271

7372
@After
7473
public void tearDown() throws SQLException {
75-
try (Connection con = getConnection();
76-
Statement statement = con.createStatement()) {
74+
try (Statement statement = connection.createStatement()) {
7775
statement.execute("drop table if exists testtable_cml");
7876
}
7977
}
@@ -126,8 +124,7 @@ public void run() {
126124
@Test
127125
public void testQueryNotHanging() throws SQLException {
128126
Properties paramProperties = new Properties();
129-
try (Connection connection = getConnection(paramProperties);
130-
Statement statement = connection.createStatement()) {
127+
try (Statement statement = connection.createStatement()) {
131128
queryRows(statement, 100, 160);
132129
}
133130
}

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

Lines changed: 46 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959

6060
/** Connection integration tests */
6161
@Category(TestCategoryConnection.class)
62-
public class ConnectionIT extends BaseJDBCTest {
62+
public class ConnectionIT extends BaseJDBCWithSharedConnectionIT {
6363
// create a local constant for this code for testing purposes (already defined in GS)
6464
public static final int INVALID_CONNECTION_INFO_CODE = 390100;
6565
private static final int SESSION_CREATION_OBJECT_DOES_NOT_EXIST_NOT_AUTHORIZED = 390201;
@@ -90,10 +90,9 @@ public void testSimpleConnection() throws SQLException {
9090
public void test300ConnectionsWithSingleClientInstance() throws SQLException {
9191
// concurrent testing
9292
int size = 300;
93-
try (Connection con = getConnection();
94-
Statement statement = con.createStatement()) {
95-
String database = con.getCatalog();
96-
String schema = con.getSchema();
93+
try (Statement statement = connection.createStatement()) {
94+
String database = connection.getCatalog();
95+
String schema = connection.getSchema();
9796
statement.execute(
9897
"create or replace table bigTable(rowNum number,rando "
9998
+ "number) as (select seq4(),"
@@ -168,8 +167,7 @@ public void testProdConnectivity() throws SQLException {
168167

169168
@Test
170169
public void testSetCatalogSchema() throws Throwable {
171-
try (Connection connection = getConnection();
172-
Statement statement = connection.createStatement()) {
170+
try (Statement statement = connection.createStatement()) {
173171
String db = connection.getCatalog();
174172
String schema = connection.getSchema();
175173
connection.setCatalog(db);
@@ -220,31 +218,30 @@ public void testDataCompletenessInLowMemory() throws Exception {
220218
public void testConnectionGetAndSetDBAndSchema() throws SQLException {
221219
final String SECOND_DATABASE = "SECOND_DATABASE";
222220
final String SECOND_SCHEMA = "SECOND_SCHEMA";
223-
try (Connection con = getConnection();
224-
Statement statement = con.createStatement()) {
221+
try (Statement statement = connection.createStatement()) {
225222
try {
226223
final String database = TestUtil.systemGetEnv("SNOWFLAKE_TEST_DATABASE").toUpperCase();
227224
final String schema = TestUtil.systemGetEnv("SNOWFLAKE_TEST_SCHEMA").toUpperCase();
228225

229-
assertEquals(database, con.getCatalog());
230-
assertEquals(schema, con.getSchema());
226+
assertEquals(database, connection.getCatalog());
227+
assertEquals(schema, connection.getSchema());
231228

232229
statement.execute(String.format("create or replace database %s", SECOND_DATABASE));
233230
statement.execute(String.format("create or replace schema %s", SECOND_SCHEMA));
234231
statement.execute(String.format("use database %s", database));
235232

236-
con.setCatalog(SECOND_DATABASE);
237-
assertEquals(SECOND_DATABASE, con.getCatalog());
238-
assertEquals("PUBLIC", con.getSchema());
233+
connection.setCatalog(SECOND_DATABASE);
234+
assertEquals(SECOND_DATABASE, connection.getCatalog());
235+
assertEquals("PUBLIC", connection.getSchema());
239236

240-
con.setSchema(SECOND_SCHEMA);
241-
assertEquals(SECOND_SCHEMA, con.getSchema());
237+
connection.setSchema(SECOND_SCHEMA);
238+
assertEquals(SECOND_SCHEMA, connection.getSchema());
242239

243240
statement.execute(String.format("use database %s", database));
244241
statement.execute(String.format("use schema %s", schema));
245242

246-
assertEquals(database, con.getCatalog());
247-
assertEquals(schema, con.getSchema());
243+
assertEquals(database, connection.getCatalog());
244+
assertEquals(schema, connection.getSchema());
248245
} finally {
249246
statement.execute(String.format("drop database if exists %s", SECOND_DATABASE));
250247
}
@@ -253,40 +250,39 @@ public void testConnectionGetAndSetDBAndSchema() throws SQLException {
253250

254251
@Test
255252
public void testConnectionClientInfo() throws SQLException {
256-
try (Connection con = getConnection()) {
257-
Properties property = con.getClientInfo();
258-
assertEquals(0, property.size());
259-
Properties clientInfo = new Properties();
260-
clientInfo.setProperty("name", "Peter");
261-
clientInfo.setProperty("description", "SNOWFLAKE JDBC");
262-
try {
263-
con.setClientInfo(clientInfo);
264-
fail("setClientInfo should fail for any parameter.");
265-
} catch (SQLClientInfoException e) {
266-
assertEquals(SqlState.INVALID_PARAMETER_VALUE, e.getSQLState());
267-
assertEquals(200047, e.getErrorCode());
268-
assertEquals(2, e.getFailedProperties().size());
269-
}
270-
try {
271-
con.setClientInfo("ApplicationName", "valueA");
272-
fail("setClientInfo should fail for any parameter.");
273-
} catch (SQLClientInfoException e) {
274-
assertEquals(SqlState.INVALID_PARAMETER_VALUE, e.getSQLState());
275-
assertEquals(200047, e.getErrorCode());
276-
assertEquals(1, e.getFailedProperties().size());
277-
}
253+
Properties property = connection.getClientInfo();
254+
assertEquals(0, property.size());
255+
Properties clientInfo = new Properties();
256+
clientInfo.setProperty("name", "Peter");
257+
clientInfo.setProperty("description", "SNOWFLAKE JDBC");
258+
try {
259+
connection.setClientInfo(clientInfo);
260+
fail("setClientInfo should fail for any parameter.");
261+
} catch (SQLClientInfoException e) {
262+
assertEquals(SqlState.INVALID_PARAMETER_VALUE, e.getSQLState());
263+
assertEquals(200047, e.getErrorCode());
264+
assertEquals(2, e.getFailedProperties().size());
265+
}
266+
try {
267+
connection.setClientInfo("ApplicationName", "valueA");
268+
fail("setClientInfo should fail for any parameter.");
269+
} catch (SQLClientInfoException e) {
270+
assertEquals(SqlState.INVALID_PARAMETER_VALUE, e.getSQLState());
271+
assertEquals(200047, e.getErrorCode());
272+
assertEquals(1, e.getFailedProperties().size());
278273
}
279274
}
280275

281276
// only support get and set
282277
@Test
283278
public void testNetworkTimeout() throws SQLException {
284-
try (Connection con = getConnection()) {
285-
int millis = con.getNetworkTimeout();
286-
assertEquals(0, millis);
287-
con.setNetworkTimeout(null, 200);
288-
assertEquals(200, con.getNetworkTimeout());
289-
}
279+
int millis = connection.getNetworkTimeout();
280+
assertEquals(0, millis);
281+
connection.setNetworkTimeout(null, 200);
282+
assertEquals(200, connection.getNetworkTimeout());
283+
// Reset timeout to 0 since we are reusing connection in tests
284+
connection.setNetworkTimeout(null, 0);
285+
assertEquals(0, millis);
290286
}
291287

292288
@Test
@@ -725,18 +721,14 @@ public void testHeartbeatFrequencyTooLarge() throws Exception {
725721

726722
@Test
727723
public void testNativeSQL() throws Throwable {
728-
try (Connection connection = getConnection()) {
729-
// today returning the source SQL.
730-
assertEquals("select 1", connection.nativeSQL("select 1"));
731-
}
724+
// today returning the source SQL.
725+
assertEquals("select 1", connection.nativeSQL("select 1"));
732726
}
733727

734728
@Test
735729
public void testGetTypeMap() throws Throwable {
736-
try (Connection connection = getConnection()) {
737-
// return an empty type map. setTypeMap is not supported.
738-
assertEquals(Collections.emptyMap(), connection.getTypeMap());
739-
}
730+
// return an empty type map. setTypeMap is not supported.
731+
assertEquals(Collections.emptyMap(), connection.getTypeMap());
740732
}
741733

742734
@Test
@@ -829,7 +821,6 @@ public void testReadDateAfterSplittingResultSet() throws Exception {
829821

830822
@Test
831823
public void testResultSetsClosedByStatement() throws SQLException {
832-
Connection connection = getConnection();
833824
Statement statement2 = connection.createStatement();
834825
ResultSet rs1 = statement2.executeQuery("select 2;");
835826
ResultSet rs2 = statement2.executeQuery("select 2;");
@@ -846,7 +837,6 @@ public void testResultSetsClosedByStatement() throws SQLException {
846837
assertTrue(rs2.isClosed());
847838
assertTrue(rs3.isClosed());
848839
assertTrue(rs4.isClosed());
849-
connection.close();
850840
}
851841

852842
@Test

0 commit comments

Comments
 (0)