Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

session.connection() deprecated on Hibernate?

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?

like image 393
TraderJoeChicago Avatar asked Aug 19 '10 21:08

TraderJoeChicago


People also ask

How can we get connection object from hibernate Session?

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.

Can I reuse the Session in hibernate?

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.

Is Session an interface in hibernate?

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.

Can we share Session between threads in hibernate?

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).


1 Answers

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));  
like image 146
KeatsPeeks Avatar answered Sep 20 '22 00:09

KeatsPeeks