Configure DataSource in JBoss Wildfly

I was very surprised when I tried to add a new DataSource (DS) from The WildFly Web console. It’s simply not working like if you try to add a DS in WebSphere ( Strange! yes, but believe me the WebSphere web console is very complete and well documented)

So quickly to add a new DS you have to follow steps below:
(PostgreSQL DB Example):

Inside the module folder at $WILDFLY_HOME/modules add the following sub folders : org/postgresql/main

inside a main folder put the PostgreSQL driver

create an xml file module.xml and copy past the following :

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

Go to $WILDFLY_HOME/standalone folder

Edit the file standalone.xml,

inside the tag  <datasources> add the flowing:

<datasource jndi-name="java:jboss/datasources/crmlifeDS" pool-name="crmlifeDS" enabled="true" use-java-context="true">
  <connection-url>jdbc:postgresql://localhost:5432/crmlife_db</connection-url>
  <driver>postgresql</driver>
  <security>
     <user-name>crmlife</user-name>
     <password>crmlife</password>
  </security>
</datasource>

inside the tag  <datasources> add the flowing:

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

Start Your server and then test your connection:

wildfly_admin_ds

Advertisements
About

A java Developer interested in Big Data,NoSQL, Functionnal and Dynamic programming

Posted in Java_corner
5 comments on “Configure DataSource in JBoss Wildfly
  1. Vernanio says:

    Why use JBoss way??? Why not use Java EE standard way? data-source element in web.xml That much better no?

    • younss says:

      Can you, please, give an example,
      because in the web descriptor you can refer a datasource but not declare it.

      //Servlet code: 
      javax.sql.DataSource ds = (javax.sql.DataSource) ctx.lookup
                                 ("myDataSource");
      

      web.xml entries:

      <resource-ref>
         <res-ref-name>myDataSource</res-ref-name>
         <res-type>javax.sql.DataSource</res-type>
         <res-auth>CONTAINER</res-auth>
      . . .
      </resource-ref>
      

      In my post I’m declaring a datasource and given it a jndi reference, that’s will help me to refer this jndi name in my application without care about database connection parameters. Suppose that you have to run the same app in many environment. What will you do

    • jacinto oliwonco tutuparacomepony says:

      If you want to develop an application and deploy it in multiple environments it’s easier just specifying a name for the datasource and let each environment define it.

      It’s just a level of abstraction you can use (or not) depending of your needs. Just like defining constants in code or in a separate .properties file.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: