Wildfly – Install PostgreSQL JDBC Driver as a Module

Instead of deploying a JDBC driver with the wildfly auto-deploy feature, the driver can be alternatively installed as an module. This is also necessary to be used with XA-Datasources. So it is a recommended way to install the driver as a module. The following section shows how to a jdbc driver module is created.

Creating a Module

To create a module:

  1. Go to WILDFLY_HOME/modules/system/layers/base/ and create the folder org/postgresql/main;
  2. copy the file postgresql jdbc driver jar file  to the new folderWILDFLY_HOME/modules/system/layers/base/org/postgresql/main
  3. create the file module.xml in the same folder with the following content:
        <?xml version="1.0" encoding="UTF-8"?>
        <module xmlns="urn:jboss:module:1.1" name="org.postgresql">
            <resources>
                <resource-root path="postgresql-9.4.1211.jar"/>
            </resources>
            <dependencies>
                <module name="javax.api"/>
                <module name="javax.transaction.api"/>
            </dependencies>
        </module>

The name of the driver file may vary, so make sure you declare exactly the same name in the resource-root tag. At this point, the module is not available yet. Youneed to reference the module as a driver in WildFly configuration with the following jboss-cli command:

[standalone@localhost:9990 /] /subsystem=datasources/jdbc-driver=postgresql:add(
    driver-name=postgresql,
    driver-module-name=org.postgresql,
    driver-class-name=org.postgresql.Driver
)

The command returns {“outcome” => “success”} in case of success. This command resulted in the following part in the configuration file:

    <datasources>
        {...}
        <drivers>
            {...}
            <driver name="postgresql" module="org.postgresql">
                <driver-class>org.postgresql.Driver</driver-class>
            </driver>
        </drivers>
    </datasources>

It makes the JDBC driver module available for the datasource creation. So a datasource definition can now be defined to use the postgresql driver in the following way:

...
 <datasource ....>
 <connection-url>jdbc:postgresql://localhost/....</connection-url>
 <driver>postgresql</driver>
...

 

2 thoughts on “Wildfly – Install PostgreSQL JDBC Driver as a Module

Leave a Reply

Your email address will not be published. Required fields are marked *