Detailed instructions for configuring a PostgreSQL datasource in JBoss EAP 8.0 running in domain mode.
Prerequisites
- JBoss EAP 8.0 installed and running in domain mode
- PostgreSQL server running
Step 1: Download PostgreSQL JDBC Driver
First, you need to obtain the PostgreSQL JDBC driver:
- Visit the official PostgreSQL JDBC driver download page: https://jdbc.postgresql.org/download/
- Download the appropriate version matching your PostgreSQL server.
- Transfer the JAR file to your JBoss server.
Alternatively, you can download directly from the server using wget:
wget https://jdbc.postgresql.org/download/postgresql-42.7.5.jar -O /root/postgresql-42.7.5.jar
Note: Download the jar file on each node in the domain.
Step 2: Start JBoss EAP in Domain Mode
Before configuring the datasource, ensure JBoss EAP is running in domain mode:
$JBOSS_HOME/bin/domain.sh --host-config=host-master.xml
# Or to run in the background
nohup $JBOSS_HOME/bin/domain.sh --host-config=host-primary.xml > domain.log 2>&1 &
Make sure both worker1 and worker2 hosts are started and connected to the domain controller.
Step 3: Connect to JBoss CLI
Open a terminal and connect to the JBoss CLI:
$JBOSS_HOME/bin/jboss-cli.sh --connect
If connecting to a remote controller:
$JBOSS_HOME/bin/jboss-cli.sh --connect --controller=master:9990
Step 4: Add PostgreSQL JDBC Driver Module
Add the PostgreSQL JDBC driver as a module in JBoss EAP:
module add --name=com.postgresql --resources=/root/postgresql-42.7.5.jar --dependencies=wildflyee.api,java.se,java.xml,java.xml.crypto,jdk.xml.dom,jakarta.transaction.api
This creates a new module named com.postgresql with the necessary dependencies.
Step 5: Register the JDBC Driver
Register the PostgreSQL driver with the JBoss subsystem:
/profile=full/subsystem=datasources/jdbc-driver=postgresql:add(driver-name=postgresql,driver-module-name=com.postgresql,driver-xa-datasource-class-name=org.postgresql.xa.PGXADataSource)
Step 6: Verify Server Status
Before proceeding, check the status of your domain servers:
/host=worker1/server-config=server-one:read-resource
/host=worker2/server-config=server-one:read-resource
/server-group=main-server-group:read-resource
These commands provide information about server configurations and ensure they’re properly registered in the domain and their profiles.
Step 7: Remove Existing PostgreSQL Datasource
If you need to replace an existing datasource with the same name:
/profile=full/subsystem=datasources/data-source=PostgresDS:remove
Step 8: Add the PostgreSQL Datasource
Create the PostgreSQL datasource with proper configuration:
/profile=full/subsystem=datasources/data-source=PostgresDS:add(jndi-name=java:jboss/PostgresDS,driver-name=postgresql,connection-url=jdbc:postgresql://192.168.0.170:5432/jboss,user-name=jboss,password=jboss@123,validate-on-match=true,background-validation=false,valid-connection-checker-class-name=org.jboss.jca.adapters.jdbc.extensions.postgres.PostgreSQLValidConnectionChecker,exception-sorter-class-name=org.jboss.jca.adapters.jdbc.extensions.postgres.PostgreSQLExceptionSorter)
This command configures:
- JNDI Name: java:jboss/PostgresDS (used by applications to look up the datasource).
- Connection URL: Points to the PostgreSQL server and database “jboss”.
- Credentials: Username “jboss” with password “jboss@123”.
- Validation: Includes PostgreSQL-specific connection validators to ensure connection health.
Step 9: Restart Servers to Apply Changes
Restart the servers to apply the configuration:
/host=worker1/server-config=server-one:restart
/host=worker2/server-config=server-one:restart
Step 10: Test the Connection
Test the database connection:
/host=worker1/server=server-one/subsystem=datasources/data-source=PostgresDS:test-connection-in-pool
/host=worker2/server=server-one/subsystem=datasources/data-source=PostgresDS:test-connection-in-pool
A successful test will return:
{
"outcome" => "success",
"result" => [true]
}
Step 11: Accessing the Datasource in Applications
In your Java EE applications, you can access the datasource using:
@Resource(lookup = "java:jboss/PostgresDS")
private DataSource dataSource;
Or in your persistence.xml for JPA:
<persistence-unit name="MyPU" transaction-type="JTA">
<jta-data-source>java:jboss/PostgresDS</jta-data-source>
<!-- ... -->
</persistence-unit>
Conclusion
Here, we have configured a PostgreSQL datasource in JBoss EAP 8.0 domain mode. Applications deployed to the domain can now access the PostgreSQL database using the JNDI name java:jboss/PostgresDS.