Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Snow-1213120-Reuse-Connections-1 #1815

Merged
merged 7 commits into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package net.snowflake.client.jdbc;

import java.sql.Connection;
import java.sql.SQLException;
import org.junit.AfterClass;
import org.junit.BeforeClass;

public class BaseJDBCWithSharedConnectionIT extends BaseJDBCTest {

protected static Connection connection;

@BeforeClass
public static void setUpConnection() throws SQLException {
connection = getConnection();
}

@AfterClass
public static void closeConnection() throws SQLException {
if (connection != null && !connection.isClosed()) {
connection.close();
}
}
}
25 changes: 4 additions & 21 deletions src/test/java/net/snowflake/client/jdbc/ResultSet0IT.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,10 @@

/** Result set test base class. */
@Category(TestCategoryResultSet.class)
public class ResultSet0IT extends BaseJDBCTest {
public class ResultSet0IT extends BaseJDBCWithSharedConnectionIT {
private final String queryResultFormat;

public Connection init(int injectSocketTimeout) throws SQLException {
Connection connection = BaseJDBCTest.getConnection(injectSocketTimeout);
try (Statement statement = connection.createStatement()) {
statement.execute(
"alter session set "
+ "TIMEZONE='America/Los_Angeles',"
+ "TIMESTAMP_TYPE_MAPPING='TIMESTAMP_LTZ',"
+ "TIMESTAMP_OUTPUT_FORMAT='DY, DD MON YYYY HH24:MI:SS TZHTZM',"
+ "TIMESTAMP_TZ_OUTPUT_FORMAT='DY, DD MON YYYY HH24:MI:SS TZHTZM',"
+ "TIMESTAMP_LTZ_OUTPUT_FORMAT='DY, DD MON YYYY HH24:MI:SS TZHTZM',"
+ "TIMESTAMP_NTZ_OUTPUT_FORMAT='DY, DD MON YYYY HH24:MI:SS TZHTZM'");
}
return connection;
}

// TODO: Clean up init() methods when updating other test classes to use a common connection.
public Connection init() throws SQLException {
Connection conn = BaseJDBCTest.getConnection(BaseJDBCTest.DONT_INJECT_SOCKET_TIMEOUT);
try (Statement stmt = conn.createStatement()) {
Expand All @@ -54,8 +40,7 @@ public Connection init(Properties paramProperties) throws SQLException {

@Before
public void setUp() throws SQLException {
try (Connection con = init();
Statement statement = con.createStatement()) {
try (Statement statement = connection.createStatement()) {

// TEST_RS
statement.execute("create or replace table test_rs (colA string)");
Expand Down Expand Up @@ -88,9 +73,7 @@ public void setUp() throws SQLException {
}

ResultSet numberCrossTesting() throws SQLException {
Connection con = init();
Statement statement = con.createStatement();

Statement statement = connection.createStatement();
statement.execute(
"create or replace table test_types(c1 number, c2 integer, c3 float, c4 boolean,"
+ "c5 char, c6 varchar, c7 date, c8 datetime, c9 time, c10 timestamp_ltz, "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
Expand All @@ -19,6 +18,7 @@
import net.snowflake.client.ConditionalIgnoreRule;
import net.snowflake.client.RunningOnGithubAction;
import net.snowflake.client.category.TestCategoryResultSet;
import org.junit.Before;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.runner.RunWith;
Expand All @@ -29,7 +29,7 @@
*/
@RunWith(Parameterized.class)
@Category(TestCategoryResultSet.class)
public class ResultSetMultiTimeZoneLatestIT extends BaseJDBCTest {
public class ResultSetMultiTimeZoneLatestIT extends BaseJDBCWithSharedConnectionIT {
@Parameterized.Parameters(name = "format={0}, tz={1}")
public static Collection<Object[]> data() {
// all tests in this class need to run for both query result formats json and arrow
Expand All @@ -53,9 +53,8 @@ public ResultSetMultiTimeZoneLatestIT(String queryResultFormat, String timeZone)
System.setProperty("user.timezone", timeZone);
}

public Connection init() throws SQLException {
Connection connection = BaseJDBCTest.getConnection();

@Before
public void init() throws SQLException {
try (Statement statement = connection.createStatement()) {
statement.execute(
"alter session set "
Expand All @@ -67,7 +66,6 @@ public Connection init() throws SQLException {
+ "TIMESTAMP_NTZ_OUTPUT_FORMAT='DY, DD MON YYYY HH24:MI:SS TZHTZM'");
statement.execute("alter session set jdbc_query_result_format = '" + queryResultFormat + "'");
}
return connection;
}

/**
Expand All @@ -78,8 +76,7 @@ public Connection init() throws SQLException {
*/
@Test
public void testTimesWithGetTimestamp() throws SQLException {
try (Connection connection = init();
Statement statement = connection.createStatement()) {
try (Statement statement = connection.createStatement()) {
String timeStringValue = "10:30:50.123456789";
String timestampStringValue = "1970-01-01 " + timeStringValue;
int length = timestampStringValue.length();
Expand Down Expand Up @@ -113,8 +110,7 @@ public void testTimesWithGetTimestamp() throws SQLException {
*/
@Test
public void testTimestampNTZWithDaylightSavings() throws SQLException {
try (Connection connection = init();
Statement statement = connection.createStatement()) {
try (Statement statement = connection.createStatement()) {
statement.execute(
"alter session set TIMESTAMP_TYPE_MAPPING='TIMESTAMP_NTZ'," + "TIMEZONE='Europe/London'");
try (ResultSet rs = statement.executeQuery("select TIMESTAMP '2011-09-04 00:00:00'")) {
Expand All @@ -135,8 +131,7 @@ public void testDateAndTimestampWithTimezone() throws SQLException {
Calendar cal = null;
SimpleDateFormat sdf = null;

try (Connection connection = init();
Statement statement = connection.createStatement()) {
try (Statement statement = connection.createStatement()) {
statement.execute("alter session set JDBC_FORMAT_DATE_WITH_TIMEZONE=true");
try (ResultSet rs =
statement.executeQuery(
Expand Down Expand Up @@ -232,8 +227,7 @@ public void testUseSessionTimeZoneOverrides() throws SQLException {
* @throws SQLException
*/
private void testUseSessionTimeZoneHelper(boolean useDefaultParamSettings) throws SQLException {
try (Connection connection = init();
Statement statement = connection.createStatement()) {
try (Statement statement = connection.createStatement()) {
try {
// create table with all timestamp types, time, and date
statement.execute(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
Expand Down Expand Up @@ -44,8 +43,7 @@ public static List<String> queryResultFormats() {

@Test
public void testGetIntVectorAsIntArray() throws SQLException {
try (Connection con = BaseJDBCTest.getConnection();
Statement stmt = con.createStatement()) {
try (Statement stmt = connection.createStatement()) {
enforceQueryResultFormat(stmt);
Integer[] vector = {-1, 5};
try (ResultSet resultSet = stmt.executeQuery("select " + vectorToString(vector, "int"))) {
Expand All @@ -60,8 +58,7 @@ public void testGetIntVectorAsIntArray() throws SQLException {

@Test
public void testGetIntVectorAsLongArray() throws SQLException {
try (Connection con = BaseJDBCTest.getConnection();
Statement stmt = con.createStatement()) {
try (Statement stmt = connection.createStatement()) {
enforceQueryResultFormat(stmt);
Long[] vector = {-1L, 5L};
try (ResultSet resultSet = stmt.executeQuery("select " + vectorToString(vector, "int"))) {
Expand All @@ -75,8 +72,7 @@ public void testGetIntVectorAsLongArray() throws SQLException {

@Test
public void testGetFloatVectorAsFloatArray() throws SQLException {
try (Connection con = BaseJDBCTest.getConnection();
Statement stmt = con.createStatement()) {
try (Statement stmt = connection.createStatement()) {
enforceQueryResultFormat(stmt);
Float[] vector = {-1.2f, 5.1f, 15.87f};
try (ResultSet resultSet = stmt.executeQuery("select " + vectorToString(vector, "float"))) {
Expand All @@ -90,8 +86,7 @@ public void testGetFloatVectorAsFloatArray() throws SQLException {

@Test
public void testGetNullAsIntVector() throws SQLException {
try (Connection con = BaseJDBCTest.getConnection();
Statement stmt = con.createStatement()) {
try (Statement stmt = connection.createStatement()) {
enforceQueryResultFormat(stmt);
try (ResultSet resultSet = stmt.executeQuery("select null::vector(int, 2)")) {
assertTrue(resultSet.next());
Expand All @@ -105,8 +100,7 @@ public void testGetNullAsIntVector() throws SQLException {

@Test
public void testGetNullAsFloatVector() throws SQLException {
try (Connection con = BaseJDBCTest.getConnection();
Statement stmt = con.createStatement()) {
try (Statement stmt = connection.createStatement()) {
enforceQueryResultFormat(stmt);
try (ResultSet resultSet = stmt.executeQuery("select null::vector(float, 2)")) {
assertTrue(resultSet.next());
Expand All @@ -120,8 +114,7 @@ public void testGetNullAsFloatVector() throws SQLException {

@Test
public void testGetIntVectorFromTable() throws SQLException {
try (Connection con = BaseJDBCTest.getConnection();
Statement stmt = con.createStatement()) {
try (Statement stmt = connection.createStatement()) {
enforceQueryResultFormat(stmt);
stmt.execute("create or replace table test_vector_int(x vector(int, 2), y int)");
stmt.execute("insert into test_vector_int select [3, 7]::vector(int, 2), 15");
Expand All @@ -137,8 +130,7 @@ public void testGetIntVectorFromTable() throws SQLException {

@Test
public void testGetFloatVectorFromTable() throws SQLException {
try (Connection con = BaseJDBCTest.getConnection();
Statement stmt = con.createStatement()) {
try (Statement stmt = connection.createStatement()) {
enforceQueryResultFormat(stmt);
stmt.execute("create or replace table test_vector_float(x vector(float, 2), y float)");
stmt.execute("insert into test_vector_float select [-3, 7.1]::vector(float, 2), 20.3");
Expand All @@ -154,8 +146,7 @@ public void testGetFloatVectorFromTable() throws SQLException {
/** Added in > 3.16.1 */
@Test
public void testGetVectorViaGetStringIsEqualToTheGetObject() throws SQLException {
try (Connection con = BaseJDBCTest.getConnection();
Statement stmt = con.createStatement()) {
try (Statement stmt = connection.createStatement()) {
enforceQueryResultFormat(stmt);
Integer[] intVector = {-1, 5};
Float[] floatVector = {-1.2f, 5.1f, 15.87f};
Expand Down
Loading
Loading