diff --git a/src/main/java/net/snowflake/client/jdbc/SnowflakeBaseResultSet.java b/src/main/java/net/snowflake/client/jdbc/SnowflakeBaseResultSet.java index d932f322d..5f394002e 100644 --- a/src/main/java/net/snowflake/client/jdbc/SnowflakeBaseResultSet.java +++ b/src/main/java/net/snowflake/client/jdbc/SnowflakeBaseResultSet.java @@ -63,8 +63,8 @@ public abstract class SnowflakeBaseResultSet implements ResultSet { protected SnowflakeResultSetMetaDataV1 resultSetMetaData = null; protected Map parameters = new HashMap<>(); private int fetchSize = 0; - protected SFBaseSession session = null; - private SnowflakeResultSetSerializableV1 serializable = null; + protected final SFBaseSession session; + private final SnowflakeResultSetSerializableV1 serializable; private static final ObjectMapper OBJECT_MAPPER = ObjectMapperFactory.getObjectMapper(); SnowflakeBaseResultSet(Statement statement) throws SQLException { @@ -72,14 +72,19 @@ public abstract class SnowflakeBaseResultSet implements ResultSet { this.resultSetType = statement.getResultSetType(); this.resultSetConcurrency = statement.getResultSetConcurrency(); this.resultSetHoldability = statement.getResultSetHoldability(); + this.session = maybeGetSession(statement); + this.serializable = null; + } + + private static SFBaseSession maybeGetSession(Statement statement) { try { - this.session = statement.unwrap(SnowflakeStatementV1.class).connection.getSFBaseSession(); + return statement.unwrap(SnowflakeStatementV1.class).connection.getSFBaseSession(); } catch (SQLException e) { // This exception shouldn't be hit. Statement class should be able to be unwrapped. logger.error( "Unable to unwrap SnowflakeStatementV1 class to retrieve session. Session is null.", false); - this.session = null; + return null; } } @@ -96,6 +101,7 @@ public SnowflakeBaseResultSet(SnowflakeResultSetSerializableV1 resultSetSerializ this.resultSetType = resultSetSerializable.getResultSetType(); this.resultSetConcurrency = resultSetSerializable.getResultSetConcurrency(); this.resultSetHoldability = resultSetSerializable.getResultSetHoldability(); + this.session = null; this.serializable = resultSetSerializable; } @@ -109,6 +115,8 @@ protected SnowflakeBaseResultSet() throws SQLException { this.resultSetConcurrency = 0; this.resultSetHoldability = 0; this.statement = new SnowflakeStatementV1.NoOpSnowflakeStatementV1(); + this.session = null; + this.serializable = null; } @Override diff --git a/src/test/java/net/snowflake/client/jdbc/ConnectionIT.java b/src/test/java/net/snowflake/client/jdbc/ConnectionIT.java index e6b6bbe4f..9f46bdf07 100644 --- a/src/test/java/net/snowflake/client/jdbc/ConnectionIT.java +++ b/src/test/java/net/snowflake/client/jdbc/ConnectionIT.java @@ -33,6 +33,7 @@ import java.sql.SQLException; import java.sql.SQLFeatureNotSupportedException; import java.sql.Statement; +import java.util.ArrayList; import java.util.Collections; import java.util.Date; import java.util.Enumeration; @@ -41,7 +42,6 @@ import java.util.Properties; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; -import java.util.stream.Collectors; import net.snowflake.client.ConditionalIgnoreRule.ConditionalIgnore; import net.snowflake.client.RunningNotOnTestaccount; import net.snowflake.client.RunningOnGithubAction; @@ -812,21 +812,15 @@ public void testReadDateAfterSplittingResultSet() throws Exception { try (ResultSet rs = statement.executeQuery("select * from table_with_date")) { final SnowflakeResultSet resultSet = rs.unwrap(SnowflakeResultSet.class); + final long arbitrarySizeInBytes = 1024; final List serializables = - resultSet.getResultSetSerializables(1 << 20); - final List dates = - serializables.stream() - .map( - s -> { - try { - ResultSet srs = s.getResultSet(); - srs.next(); - return srs.getDate(2); - } catch (Exception e) { - throw new RuntimeException(e); - } - }) - .collect(Collectors.toList()); + resultSet.getResultSetSerializables(arbitrarySizeInBytes); + final ArrayList dates = new ArrayList<>(); + for (SnowflakeResultSetSerializable s : serializables) { + ResultSet srs = s.getResultSet(); + srs.next(); + dates.add(srs.getDate(2)); + } assertEquals(1, dates.size()); assertEquals("2015-10-25", dates.get(0).toString()); }