How To Configure datasource in WildFly

Jboss or Wildfly

Note: Here we have given examples of mysql database and Oracle database connection.

On WildFly, you have two ways of installing the JDBC driver: whether you deploy it as any other application package or you install it as a module. You can always choose to deploy the driver, but it’s specially recommend when you have a cluster environment, since the deployments are automatically propagated in the server groups.

You may have issues with the deployment if the driver is not JDBC4-compliant. In this case, installing the driver as a module solves those issues. The advantage of the JDBC driver as a module is the possibility of creating a custom WildFly bundle for your organization. This way, you can repeat exactly the same installation throughout several machines, preserving the same configuration. This is perfect for the development environment.

Deploying the JDBC Driver

Before creating a datasource we need to deploy the JDBC driver class files to use for connecting to DB. In the admin console (http://localhost:9990/console), go to Runtime > Server > Manage Deployments and click on Add to deploy the MySQL driver. Upload the driver and give a new name to it. Any JDBC4-compliant driver is automatically recognised by WildFly and made available for new datasources. If not using a JDBC4 driver, then click on En/Disable right after the deployment`.

JBoss WildFly Datasouce Creation

JBoss WildFly Datasouce Creation

Click on Add

JBoss WildFly Datasouce Creation

JBoss WildFly Datasouce Creation

Upload the mysql connector jar file which you can download from Mysql Connector

JBoss WildFly Datasouce Creation

JBoss WildFly Datasouce Creation

JBoss WildFly Datasouce Creation

JBoss WildFly Datasouce Creation

Update the name to something like mysql_connector

JBoss WildFly Datasouce Creation

JBoss WildFly Datasouce Creation

Click on Save button.

JBoss WildFly Datasouce Creation

JBoss WildFly Datasouce Creation

Select the mysql_connector and click on En/Disable to enable the deployment.

JBoss WildFly Datasouce Creation

JBoss WildFly Datasouce Creation

Click on confirm.

For creating datasource for Oracle Database also you can just create the same way as of mysql but make sure the driver jar is ojdbc6 jar file which can be downloaded from oracle website.

Example Screenshots:

JBoss WildFly Datasouce Creation

JBoss WildFly Datasouce Creation

JBoss WildFly Datasouce Creation

JBoss WildFly Datasouce Creation

JBoss WildFly Datasouce Creation

JBoss WildFly Datasouce Creation

Once the connector drivers are enabled we can proceed on next steps which are data source creation:

On the admin console:

  • Go to Profile > subsytems > Connector > Datasources and click on Add to create a datasource.
JBoss WildFly Datasouce

JBoss WildFly Datasouce

  • Give a name to the datasource to easily identify it in the console. We use AppDS in our example.
  • Define the JNDI name appending the prefix java:/ to your current datasource name like java:/jdbc/AppDS and click Next.
JBoss WildFly Datasouce Attributes

JBoss WildFly Datasouce Attributes

  • Select the driver you deployed or added as a module and click Next.
JBoss WildFly Datasouce Driver

JBoss WildFly Datasouce Driver

  • Fill in the connection parameters to your database and click Done when finished. For example:
    • Connection URL: jdbc:mysql://localhost:3306/AppDS
    • Username: db_user
    • Password: secret
JBoss WildFly Datasouce Connection strings

JBoss WildFly Datasouce Connection strings

Test the connection to make sure datasource is correct, on successful test the output shall show something like below

JBoss WildFly Datasouce Connection string testing

JBoss WildFly Datasouce Connection string testing

Datasource For Oracle Database:

You can fill the details like below for Oracle DB connectivity

JBoss WildFly Oracle Datasouce attributes

JBoss WildFly Oracle Datasouce attributes

Select the ojdbc6.jar and click on next

JBoss WildFly JDBC driver

JBoss WildFly JDBC driver

Fill the connection details like below screen shot. To connect to Oracle DB we shall use the below connection url: jdbc:oracle:thin:@localhost:1521:orcl

JBoss WildFly Oracle connect string

JBoss WildFly Oracle connect string

Test the connection and it shall show successful like below:

JBoss WildFly Oracle connect string testing

JBoss WildFly Oracle connect string testing

 

We can use CLI for command line configuration of the same. Below is a example on the same

Below is a example to configure datasource using CLI

[[email protected] bin]# ./jboss-cli.sh --connect --controller=localhost:9990

[[email protected]:9990 /] data-source add --name=TesteCliDS --jndi-name=java:/TesteCliDS --driver-name=mysql --connectionurl=jdbc:mysql://localhost:3306/testeCli --user-name=testecli --password=changeme

4. See the logs that confirm that the datasource has been created and enabled as follows:

JBAS010400: Bound data source [java:/TesteCliDS]

5. Now, let’s check the standalone.xml or domain.xml configuration file as follows to see whether the datasource has been created:

<datasourcejndi-name="java:/TesteCliDS" poolname="TesteCliDS" enabled="false">
<connection-url>jdbc:mysql://localhost:3306/testeCli</connection-url>
<driver>mysql</driver>
<security>
<user-name>testecli</user-name>
<password>changeme</password>
</security>
</datasource>

To disable the datasource, use the following command line:

[[email protected]:9990 /] data-source --name=TesteCliDS disable

operation-requires-reload: true
process-state: reload-required

To remove the datasource, just repeat the preceding command and use the remove parameter as follows:

[[email protected]:9990 /] data-source --name=TesteCliDS remove

process-state: reload-required

We can reload the WildFLy server easily through the CLI by running the following command:

[[email protected]:9990 /] reload

Now, if we look at the standalone.xml/domain.xml file, the datasource is no longer present.

This completes datasource creation steps on Jboss WildFly latest version.

In case of any ©Copyright or missing credits issue please check CopyRights page for faster resolutions.

13 Responses

  1. rafael rozon says:

    Thanks a lot, for the post. Saved me a lot of time.

  2. Diego says:

    Thanks so much for your help

  3. ina says:

    Sir this is just great!! Most freshers try using the CLI method and fail. Thanks for the GUI writeup!

  4. warlord says:

    Thank you very much man. This really helped me

  5. Mainak says:

    thanks a lot Ramakanta……. this really helped me to connect to my Oracle DB from Wildfly standalone instance.

    Actually I am doing POC on JBoss-Wildfly. Currently I use WebSphere and want to move to JBoss. So basically I want to clone my WebSphere deployed application to JBoss. Can you suggest your inputs for the queries I asked in the below thread.

    https://developer.jboss.org/thread/267957

  6. Humberto Romero says:

    Works great! Thank you. Tried the same flow with WildFly 10 and worked.

  7. i am getting this error while testin connection jbas010440: failed to invoke operation: jbas010447: connection is not valid

    connection url-jdbc:oracle:thin:@::WEBDB

Leave a Reply