2 Replies Latest reply on Feb 2, 2017 8:55 AM by wdfink

    Cannot access added as module jdbc postgres jar - WildFly 10.1

    drzeyan

      I have postgres jdbc driver configured as module in Wildfly 10.1. What I am trying to do, is to use that driver as dependency in application that will be deployed on the server. In application, I mark this dependency as provided (in Maven's pom.xml. I do the same with jboss-logging or java-ee dependency) but it seems to be not working.

      Current configuration:

      Wildfly postgres module is added at wildfly-10.1.0.Final\modules\org\postgresql\mainwhere there is: postgresql-9.4-1206-jdbc4.jar and module.xml with following content:

      <module xmlns="urn:jboss:module:1.1" name="org.postgresql"> 
           <resources> 
                <resource-root path="postgresql-9.4-1206-jdbc4.jar"/> 
           </resources> 
           <dependencies> 
                <module name="javax.api"/> 
                <module name="javax.transaction.api"/> 
           </dependencies> 
      </module>
      

       

      Module is used to define datasource. To this point, everything is working well - with hibernate help tables are happily mapped to entities. Except one thing:

      I started to map postgres-jsonb columns with use of javax.persistence.AttributeConverterand following happens:

      Scenario 1

      When I mark postgresql-9.4-1206-jdbc4.jar as a provided (in pom.xml - deployed application), I get following error trying to convert anything:

      Caused by: java.lang.ClassNotFoundException: org.postgresql.util.PGobject from [Module "deployment.project-1.0.1.ear.project.data-1.0.1-SNAPSHOT.jar:main" from Service Module Loader] at org.jboss.modules.ModuleClassLoader.findClass(ModuleClassLoader.java:198) at org.jboss.modules.ConcurrentClassLoader.performLoadClassUnchecked(ConcurrentClassLoader.java:363) at org.jboss.modules.ConcurrentClassLoader.performLoadClass(ConcurrentClassLoader.java:351) at org.jboss.modules.ConcurrentClassLoader.loadClass(ConcurrentClassLoader.java:93) ... 269 more

      Which means that postgres jar somehow can not be accessed. I expected  that this will work the same as java-ee or jboss-logging dependencies which I mark as provided, but apparently it's not.

      Scenario 2

      When I use postgresql-9.4-1206-jdbc4.jar with default scope, there is following error:

      Caused by: java.lang.ClassCastException: org.postgresql.util.PGobject cannot be cast to org.postgresql.util.PGobject at com.project.entity.util.converters.JSONBTypeConverter.convertToEntityAttribute(JSONBTypeConverter.java:33) at com.project.entity.util.converters.JSONBTypeConverter.convertToEntityAttribute(JSONBTypeConverter.java:1) at org.hibernate.type.descriptor.converter.AttributeConverterSqlTypeDescriptorAdapter$2.doConversion(AttributeConverterSqlTypeDescriptorAdapter.java:140) ... 266 more

      Which means: class loader loads the same jar two times, and can not cast objects to itself.

      Question: Why provided scope of the dependency does not work for manually added (to wildfly) postgres driver?

        • 1. Re: Cannot access added as module jdbc postgres jar - WildFly 10.1
          andey

          You need to use a jboss-deployment-structure.xml to add the module dependency to your deployment.

          ------------------------------------------------------------

          <jboss-deployment-structure>

              <deployment>

                  <dependencies>

                      <module name="org.postgresql" />

                  </dependencies>

              </deployment>

              <sub-deployment name="project.data-1.0.1-SNAPSHOT.jar">

                  <dependencies>

                      <module name="org.postgresql" />

                  </dependencies>

              </sub-deployment>

          </jboss-deployment-structure>

          ------------------------------------------------------------

          Note the module dependency on the EAR may not be required. It just depends on how your EAR is setup.

           

          Another option would be to add a Dependencies manifest entry. Since you're using Maven you could just use the maven-jar-plugin (since it appears this is a JAR inside an EAR) to create the entry.

          ------------------------------------------------------------

          <plugin>

              <groupId>org.apache.maven.plugins</groupId>

              <artifactId>maven-jar-plugin</artifactId>

              <configuration>

                  <archive>

                      <manifestEntries>

                          <Dependencies>org.postgresql</Dependencies>

                      </manifestEntries>

                  </archive>

              </configuration>

          </plugin>

          ------------------------------------------------------------

          • 2. Re: Cannot access added as module jdbc postgres jar - WildFly 10.1
            wdfink

            Adding maven dependencies with scope=provided will solve the compiler issues, but as you exclude the jar from your application (which is recommended) and use a module you need to declare it.

            For some WildFly will have an automatic detection but DB driver and third party libraries need to be declared explicit like Anup already mentioned.

             

            You can read more about this here

            Class Loading in WildFly - WildFly 10