How to add a datasource to Red Hat EAP 7.2
Environment:
In this article, I used a machine with Linux Fedora 29.
To complete this tutorial you will need the following packages:
- Red Hat EAP 7.2 installed and running
- A running installation of MariaDB v 15.1 or an equivalent version of MySQL
Introduction
This tutorial will show you how to include the connection with a MariaDB database in the Red Hat EAP 7.2. Even this tutorial is focused in MariaDB, and is totally compatible with MySQL, you can use it to guide you to include any other database in the EAP. In some steps you will see some information specific to the database, but ever information include a reference to you check the same information related to other databases, like the connection URL or driver.
You can add the datasource using the Web Console of the EAP and the command line interface (CLI), how some servers don’t have a graphical interface, and you can’t use the Web Console inside it, this tutorial use only the CLI to interact with the EAP.
If you have any questions, please contact me using the comments on this post or using any other way.
Download Driver
The first step, before opening the EAP or do any changes in the database, you need the Driver of the database. This driver will be used by the EAP to know how to communicate with the database. Each database has a different way to do this communication and to be more easy to application servers, like EAP, these databases have their Drivers. Go ahead and download the latest version of the MySQL connection driver. We will download the MySQL driver because it is the driver used by MariaDB.
Go to https://www.mysql.com/products/connector/ and download the JDBC Driver for MySQL (Connector/J)
Include the driver in a temporary path and extract the content, you can use unzip command to that
$ tar -xzvf mysql-connector-java-8.0.17.tar.gz
In my case I am using the version 8.0.17, but you can use a different version, only replace the version on the file name.
To find the full list of drivers used by EAP to others database, please take a look on this documentation
Add module
To add a module in the EAP by CLI you need to open the CLI without connect to the EAP, to do that follow the steps below
$ cd $EAP_HOME/bin $ ./jboss-cli.sh $ module add --name=org.mariadb --resources=/home/roribeir/Downloads/mysql-connector-java-8.0.17/mysql-connector-java-8.0.17.jar --dependencies=javax.api,javax.transaction.api
Note: The $EAP_HOME is the path to the Red Hat EAP 7.2 installation and the /home/roribeir/Downloads is the path that I used to extract the MySQL connector.
What is this command and theis params? The command module is used to manage the modules of EAP, look at the description of the options:
| Option | Desciption |
| ADD | The option that show what you want to do wiht the module. |
| –name | Define the name of the module, normally is used the fully qualified name of the driver |
| –resources | The path to the driver |
| –dependencies | Define the dependence that the driver needs to work |
Start the server
The next steps to configure the datasource uses the CLI connected to the EAP server, because of that we need to start the EAP server with the command below, execute it in another terminal window.
$ cd $EAP_HOME/bin $ ./standalone.sh
Add JDCB driver
First we need to connect our running CLI with the EAP server, to do that only run the command connect
$ connect
Now we will add the jdbc driver to the datasources subsystem of the EAP using the CLI connected.
$ /subsystem=datasources/jdbc-driver=mariadb:add(driver-module-name=org.mariadb, driver-name=mariadb)
Explain the command
In the CLI of the EAP to you use any subsystem functionality, you need to use the path to the subsystem (/subsystem) and to their functionality. In this case we are using the datasources subsystem (/subsystem=datasources). How we are adding a jdbc driver, we need to use the jdbc-driver functionality of the datasources subsystem (/subsystem=datasources/jdbc-driver). The table below shows the options of the functionality
| Option | Description |
| jdbc-driver=mariadb | How you can have more than one jdbc driver in the same EAP, when you use this functionality you need to define the jdbc driver name, in this case the name is mariadb, but you can use the name you prefer. |
| :add | This is what you can do with the jdbc driver |
| driver-module-name | Is the name of the module previous included in the EAP |
| driver-name | Is the name of the driver that you are including, this need to be the same name as included in the jdbc-driver=mariadb |
Add datasource
The last step to add the datasource. In the CLI connected, run the following command:
$ data-source add --name=MariaDBDS --jndi-name=java:jboss/database-name --driver-name=mariadb --connection-url=jdbc:mysql://localhost:3306/database-name --user-name=user --password=password --validate-on-match=true --background-validation=false --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
On this command I will explain only the options that you need to change, the other, only copie like as it.
| Option | Description |
| –name=MariaDBDS | Define the name to this datasource, you can define the name that you want. |
| –jndi-name= java:jboss/database-name | This option define the JNDI name used to connect with this datasource, the JNDI name is used by your application, that will be included in the persistence.xml of your war application. Explain what is used this option and how to configure it in an application is out of the scope of this post. |
| –driver-name=mariadb | The name of the driver that we create previos in this tutorial |
| –connection-url= jdbc:mysql://localhost:3306/database-name | The url to EAP connect to the Database, the jdbc:mysql://localhost:3306/ is the default connection url of any MySQL or MariaDB database, if you do any configuration change in your database that changes this default url, use your custom url instead of this. The database-name is the name of the database that the EAP will connect. |
Note: To see all options, for other databases, look the documentation of JBoss EAP here
Test connection
After these steps, the database is configured, but to check if it is ok, you can do a connection test using the command below
$ /subsystem=datasources/data-source=MariaDBDS:test-connection-in-pool
Conclusion
In this tutorial you learned how to:
- The basic of the EAP CLI use
- How to add a module on EAP 7.2 using CLI
- How to add a JDBC driver on EAP 7.2 using CLI
- How to add a datasource on EAP 7.2 using CLI
- How to test a datasource connection
