Skip to content

WildFly

Connecting WildFly to PostgreSQL and MySQL: driver modules, datasources and common errors.

2 min read

References

* How to use PostgreSQL with WildFly and JBoss

How to connect WildFly to PostgreSQL

Download the PostgreSQL driver

Download a compatible drive with your instance (e.g. PostgreSQL JDBC 4.2 Driver, 42.7.1):

https://jdbc.postgresql.org/download.html

Add PostgreSQL driver to WildFly

In [WILDFLY_HOME]/modules

create the directory:

Terminal window
/org/postgresql/main

and copy the jdbc file.

Some developers suggest creating the structure in *modules/system/layers/*base adding the custom module to the WildFly/JBoss exiting layered modules. This approach is not recommended by RedHat, here you can read an explanation.

Create the module in WildFly

In /org/postgresql/main

create the module.xml file

<?xml version='1.0' encoding='UTF-8'?>
<module xmlns="urn:jboss:module:1.9" name="org.postgresql">
<resources>
<resource-root path="postgresql-42.7.1.jar"/>
</resources>
<dependencies>
<module name="javax.api"/>
<module name="javax.transaction.api"/>
</dependencies>
</module>

At the end you should have a similar structure:

|-- modules
| |-- org
| | `-- postgresql
| | `-- main
| | `-- module.xml
| | `-- postgresql-42.7.1.jar
|-- system

In alternative, you can use the Wildfly CLI:

[WILDFLY_HOME]/bin/jboss-cli.sh

Terminal window
module add --name=org.postgresql --resources=[JDBC_FILE_PATH]postgresql-42.7.1.jar --dependencies=javax.api,javax.transaction.api

Add the datasource

In our case, we use the standalone instance of WildFly.

Open [WILDFLY_HOME]/standalone/configuration/standalone-full.xml

Locate the existing datasources, probably you have an instance of H2 already configured:

<subsystem xmlns="urn:jboss:domain:datasources:7.1">
<datasources>

Add the postgresql datasource, update according to your configuration:

For non-xa datasource:

<datasource jndi-name="java:jboss/datasources/PostgresDS" pool-name="PostgresDS">
<connection-url>jdbc:postgresql://localhost:5432/postgres</connection-url>
<driver>postgresqlDS</driver>
<security user-name="postgres" password="secret"/>
</datasource>

For xa datasource:

<xa-datasource jndi-name="java:jboss/datasources/PostgresXA" pool-name="PostgresXA">
<driver>postgresqlXA</driver>
<xa-datasource-property name="url">jdbc:postgresql://localhost:5432/postgres</xa-datasource-property>
<security user-name="postgres" password="secret"/>
</xa-datasource>

This datasource references a driver named postgresql that we have to add, in the section <datasources><drivers>:

For non-xa datasource:

<driver name="postgresqlDS" module="org.postgresql">
<driver-class>org.postgresql.Driver</driver-class>
</driver>

For xa datasource:

<driver name="postgresqlXA" module="org.postgresql">
<xa-datasource-class>org.postgresql.xa.PGXADataSource</xa-datasource-class>
</driver>

Restart the server and check that the connection worked correctly.

You should see in the log a similar message that confirms the connection to your database.

Terminal window
10:12:15,424 INFO [org.jboss.as.connector.subsystems.datasources] (ServerService Thread Pool -- 44) WFLYJCA0005: Deploying non-JDBC-compliant driver class org.postgresql.Driver (version 42.7)
10:12:15,426 INFO [org.jboss.as.connector.deployers.jdbc] (MSC service thread 1-1) WFLYJCA0018: Started Driver service with driver-name = postgresql

Errors

WFLYJCA0047: Connection is not valid

The inclusion of the datasource-class in your configuration e.g.

<datasource-class>org.postgresql.ds.PGSimpleDataSource</datasource-class>

could throw the following error: WFLYJCA0047: Connection is not valid

It turns out after setting “Connection Properties” instead of “Connection URL”. Only then WildFly connects to the correct database server.

Connection Properties

<subsystem xmlns="urn:jboss:domain:datasources:7.1">
<datasources>
<datasource jndi-name="java:jboss/datasources/hydrads" pool-name="hydrads">
<connection-url>jdbc:postgresql://localhost:5432/dbhydra</connection-url>
<driver>postgresqlds</driver>
<security user-name="hydra" password="P@ssw0rd1"/>
</datasource>
<xa-datasource jndi-name="java:jboss/datasources/hydraxa" pool-name="hydraxa">
<driver>postgresqlxa</driver>
<xa-datasource-property name="url">jdbc:postgresql://localhost:5432/dbhydra</xa-datasource-property>
<xa-pool>
<min-pool-size>10</min-pool-size>
<max-pool-size>20</max-pool-size>
<prefill>true</prefill>
</xa-pool>
<security user-name="hydra" password="P@ssw0rd1"/>
</xa-datasource>
<drivers>
<driver name="postgresqlds" module="org.postgresql">
<driver-class>org.postgresql.Driver</driver-class>
</driver>
<driver name="postgresqlxa" module="org.postgresql">
<xa-datasource-class>org.postgresql.xa.PGXADataSource</xa-datasource-class>
</driver>
</drivers>
</datasources>
</subsystem>

How to connect WildFly to MySQL

Install MySQL JDBC Connector

Create if not exist and then go to the directory WILDFLY_HOME/modules/system/layers/base/com/mysql/driver/main. Download MySQL JDBC Connector, for example mysql-connector-java-8.0.21.jar, and then copy it to the directory. Create module.xml file

<?xml version="1.0" encoding="UTF-8"?>
<module xmlns="urn:jboss:module:1.8" name="com.mysql.driver">
<resources>
<resource-root path="mysql-connector-java-8.0.21.jar" />
</resources>
<dependencies>
<module name="javax.api" />
<module name="javax.transaction.api" />
</dependencies>
</module>

Adding DataSource

There are three methods to add datasource to WildFly.

Admin Console

TODO:

standalone.xml

TODO:

CLI

TODO:

WildFly Micro Maven Plugin

TODO:

Related articles

3 min read

Jakarta

Jakarta EE namespaces, project settings and what changed in Jakarta EE 10 and 11.

  • Java & Jakarta EE
8 min read

JSF

Jakarta Faces (JSF) notes: WebSocket push, scopes, events, Ajax, security and cross-field validation.

  • Java & Jakarta EE
  • Web & Frontend
2 min read

Jasper Reports

JasperReports tips: internationalization, pagination, split types and floating elements.

  • Java & Jakarta EE

Type to search the whole site.