2 Replies Latest reply on Jul 13, 2014 12:21 PM by Flemming Harms

    My JBoss extension can't see the database driver because of a classloader issue

    Flemming Harms Novice

      First a short description of what I trying to achieve. I have created my own JBoss7 extension where I hook into the PARSE_WEB_DEPLOYMENT phase and I want to create my own temp hibernate session for bootstrapping our database.


      But for some reason Hibernate can’t see the postgrel driver because of a classloader issue. From what I can see it try to load the postgresql driver from the hibernate module, and it can’t see the postgrel driver of course.


      I found a temp workaround where I define the hibernate and postgrel resource in the resources section instead, but I would prefer I could just depend on existing modules instead


              <resource-root path="jboss-as-db-bootstrap-1.0.2.BETA.jar"/>
              <resource-root path="postgresql-9.1-903.jdbc4.jar"/>
              <resource-root path="hibernate-core-4.2.0.CR1.jar"/>
              <resource-root path="hibernate-commons-annotations-4.0.1.Final.jar"/>
              <!-- Insert resources here -->


      I have tried many solution with no luck, so my question is how can archive this using the module.xml below.


      <module xmlns="urn:jboss:module:1.1" name="org.jboss.as.extension.db_bootstrap">
              <resource-root path="jboss-as-db-bootstrap-1.0.2.BETA.jar"/>
             <module name="javax.annotation.api"/>
              <module name="javax.api"/>
              <module name="org.jboss.jandex"/>
              <module name="org.jboss.staxmapper"/>
              <module name="org.jboss.as.controller"/>
              <module name="org.jboss.as.domain-management"/>
              <module name="org.jboss.as.server"/>
              <module name="org.jboss.metadata"/>
              <module name="org.jboss.modules"/>
              <module name="org.jboss.msc"/>
              <module name="org.jboss.vfs"/>
              <module name="org.jboss.logging"/>
             <module name="org.scannotation.scannotation" />
              <!-- user modules for the plug-in -->
              <module name="org.hibernate" />
              <module name="org.hibernate.envers" />
              <module name="org.javassist"/>
              <module name="org.apache.log4j" />
              <module name="org.postgresql"/>


      Code for bootstrapping hibernate from the sub module.

      ClassLoader currentClassLoader = DbBootstrapDetectorResourceDefinition.class.getClassLoader();
      URL resource = classLoader.getResource(hibernateCfg);
      ClassLoaderService classLoaderService = createClassLoaderService(classLoader,currentClassLoader);
      DbBootstrapLogger.ROOT_LOGGER.tracef("Using hibernate configuration file %s ",hibernateCfg);
      Configuration configuration = new Configuration();
      configuration.configure(resource); // configures settings from hibernate.cfg.xml
      SessionFactory sessionFactory = configuration.buildSessionFactory(new ServiceRegistryBuilder(
                      new BootstrapServiceRegistryImpl(
                              new LinkedHashSet<Integrator>())).applySettings(configuration.getProperties()).buildServiceRegistry());
       return sessionFactory.openSession();
      private ClassLoaderService createClassLoaderService(ClassLoader classLoader, ClassLoader currentClassLoader) throws Exception{
              ClassLoaderService classLoaderService = new ClassLoaderServiceImpl(currentClassLoader, classLoader, classLoader,currentClassLoader);
              return classLoaderService;