Setting PostgreSQL Datasource in JBOSS EAP 6.4 & 7.0

Hi, I just want to sharing about how to create datasource in JBOSS EAP 6.4 and 7.0. The reason why I write this is quite simple, I have certain Spring application which deployed in JBOSS EAP 6.4, when I’m gonna move this application on the new application server which JBOSS EAP 7.0 it was failed. Finally, I found a certain difference in the part of Datasource Setting and Jboss Deployment Structure file.

Setting Datasource in JBOSS EAP 6.4

Ok now basically my datasource setting in JBOSS Eap 6.4 like this :

<datasource jndi-name="java:jboss/datasources/myDS" pool-name="myDS" enabled="true" use-java-context="true">
      <connection-url>jdbc:postgresql://localhost:5432/postgre_db</connection-url>
      <driver>postgresql</driver>
      <pool>
           <min-pool-size>2</min-pool-size>
           <max-pool-size>20</max-pool-size>
           <prefill>true</prefill>
     </pool>
     <security>
          <user-name>postgres</user-name>
          <password>mypassword</password>
     </security>
</datasource>

You can go to standalone.xml in $jboss_home/standalone/configuration directory, in the section datasources you can insert above code. And also you need to describe what kind jdbc driver that you use, in this example I use postgresql. So the jdbc driver declaration will be like this :

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

in then same file (standalone.xml) you can scroll down slowly and you will find drivers section, and you can insert above code on that drivers section

And please ensure that you have added postgre jdbc driver on jboss module, by creating directory on the $jboss_home/modules. there are 3 directories that you need to create in it which is org/postgresql/main so the directory hierarchy should be $jboss_home/modules/org/postgresql/main

.

On that main directory you need to add jdbc driver jar, and you also need to create main.xml file. the main.xml file will content will be like this :

<?xml version="1.0" encoding="UTF-8"?>
<module xmlns="urn:jboss:module:1.1" name="org.postgresql">
  <resources>
    <!-- jar file name can be different, depends on version you gonna use -->
    <resource-root path="postgresql-9.4-1200-jdbc41.jar"/> 
  </resources>
  <dependencies>
    <module name="javax.api"/>
    <module name="javax.transaction.api"/>
  </dependencies>
</module>

In the spring application side, you can call this JNDI source by the following code :

    @Bean
    public DataSource dataSource() {
        final JndiDataSourceLookup dsLookup = new JndiDataSourceLookup();
        dsLookup.setResourceRef(true);
        DataSource dataSource = dsLookup.getDataSource("java:jboss/datasources/myDS");
        return dataSource;
    }

And you also need to create jboss-deployment-structure.xml inside webapp/WEB-INF directory

<?xml version="1.0" encoding="UTF-8"?>
<jboss-deployment-structure>
    <deployment>
        <exclude-subsystems>
            <subsystem name="jpa" />
        </exclude-subsystems>
        <exclusions>
            <!-- WFCORE-209 workaround -->
            <module name="javaee.api" />
            <module name="javax.persistence.api" />
            <module name="org.hibernate" />
        </exclusions>
        <dependencies>
        <!-- http://stackoverflow.com/questions/31706871/using-jpa-2-1-in-eap-6-4-0 -->
            <!-- WFCORE-209 workaround -->
            <module name="javax.activation.api" export="true"/>
            <module name="javax.annotation.api" export="true"/>
            <module name="javax.ejb.api" export="true"/>
            <module name="javax.el.api" export="true"/>
            <module name="javax.enterprise.api" export="true"/>
            <module name="javax.enterprise.deploy.api" export="true"/>
            <module name="javax.inject.api" export="true"/>
            <module name="javax.interceptor.api" export="true"/>
            <module name="javax.jms.api" export="true"/>
            <module name="javax.jws.api" export="true"/>
            <module name="javax.mail.api" export="true"/>
            <module name="javax.management.j2ee.api" export="true"/>
            <!-- <module name="javax.persistence.api" export="true"/> -->
            <module name="javax.resource.api" export="true"/>
            <module name="javax.rmi.api" export="true"/>
            <module name="javax.security.auth.message.api" export="true"/>
            <module name="javax.security.jacc.api" export="true"/>
            <module name="javax.servlet.api" export="true"/>
            <module name="javax.servlet.jsp.api" export="true"/>
            <module name="javax.transaction.api" export="true"/>
            <module name="javax.validation.api" export="true"/>
            <module name="javax.ws.rs.api" export="true"  services="export"/>
            <module name="javax.xml.bind.api" export="true"/>
            <module name="javax.xml.registry.api" export="true"/>
            <module name="javax.xml.soap.api" export="true"/>
            <module name="javax.xml.ws.api" export="true"/>

            <!-- This one always goes last. -->
            <module name="javax.api" export="true"/>
        </dependencies>
    </deployment>
</jboss-deployment-structure>

Setting Datasource in JBOSS EAP 7.0

I made modification setting to make my spring application can be run in JBOSS EAP 7.0.

Datasource part in standalone.xml

<datasource jndi-name="java:jboss/myDS" pool-name="myDS">
        <connection-url>jdbc:postgresql://localhost:5432/postgre_db</connection-url>
        <security>
             <user-name>postgres</user-name>
             <password>mypassword</password>
        </security>
        <validation>
            <valid-connection-checker class-name="org.jboss.jca.adapters.jdbc.extensions.postgres.PostgreSQLValidConnectionChecker"/>
            <validate-on-match>true</validate-on-match>
            <background-validation>false</background-validation>
            <exception-sorter class-name="org.jboss.jca.adapters.jdbc.extensions.postgres.PostgreSQLExceptionSorter"/>
         </validation>
</datasource>

in driver part

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

in jboss module part, i change jdbc driver version, therefore main.xml should be changed

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

And the last, in jboss-deployment-structure.xml I comment javax.enterprise.deploy.api and javax.xml.registry.api

<?xml version="1.0" encoding="UTF-8"?>
<jboss-deployment-structure>
	<deployment>
		<exclude-subsystems>
			<subsystem name="jpa" />
		</exclude-subsystems>
		<exclusions>
			<!-- WFCORE-209 workaround -->
			<module name="javaee.api" />
			<module name="javax.persistence.api" />
			<module name="org.hibernate" />
		</exclusions>
		<dependencies>
			<!-- http://stackoverflow.com/questions/31706871/using-jpa-2-1-in-eap-6-4-0 -->
			<!-- WFCORE-209 workaround -->
			<module name="javax.activation.api" export="true" />
			<module name="javax.annotation.api" export="true" />
			<module name="javax.ejb.api" export="true" />
			<module name="javax.el.api" export="true" />
			<module name="javax.enterprise.api" export="true" />
			<!-- disabled at jboss eap 7.0 <module name="javax.enterprise.deploy.api" export="true" /> -->
			<module name="javax.inject.api" export="true" />
			<module name="javax.interceptor.api" export="true" />
			<module name="javax.jms.api" export="true" />
			<module name="javax.jws.api" export="true" />
			<module name="javax.mail.api" export="true" />
			<module name="javax.management.j2ee.api" export="true" />
			<!-- <module name="javax.persistence.api" export="true"/> -->
			<module name="javax.resource.api" export="true" />
			<module name="javax.rmi.api" export="true" />
			<module name="javax.security.auth.message.api" export="true" />
			<module name="javax.security.jacc.api" export="true" />
			<module name="javax.servlet.api" export="true" />
			<module name="javax.servlet.jsp.api" export="true" />
			<module name="javax.transaction.api" export="true" />
			<module name="javax.validation.api" export="true" />
			<module name="javax.ws.rs.api" export="true" services="export" />
			<module name="javax.xml.bind.api" export="true" />
			<!-- disabled at jboss eap 7.0  <module name="javax.xml.registry.api" export="true" /> -->
			<module name="javax.xml.soap.api" export="true" />
			<module name="javax.xml.ws.api" export="true" />
			
			<!-- This one always goes last. -->
			<module name="javax.api" export="true" />


		</dependencies>
	</deployment>
</jboss-deployment-structure>

And this solution is worked for me, happy coding and thanks !!!

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>