Skip to main content

How to configure a Datasource in JBoss / WildFly as a Module


JDBC (Java Database Connectivity) is a Java API to connect and execute the query with the database. It is a part of JavaSE (Java Standard Edition). JDBC API uses JDBC drivers to connect with the database. The JDBC driver converts the application code to the database language. This means that if the correct driver is installed, an application can be used with any supported database. 
Install a JDBC Driver as a Module
JDBC drivers can be installed as a core module using the management CLI using the following steps.
1. Download the appropriate JDBC driver from your database vendor.
2. Start the JBoss EAP/WildFly server.
3. Launch the management CLI with--connect argument to connect to the running instance.
$ EAP_HOME/bin/jboss-cli.sh --connect
And you will get the below cli


[standalone@localhost:9990 /]

4. Use the module add management CLI command to add the new core module. Syntax :- module add --name=<MODULE_NAME> --resources=<PATH_TO_JDBC_JAR> --dependencies=<DEPENDENCIES>
[standalone@localhost:9990 /] module add –name=com.mysql --resources=/tmp/mysql-connector-java-5.1.47-bin.jar --dependencies=javax.api,javax.transaction.api

5. Now you can see the datasource module added into the EAP_HOME/modules.


$ ls EAP_HOME/modules/com/mysql/main/
module.xml mysql-connector-java-5.1.47-bin.jar

6. We have added the mysql jar as a module in EAP_HOME/modules and now register the mysql JDBC driver.
Syntax :- /subsystem=datasources/jdbc-driver=<DRIVER_NAME>:add(driver-name=<DRIVER_NAME>,driver-module-name=<MODULE_NAME>,driver-xa-datasource-class-name=<XA_DATASOURCE_CLASS_NAME>, driver-class-name=<DRIVER_CLASS_NAME>)


[standalone@localhost:9990 /] /subsystem=datasources/jdbc-driver=mysql:add(driver-name=mysql,driver-module-name=com.mysql,driver-xa-datasource-class-name=com.mysql.jdbc.jdbc2.optional.MysqlXADataSource, driver-class-name=com.mysql.jdbc.Driver)

7. You can see the datasource module has been registered in standalone.xml into the “<driver>” section of datasources subsystem.


<driver name="mysql" module="com.mysql">
<driver-class>com.mysql.jdbc.Driver</driver-class>
<xa-datasource-class>com.mysql.jdbc.jdbc2.optional.MysqlXADataSource</xa-datasource-class>
</driver>


8. Now add the datasource connection.
Syntax :- /subsystem=datasources/data-source=<DATASOURCE_NAME>:add(jndi-name=java:/<JNDI_NAME>, driver-name=<DRIVER_NAME>, connection-url="jdbc:mysql://<DATABASE_SERVER_NAME>:<DB_PORT>/<DATABASE_NAME>", user-name=<DB_USER>, password=<DB_PASSWORD>)



[standalone@localhost:9990 /] /subsystem=datasources/data-source=mysql_jndi:add(jndi-name=java:/mysql_jndi, driver-name=mysql, connection-url="jdbc:mysql://192.168.122.165:3306/JBTRAVEL", user-name=JBTRAVEL, password=JBTRAVEL)

9. And also the datasource connection has been added in standalone.xml into the “<datasource>” section of datasources subsystem.



<datasource jndi-name="java:/mysql_jndi" pool-name="mysql_jndi">
<connection-url>jdbc:mysql://192.168.122.165:3306/JBTRAVEL</connection-url>
<driver>mysql</driver>
<security>
<user-name>JBTRAVEL</user-name>
<password>JBTRAVEL</password>
</security>
</datasource>

10. Now test the datasource connection.
Syntax :- /subsystem=datasources/data-source=<DATASOURCE_NAME>:test-connection-in-pool


[standalone@localhost:9990 /] /subsystem=datasources/data-source=mysql_jndi:test-connection-in-pool

Comments

Popular posts from this blog

How to configure a Datasource in JBoss / WildFly as a JAR Deployment

JDBC drivers can be installed as a JAR deployment using either the management CLI or the management console. As long as the driver is JDBC 4-compliant, it will automatically be recognized and installed as a JDBC driver upon deployment. 1. Download the appropriate JDBC driver from your database vendor. 2. Start the JBoss EAP/WildFly server. 3. Now most of the drivers coming with JDBC 4-compliant, but in case If the JDBC driver JAR is not JDBC 4-compliant, it can be made deployable using the following steps. i) Create a directory structure META-INF/services on your local system. $ mkdir -p META-INF/services    ii) Create a file inside META-INF/services/java.sql.Driver. $ touch META-INF/services/java.sql.Driver   iii) Add one line in the file to indicate the fully-qualified class name of the JDBC driver. $ echo “com.mysql.jdbc.Driver” > META-INF/services/java.sql.Driv...

Shift your Data into Virtualization

A single approach to data management that allows an application or user to retrieve and manipulate data without knowing any technical details about the data. That approach called Data Virtualization. Data Virtualization is different than traditional virtualization like - VMWare, Hypervisor, KVM, etc. because we already learned how to do virtualization of OS, Hardware and Storage, now time to add some more into virtualization, which is DATA. What is Data Virtualization? Data virtualization is a single window used to describe any approach to data management that allows an application to retrieve and manipulate data without requiring technical details about the data, such as how it is formatted, or where it is physically located. Why use Data Virtualization? Data virtualization promotes efficiency in data usage and processing and accelerates time to market on projects with complex data storage infrastructure. The purpose is to allow data to be accessed without creating extra ...

How to Install JBOSS EAP 7.0.0 on RHEL6.5/CentOS6.5 – a step by step tutorial of INSTALLER Installation

INTRODUCTION In this tutorial, we will demonstrate how to install and start a JBoss EAP 7.0.0 server on RHEL 6.5/CentOS 6.5. We use Oracle JDK 8 for this tutorial. This Tutorial Consists Of The Following Steps: Step 1: Download installer link Step 2: JDK installation and verification Step 3 to Step 14: JBoss EAP 7 installation procedure using INSTALLER Installation Step 15: Start Jboss EAP 7 server Red Hat JBoss EAP 7.0 is based on Wildfly 10 , and provides pre-configured options for features such as high-availability clustering, messaging, and distributed caching. And it is an application server that works as a middleware platform, is built on open standards, and is compliant with the Java EE 7 specification. Step 1: Download the installer from: https://developers.redhat.com/products/eap/download/ Select the EAP 7.0.0 (Developers version) from the list. Click on Installer option within Download column. For Linux/ Windows/Mac...