We need to be able to get the associated java.sql.Connection
of a hibernate session. No other connection will work, as this connection may be associated with a running transaction.
If session.connection() is now deprecated, how am I supposed to do that?
Hibernate doesn't provide any method to retrieve the java. sql. Connection that's used by the current Session. But you can call the doWork(Work work) or doReturningWork(ReturningWork<T> work) method on the Session interface to perform JDBC related work.
So, how can i reuse an Hibernate Session, in the same thread, that has been previously closed? Either use the built-in " managed " strategy (set the current_session_context_class property to managed ) or use a custom CurrentSessionContext derived from ThreadLocalSessionContext and override ThreadLocalSessionContet.
The Session interface is the main tool used to communicate with Hibernate. It provides an API enabling us to create, read, update, and delete persistent objects. The session has a simple lifecycle.
If you were sharing a Hibernate Session between two threads, then one thread changes might not be visible to some other thread (without proper synchronization or volatile reads).
You now have to use the Work API:
session.doWork( new Work() { public void execute(Connection connection) throws SQLException { doSomething(connection); } } );
Or, in Java 8+ :
session.doWork(connection -> doSomething(connection));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With