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

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

    fharms

      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

       

      <resources>
              <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 -->
      </resources>
      

       

      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">
          <resources>
              <resource-root path="jboss-as-db-bootstrap-1.0.2.BETA.jar"/>
          </resources>
          <dependencies>
             <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"/>
          </dependencies>
      </module>
      

       

      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(
                              classLoaderService,
                              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;
       }
      

       

      Thanks

      /Flemming

        • 1. Re: My JBoss extension can't see the database driver because of a classloader issue
          wdfink

          You might flag the modules with <module name="" export="true"

           

          See the xsd

          Specifies whether this module dependency is re-exported by default (default is "false"). Setting this attribute to true sets the default action for the export filter list to "accept"; leaving it as false sets the default action to "reject".  Thus you can still export dependency resources even if this attribute is false by listing explicit paths for the export list.

          • 2. Re: Re: My JBoss extension can't see the database driver because of a classloader issue
            fharms

            I already tried that with no success.


            In debug mode I can see when Hibernate get a connection through the DriveManage it use this "ModuleClassLoader for Module "org.hibernate:main" from local module loader" to load the driver class, and this is after I tried export the postgresql module.

             

            <module name="org.postgresql" export="true"/>
            

             

            /Flemming