Configure JBoss and PostgreSQL jdbc datasource

This post is about adding PostgreSQL driver and data source to JBoss Application Server. 
Used versions are: JBoss AS 7.1.1, PostgreSQL 9.1.10 and postgresql-9.3-1100.jdbc41 driver.

1. Download the driver

Download postgresql jdbc driver here http://jdbc.postgresql.org/download.html

2. Add driver module to JBoss AS

Create postgresql/main subfolder in existing folder $JBOSS_HOME/modules/org. Copy downloaded driver to newly created $JBOSS_HOME/modules/org/postgresql/main folder. Add there module.xml file:

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

In xml above just replace <resource-root path="postgresql-9.3-1100.jdbc41" /> with your postgresql driver file name

3. Edit standalone.xml

Add driver element to <datasources><drivers>  section in  $JBOSS_HOME/standalone/configuration/standalone.xml:

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

Add data source element to <datasources> section:

<datasource jndi-name="java:jboss/datasources/PostgreSQL-DS" pool-name="MyPostgreSQLDataSource" enabled="true" jta="true" use-java-context="true" use-ccm="true">
   <connection-url>jdbc:postgresql://localhost:5432/my_database_name_here</connection-url>
   <driver>postgresql</driver>
   <security>
      <user-name>my_user_name_here</user-name>
      <password>my_password_here</password>
   </security>
</datasource>

 

4.  Check the result. 

Restart JBoss AS and look into the log.   Record like "Bound data source [java:jboss/datasources/PostgreSQL-DS]" should be present, error messages otherwise. Errors may be caused by wrong values in driver or datasource sections, incorrect user name, password, bad file name in in module.xml etc.
Finally let's go to JBoss AS admin console http://localhost:9990/console/App.html#datasources and find our postgresql data source. Select it and press Connection – Test Connection for final check.

Leave a Reply

Your email address will not be published.