How to add a datasource to Wildfly

This tutorial explains 3 methods how to add a datasource to the application server Wildfly from Redhat. If you don’t have Wildfly installed on your machine, you can follow my other posts:


Even if these steps are driver-agnostic, the example will use MySQL Connector (in other words, the JDBC Driver for MySQL) and Wildfly 10.0.0-Final.

For the next steps, you will need:

  • Wildfly installed and started
  • MySQL, MariaDB, PostgreSQL or any datasource compatible with Wildfly
  • The RDMS started and a database created (e.g. database: tutorial)

Notice that all these 3 methods have those steps in common:

  1. Add the JDBC Driver (a.k.a. Connector)
  2. Configure you datasource

Method 1: Add datasource via Admin-Console (the easy method)

1. Add the JDBC Driver

  1. Open the console from the browser (http://localhost:9990/console for a local server)Screen Shot 2016-07-23 at 16.21.50
  2. Then in Deployments, to add to open the wizardScreen Shot 2016-07-23 at 16.25.10
  3. After having clicked next, upload your Connector (i.e. JDBC Driver)Screen Shot 2016-07-23 at 16.26.50Here is the MySQL page to download the driver: MySQL Connector URL
  4. After uploading it, you may want to change the connector name. My recommendation is to name your connector with the provider-name, type and the project’s name that will use it, since your application server my have some projects with different versions of the same connector provider. Screen Shot 2016-07-23 at 16.34.17Note that the name is how the deployment is known to the users and must be unique. The runtime name is how is known to the server. This way you may have multiple names but refering to the same runtime name.
    Do not forget to enable your new deployment! (It should be enabled by default)
  5. Press finish to deploy your connector to your application server.

2. Configure you datasource

  1. Add a new datasource by clicking on ‘Start’ on the right of ‘Create a Datasource’. This will define a datasource to be used by deployed applications.
    Screen Shot 2016-07-23 at 16.48.22
  2. Then select ‘Subsystems’ and ‘Datasources’ and either Non-XA or XA depending of your project type. This tutorial needs a Non-XA datasource. Then click ‘Add’.
    Screen Shot 2016-07-23 at 16.53.52
  3. Then click ‘Add’, choose your datasource (MySQL Datasource for this tutorial) and click next.
    Screen Shot 2016-07-23 at 16.55.30
  4. Give to the attributes name and a JNDI name to your datasource. The JNDI name is like a name in a directory service allowing your programs to discover the data and object. Both name and JNDI name have to be unique. Then click ‘next’.
    Screen Shot 2016-07-23 at 17.00.40
  5. Then select your JDBC Driver you have deployed before. To do that, click on ‘Detected Driver’ and select it and click ‘next’
    Screen Shot 2016-07-23 at 17.27.23
  6. Then add the attributes you will need to connect to your RDMS server.
    Screen Shot 2016-07-23 at 17.53.37
  7. On the next screen, you can easily test your connection. In case of failure, the details will guide you to the solution.
    Screen Shot 2016-07-23 at 18.03.07

Pros / Cons

+ Easy to configure
+ No coding
– Need to use the management console, could be a security issue for a company
– Need to reboot your server to add a datasource.

Method 2: Add datasource via standalone.xml

The file that configures your server is named ‘standalone.xml’ and it is located in your wildfly installation directory under:


Important: The management console is updating the standalone.xml. Hence all we have done in method 1 is actually an abstraction of what we will do here.

1. Add the JDBC Driver

  1. With help of your window manager, go under
  2. Then create a subdirectory for your JDBC Driver representing the package structure of your Connector. So create the subfolder:
  3. Copy your JDBC Driver into the directory you have just created.
  4. Create a XML file in the directory named module.xml.
  5. $ pwd
    $ cd modules/system/layers/base/com/mysql/
    $ touch module.xml
  6. Then copy this into the module.xml you created:
    <?xml version="1.0" encoding="UTF-8"?>
    <module xmlns="urn:jboss:module:1.3" name="com.mysql">
        <resource-root path="mysql-connector-java-5.1.39-bin.jar"/>
        <module name="javax.api"/>
        <module name="javax.transaction.api"/>
  7. Edit the standalone.xml file located:
  8. Find the <drivers> element (after the <datasource> element). Then add this snippet:
     <driver name="mysql" module="com.mysql">
  9. That’s it, the driver has been added to Wildfly. Now let’s configure the datasource.

2. Configure you datasource

  1. Just above the driver snippet, add this datasoure snippet:
    <datasource jta="true" jndi-name="java:/MySqlDS" pool-name="MySqlDS" enabled="true" use-ccm="true">
      <valid-connection-checker class-name="org.jboss.jca.adapters.jdbc.extensions.mysql.MySQLValidConnectionChecker"/>
      <exception-sorter class-name="org.jboss.jca.adapters.jdbc.extensions.mysql.MySQLExceptionSorter"/>

    Make sure that the name of the driver (between the <driver> element are the name given to your driver). You can also configure the username, password and database name.

  2. Restart Wildfly and verify by checking the connection to the database through the mangement console:
    Screen Shot 2016-07-23 at 18.03.07

Pros / Cons

+ Standanlone.xml file can be part of your deployment, which is handy.
– Critical if multiple application run on the same server.
– Need to reboot your server to add a datasource.

Method 3: Add datasource via CLI

CLI stands for Command Line Interface and is a command line management tool for JBoss Application Server. It allows a developer / operator to execute management operations and includes these features:


  • connect to the specific controller or server instance;
  • view the available managed components, their attributes and operations;
  • deploy and undeploy standard packages at runtime in standalone and domain modes;

The two main advantages of the CLI is:

  • You can automate your interactation with Wildfly through a script.
  • You can run this on a running Wildfly server.

The CLI is located at:


1. Add the JDBC Driver

  1. Start the command line interface with the following command:
    ./ --connect controller=
  2. Then install the JDBC jar by loading it into a module.
    module add --name=com.mysql --resources=~/Downloads/mysql-connector-java-5.1.39/mysql-connector-java-5.1.39-bin.jar --dependencies=javax.api,javax.transaction.api

    Important: pay attention to where to add a space, and where not to add a space, this is scripting 😉

2. Configure you datasource

  1. Add the driver to the data-sources subsystem.

    It returns {“outcome” => “success”} in case of success.

  2. Create the datasource that uses the JDBC driver we just installed.
    data-source add --jndi-name=java:/MySqlDS --name=MySqlPool --connection-url=jdbc:mysql://localhost:3306/mysqldb --driver-name=mysql --user-name=root
  3. Test your connection and that’s it.Screen Shot 2016-07-23 at 18.03.07

    Pros / Cons

    + Best option from a dev-ops perspective. A script can be run before deploying an application and the script can be part of your deployment as well.
    + Plays very well with technologies like Docker.
    + Configuration can be changed at runtime! So no reboot required.


byteslounge: container vs application managed entity manager

mastertheboss: configuring a datasource with postgresql and wildfly

Thanks to

  • David Kühner for the technical review

How to add a datasource to Wildfly

How to install WildFly on Windows

Formerly known as JBoss AS or simply JBoss, WildFly is a fantastic full-featured multiplaform open-source and free application server authored by Red Hat.

This quick tutorial will list the steps to install this AS on a Windows machine.

PREREQUISITE: Install the lastest Java Development Kit (JDK)

To be able to run the AS, you will need a compatible JDK (JDK 7 Update 75 or later), if possible the lastest available on the Oracle website.

  1. Go to this link and download the lastest JDK (e.g. jdk-8u91-windows-x64.exe).
  2. Change the installation directory to c:\Java\jdk{your-version} (e.g. jdk1.8.0_91)screen1This should be like:screen2
  3. Create an environment variable JAVA_HOME for your system.
    1. On your keyboard, press windows+break
    2. Click on ‘Advanced System Configuration’
    3. Under ‘Environment variables’, add the variable JAVA_HOME with values ‘c:\Java\jdk{your-version}’screen3
    4. And add it to the Path variablescreen4
    5. You will need to close any command windows that were open before executing a command with this path, since there is no way to reload environment variables from an active command prompt.
    6. To check if the path variable is correctly configured, type ‘javac -version’ in the prompt. This will give you the version of the java compiler you installed.screen5

Installing WildFly 10

  1. Download the lastest stable version of the Enterprise Application Integration (EAI) server from the official website (Link). (E.g.
  2. Extract the zip archive to C:\EAI. Your system will create a brand new folder named ‘wildfly-10.0.0.Final’ under C:\EAI.
  3. Execute the script ‘C:\EAI\wildfly-10.0.0.Final\bin\standalone.bat‘ to set up the bootstrap environment, check the installation and start the server.screen6
  4. After that the server has been started, you should be able to access the web server at the address http://localhost:8080 and to access to the console at http://localhost:8080/console.screen7

Configure WildFly server

Add a user to access to the console

To be able to access the admin console, you will need to add a new user by executing the script ‘add-user.bat’ located under your WildFly installation folder (e.g. c:\EAI\wildfly-10.0.0.Final).

  1. Execute the script ‘add-user.bat’ in a console window.
  2. Add a Management User by just typing ‘enter’ since this is the default option.screen0
  3. Enter the details of the new user to add as described. (e.g. ‘admin’ and ‘1234’ for a local installation).
  4. Revisit the server console at localhost:9990/console and enter your credentials.
  5. Now, you should be able to see thisscreen1
How to install WildFly on Windows