snowflake-jdbc icon indicating copy to clipboard operation
snowflake-jdbc copied to clipboard

SNOW-558640: Executing USE ROLE clears connection catalog/schema

Open grantatspothero opened this issue 2 years ago • 2 comments

Executing USE ROLE clears the connection default catalog but current_database() still returns the default catalog/database. Is this intended behavior?

Example to reproduce behavior:

Connection connection = driver.connect("jdbc:snowflake://<Accountname>:443/?db=demo", properties);

connection.getCatalog(); // returns "demo"
ResultSet rs = connection.prepareStatement("select current_database()").executeQuery(); 
rs.next(); 
rs.getString(1); // returns "demo"


connection.prepareStatement("USE ROLE test").executeQuery();
connection.getCatalog(); // returns null
ResultSet rs = connection.prepareStatement("select current_database()").executeQuery(); 
rs.next(); 
rs.getString(1); // returns "demo"

The reason is the json response from snowflake has finalDatabaseName set to null and then the connection catalog is set to null: https://github.com/snowflakedb/snowflake-jdbc/blob/master/src/main/java/net/snowflake/client/jdbc/SnowflakeResultSetSerializableV1.java#L496-L497

A similar problem also occurs when setting the connection default schema and then executing USE ROLE.

grantatspothero avatar Mar 11 '22 19:03 grantatspothero

@sfc-gh-wfateem is this fixed in any newer versions? I don't see a mention in release notes or a related commit.

hashhar avatar Jun 10 '22 10:06 hashhar

The problem I am running into is that as soon as I have executed USE ROLE on a connection the result of connection.getCatalog becomes incorrect and I have to find alternative ways to preserve and restore correct catalog name in all places where connection is used.

Which either means injecting correct catalog name into SQL statements or changing the parameters passed to JdbcMetaData methods like getTables.

hashhar avatar Jun 10 '22 10:06 hashhar

Please check this with upcoming March 2023 release. We fix similar issue.

sfc-gh-igarish avatar Mar 15 '23 04:03 sfc-gh-igarish

Thank you! Looks like it was fixed.

ssheikin avatar Jul 27 '23 09:07 ssheikin