you're right! my snippet code works only when sessionFactory starts but not after...in fact, i saw in DriverManagerConnectionProvider (configure() method) that hibernate try to find the driver class using Thread.currentThread().getContextClassLoader() as last option
Code:
try {
// trying via forName() first to be as close to DriverManager's semantics
Class.forName(driverClass);
}
catch (ClassNotFoundException cnfe) {
try {
ReflectHelper.classForName(driverClass);
}
catch (ClassNotFoundException e) {
String msg = "JDBC Driver class not found: " + driverClass;
log.error( msg, e );
throw new HibernateException(msg, e);
}
}
....
ReflectHelper.classForName() method:
try {
ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
if ( contextClassLoader != null ) {
return contextClassLoader.loadClass(name);
}
}
catch ( Throwable ignore ) {
}
But when application (after hibernate initialization) requests to obtain a connection the follow code is used:
Connection conn = DriverManager.getConnection(url, connectionProps);
however, in this case, DriverManager looks for caller classloader (to find registered Driver) and not your custom ClassLoader (which registered the Driver).
I think either you must supply your own implementation of ConnectionProvider and use :
Code:
Class<?> clazz = Class.forName(driverClass, true, Thread.currentThread().getContextClassLoader());
Driver loadedDriver = (Driver) clazz.newInstance();
conn = loadedDriver.connect(jdbcUrl, infos);
or inspire you of org.hibernate.console.ConsoleConfiguration class and registerFakeDriver method. I noted they use DriverManager.registerDriver(Driver) method. the jars which contains this class is: org.hibernate.eclipse.jar
I hope it help you!