WildFly deployable datasources

In this tutorial, we’ll walk through the process of deploying a WildFly datasource XML file as part of your Java EE application. WildFly, formerly known as JBoss AS (Application Server), is a popular open-source application server developed by Red Hat. A datasource XML file contains configuration details for database connections, such as database URL, username, password, and driver class.

Step 1: Install the JDBC driver

The simplest way to provide a driver is copying it into the deployments folder of your application server. Supposing we are going to install a MySQL Driver:

cp mysql-connector-java-5.1.18-bin.jar  /usr/wildfly/standalone/deployments                        

Now check that the driver has been successfully deployed on the server logs:

09:32:10,400 INFO  [org.jboss.as.server] (DeploymentScanner-threads - 2) JBAS018559: 
Deployed "mysql-connector-java-5.1.18-bin.jar"

Step 2 Deploy the datasource file

Just fill in an xml file which ends (as we used to do) in -ds.xml, for example this is a mysql-ds.xml which is suitable for MySQL database:

<datasources xmlns="http://www.jboss.org/ironjacamar/schema">
  <datasource jndi-name="java:jboss/datasources/MySqlDB" pool-name="MySQLPool">
      <connection-url>jdbc:mysql://localhost:3306/as7db</connection-url>
      <driver>mysql-connector-java-5.1.18-bin.jar</driver>
      <pool>
          <max-pool-size>30</max-pool-size>
      </pool>
      <security>
          <user-name>root</user-name>
          <password>admin</password>
      </security>
  </datasource>
</datasources>

WildFly should log your successful datasource deployment:

09:37:45,949 INFO  [org.jboss.as.server.deployment] (MSC service thread 1-2) JBAS015876: Starting deployment of "mysql-ds.xml"
09:37:46,001 INFO  [org.jboss.as.connector.subsystems.datasources] (MSC service thread 1-8) JBAS010400: Bound data source [jboss/datasources/MySqlDB]
09:37:46,042 INFO  [org.jboss.as.server] (DeploymentScanner-threads - 2) JBAS018559: Deployed "mysql-ds.xml"

At this point you might wonder if there is any pitfall when using the older -ds.xml approach ? well actually if you “bypass” the management interface and deploy the datasource in the old way, you will not be able to manage it through the CLI or Web admin interface. As you can see from this snapshot, our datasource is not enlisted through the manageable resources of the Web interface

 

jboss as 7 as7 datasource deployment

So you should consider using deployable datasource for development/testing purpose and rather switch to the standard AS7 approach in a production environment.

Deploying the datasource along with your application

You can actually deploy your datasource along with your application, just you used to do in the past.
Here’s a sample Web application Structure which ships with a datasource (in the WEB-INF folder). For other a JAR archive you would rather need copying the datasource in the META-INF folder:

Sample.war
¦   home.xhtml
¦
+---WEB-INF
    ¦   faces-config.xml
    ¦   web.xml
    ¦  

mysql-ds.xml


    ¦
    +---classes
    ¦   ¦
    ¦   +---META-INF
    ¦           MANIFEST.MF
    ¦           persistence.xml
    ¦
    +---lib
            mysql-connector-java-5.1.18-bin.jar

Conclusion

In this tutorial, we’ve covered the process of deploying a WildFly datasource XML file as part of your Java EE application. By configuring the datasource XML file and placing it in the appropriate directory within your application, WildFly will automatically deploy it along with your application. This allows you to manage your datasource configuration in a centralized manner and ensures that it’s always available whenever your application is deployed.