How To Configure datasource in 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`.
Click on Add
Upload the mysql connector jar file which you can download from Mysql Connector
Update the name to something like mysql_connector
Click on Save button.
Select the mysql_connector and click on En/Disable to enable the deployment.
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:
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.
- 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.
- Select the driver you deployed or added as a module and click Next.
- 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
Test the connection to make sure datasource is correct, on successful test the output shall show something like below
Datasource For Oracle Database:
You can fill the details like below for Oracle DB connectivity
Select the ojdbc6.jar and click on next
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
Test the connection and it shall show successful like below:
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
[root@wildfly bin]# ./jboss-cli.sh --connect --controller=localhost:9990 [standalone@localhost: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:
[standalone@localhost: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:
[standalone@localhost: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:
[standalone@localhost: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.
Thanks a lot, for the post. Saved me a lot of time.
Glad it helped! 🙂
Thanks so much for your help
Sir this is just great!! Most freshers try using the CLI method and fail. Thanks for the GUI writeup!
Glad this helped you!!
Thank you very much man. This really helped me
welcome. glad it helped
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
you can use Apache httpd for static content with mod_jk combination.
Works great! Thank you. Tried the same flow with WildFly 10 and worked.
glad to know 🙂
i am getting this error while testin connection jbas010440: failed to invoke operation: jbas010447: connection is not valid
connection url-jdbc:oracle:thin:@::WEBDB
i ahve given my ip and port number,its not visible here