51

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?

M Sach
  • 33,416
  • 76
  • 221
  • 314

3 Answers3

96

Here is how you can use it:

session.doWork(new Work() {
    @Override
    public void execute(Connection connection) throws SQLException {
        //connection, finally!
    }
});
Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674
  • Question: I get `doWork is not valid without active transaction` with this code, how to I start a transaction? – OscarRyz Dec 06 '12 at 20:52
  • 2
    @OscarRyz: if you are using Spring, `@Transactional` or `TransactionTemplate` is enough. In raw Hibernate you must [run `session.beginTransaction()` manually](http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/transactions.html#transactions-basics). – Tomasz Nurkiewicz Dec 06 '12 at 20:55
  • Hi Buddy.. Nice answer but doWork() is also deprecated now. – Logicalj Jan 09 '13 at 10:33
  • 2
    @Logicalj: can you provide some reference? It's [still valid in 4.1](http://docs.jboss.org/hibernate/orm/4.1/javadocs/org/hibernate/Session.html#doWork(org.hibernate.jdbc.Work))... – Tomasz Nurkiewicz Jan 09 '13 at 11:14
  • 5
    also worth to mention, there is a doReturningWork(...) method if you want to return a result. – Chris Apr 30 '14 at 16:55
  • @Chris, really worth, I think you should post it as an answer. – Nikita Bosik Jul 13 '16 at 16:55
  • In Java 8 you can do ```session.doWork(connection -> yourMethodInsideDoWork(connection))``` or even ```session.doWork(this::yourMethodInsideDoWork)``` – Optio Jun 14 '17 at 09:12
  • Hi everyone. This video has more information: https://www.youtube.com/watch?v=cCcfZ4QQAiM – Eder Armando Anillo Lora Mar 01 '23 at 20:35
29

Try this:

((SessionImpl)getSession()).connection()
Anthon
  • 69,918
  • 32
  • 186
  • 246
  • 11
    To anyone who gets here unwarned (just like me): the [`Session.connection`](http://docs.jboss.org/hibernate/core/3.5/api/org/hibernate/Session.html#connection%28%29) method is currently deprecated. – Dinei Jun 13 '15 at 21:20
  • 5
    never cast to an Impl! its internal! (org.hibernate.internal.SessionImpl). And you cannot test this code with a mock anymore. This is bad for many reasons. – Rainer Jun 27 '16 at 17:16
12

I had a similar Problem and I used the ConnectionProvider class to get the connection. See my solution:

Session session = entityManager.unwrap(Session.class);
SessionFactoryImplementor sessionFactoryImplementation = (SessionFactoryImplementor) session.getSessionFactory();
ConnectionProvider connectionProvider = sessionFactoryImplementation.getConnectionProvider();
try {
       connection = connectionProvider.getConnection();
       ...
}
awesoon
  • 32,469
  • 11
  • 74
  • 99
OldFabiotta
  • 121
  • 1
  • 4