Skip to content
Advertisement

Creating thread safe MyBatis sessions from a java.sql.Connection

I’m trying to use MyBatis with a Snowflake database. My situation is similar to that of this poster:

Configure mybatis to use an existing connection

Essentially, I can get a java.sql.Connection Object, but I cannot get that by way of a DataSource, or other steps normally done by RDBMS databases like Oracle. One proposed solution is to do something like this:

These will be in a multi-threaded environment. If someone closes the snowflakeSession object, does that close the underlying java.sql.Connection object? I plan to re-use these sessions, but only within a thread. Is this necessary?

For Oracle, I can do this:

If I could create say a SnowflakeDataSource object, I could easily create the connection in the same way as I do for Oracle. I have to be careful how to create the connection because it’s also the same point that MyBatis scans the XML mapper files, or re-uses what it already found from a previous connection request.

Advertisement

Answer

It does not appear to be documented but the Snowflake’s JDBC Driver package does offer a basic DataSource class that can fetch entirely new connection objects when DataSource::getConnection() is called:

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement