2

I am loading drivers dynamically for JDBC. It works fine, however this DriverManager becomes useless when I try to open a hibernate session

org.hibernate.service.classloading.spi.ClassLoadingException: Specified JDBC Driver com.mysql.jdbc.Driver could not be loaded 

Here is the code

    public class TestHibernateSessionFactory {


        public void test() throws MalformedURLException, InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException { 
        URL u = new URL("jar:file:/C:\\Users\\...\\mysql-connector-java-5.1.40-bin.jar!/");
        String classname = "com.mysql.jdbc.Driver";
        URLClassLoader ucl = new URLClassLoader(new URL[] { u });
        Driver d = (Driver)Class.forName(classname, true, ucl).newInstance();
        DriverManager.registerDriver(new DriverLoader(d));


        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306", "admin", "password"); 

    // this is the proof that DriverManager loaded and works fine
        System.out.println("CONNECTION OBJECT WORKS FINE: " + con);

    // Now I want to try this same technique with hibernate     
        Session session = null; 
        Transaction tx = null; 

        SessionFactory sf = buildSessionFactory("jdbc:mysql://localhost:3306", "admin", "password"); 

// ERROR  Specified JDBC Driver com.mysql.jdbc.Driver could not be loaded  WHY ??? 
            session = sf.openSession(); 
            System.out.println(session);     
        }

        private static SessionFactory buildSessionFactory(String myUrl, String myUser, String myPass) {

            Configuration configuration = new Configuration();
            configuration.configure();

            configuration.setProperty("hibernate.connection.url", myUrl);
            configuration.setProperty("hibernate.connection.username", myUser);
            configuration.setProperty("hibernate.connection.password", myPass); 
            configuration.setProperty("hibernate.connection.driver_class", "com.mysql.jdbc.Driver"); 
            configuration.setProperty("hibernate.hbm2ddl.auto", "create-drop"); 

            ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry(); 

            return configuration.buildSessionFactory(serviceRegistry);
    }

    }

Two questions:

  1. Why doesn't it work with hibernate, but works with jdbc?
  2. How to fix it?
2

2 Answers 2

1

The answer is that the Hibernate will attempt to load the driver from the context (current thread) classloader and it doesn't have the driver. The first part works because you use the classloader that has the driver to create a connection.

The solution could be to use manipulate the context classloader. Just don't forget to clean it up when methods using the loaded driver exit.

1
  • Thank you very much +1. Could you please share some code May 27, 2017 at 0:50
1

The approach to your problem could be something like this:

Idea is to have your own classloader and use that ClassLoader class of yours before initializing your Hibernate SessionFactory

Like this:

Thread.currentThread().setContextClassLoader(myOwnClassLoader);

Though this is not one of the best solutions to address your problem, that is how it is. Though this is an incomplete discussion, it is still useful enough to give you pointers to proceed.

Hope this helps!!!

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.