2

I have requirement where I need java.sql.Connection. But I am using Hibernate here. Somehow I researched and I found below alternative but that's not working.

import org.hibernate.connection.ProxoolConnectionProvider;
public class ConnectionDB{
    //I have imported below class 
    ProxoolConnectionProvider proxoolProvider = new ProxoolConnectionProvider();
    org.hibernate.cfg.Configuration cfg = HibernateUtil.getConfiguration();//this method will return configuration 
    java.util.Properties props =  cfg.getProperties();//This will return Properties Object
    //Using properties object I just tried to get The Connection Object by following method
    proxoolConn.configure(props);// I just configured the Porperties object 
    proxoolConn.getConnection();
}

But no luck I just end-up with no exception in console .. I am using Struts 2, Hibernate and JasperReports.

Can any one help me to get the connection object from Hibernate?

2
  • What version of Hibernate are you using? Commented Jun 11, 2014 at 0:44
  • @Muel hibernate3.jar I can see in my library.. Commented Jun 11, 2014 at 0:49

2 Answers 2

7

The following code assumes you have an existing and open Hibernate org.hibernate.Session:

public void doWorkOnConnection(Session session) {
  session.doWork(new Work() {
    public void execute(Connection connection) throws SQLException {
      //use the connection here...
    }
  });
}

Should you need information on how to use the above classes, please read the Hibernate 3.5 Javadoc. Specifically, read Session and then Work.

Sign up to request clarification or add additional context in comments.

1 Comment

In Java 8 you can do session.doWork(connection -> yourMethodInsideDoWork(connection)) or even session.doWork(this::yourMethodInsideDoWork)
1

You can also downcast the Session method into a SessionImpl and get the connection object easily:

SessionImpl sessionImpl = (SessionImpl) session;
Connection conn = sessionImpl.connection();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.