Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get jdbc connection from hibernate session? [duplicate]

I want to get jdbc connection from hibernate session.There is method inside hibernate session i.e session.connection(); but it has been deprecated. i know this works still but i dont want to use deprecated method as i am sure they must have provide some alternative for this? At http://docs.jboss.org/hibernate/orm/3.5/api/org/hibernate/Session.html connection method api says using org.hibernate.jdbc.Work for this purpose but i dont find any example for that?

like image 356
M Sach Avatar asked Feb 28 '12 13:02

M Sach


People also ask

Can session be reused in Hibernate?

Spring opens a new Hibernate Session at the beginning of the request. These Sessions are not necessarily connected to the database. Every time the application needs a Session, it will reuse the already existing one.

Can we use Hibernate with JDBC?

Hibernate and JPA-based applications do not eliminate the need for JDBC. When you use Hibernate, all database interactions still occur with the JDBC APIs. Hibernate and JPA are actually built on top of the JDBC API.

What happens if Hibernate session is not closed?

When you don't close your Hibernate sessions and therefore do not release JDBC connections, you have what is typically called Connection leak. So, after a number of requests (depending on the size of your connection pool) the server will not be able to acquire a connection to respond your request.


2 Answers

Here is how you can use it:

session.doWork(new Work() {     @Override     public void execute(Connection connection) throws SQLException {         //connection, finally!     } }); 
like image 55
Tomasz Nurkiewicz Avatar answered Sep 20 '22 03:09

Tomasz Nurkiewicz


Try this:

((SessionImpl)getSession()).connection() 
like image 37
Mohammad Hosseini Avatar answered Sep 21 '22 03:09

Mohammad Hosseini