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:
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
1
2
3
4
5
6
7
8
9
10
<?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:
m
s
o
y
d
s
u
t
l
o
`
e
e
r
-
m
s
g
-
p
`
o
-
s
-
t
g
m
`
`
r
a
-
-
e
i
-
-
s
n
q
m
p
l
o
o
d
s
u
t
l
g
e
r
.
e
x
s
m
q
l
l
-
4
2
.
7
.
1
.
j
a
r
In alternative, you can use the Wildfly CLI:
[WILDFLY_HOME]/bin/jboss-cli.sh
1
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:
1
2
<subsystem xmlns="urn:jboss:domain:datasources:7.1" >
<datasources>
Add the postgresql datasource, update according to your configuration:
For non-xa datasource:
1
2
3
4
5
<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:
1
2
3
4
5
<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:
1
2
3
<driver name="postgresqlDS" module="org.postgresql" >
<driver-class>org.postgresql.Driver</driver-class>
</driver>
For xa datasource:
1
2
3
<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.
1
2
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.
1
<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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<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
1
2
3
4
5
6
7
8
9
10
<?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: