AndreyB
AndreyB

Reputation: 55

Wildfly Postgres JDBC module issue

I have postgres jdbc driver configured as module in Wildfly 10. What I am trying to do, is to use that driver as dependency in application that will be deployed on the server - so in application, I mark this dependency as provided (in pom.xml file) but it seems to be not working.

Current configuration:

Wildfly postgres module is added at wildfly-10.1.0.Final\modules\org\postgresql\main where 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.AttributeConverter and following happens:

Scenario 1

When I use 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.priject-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

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 load the same jar two times, and can not cast this object to itself.

Question: Why provided scope of the dependency does not work for manually added (to wildfly) postgres driver? (I guess that would be the solution for me)

Upvotes: 1

Views: 2136

Answers (1)

James R. Perkins
James R. Perkins

Reputation: 17780

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>

Upvotes: 3

Related Questions