Adding the MySQL JDBC driver into Wildfly
Out of the box, Wildfly only comes with an H2 JDBC driver. Most likely, you have a different database such as Oracle, MS SQL, or MySQL. As long as you have access to Wildfly, installing the appropriate JDBC driver is fairly straightforward. The advantage of installing it into Wildfly itself is that you can enforce a specific JDBC driver version for all of your deployed applications. However, if you are deploying an application to a clustered Wildfly, it could be better to deploy the JDBC driver along with your application.
All of Wildfly’s “extensions” are located in WILDFLY_HOME/modules/system/layers/base. If you navigate to this folder, you should see package folders. These package folders correspond to the package of the extension. Let’s install the MySQL JDBC driver, so go ahead and create a com/mysql folder tree. Once there, you’ll need to create another folder, called “main”. Within this folder, you will need to copy the JDBC JAR file (Be sure to grab the latest driver!) and create a module.xml file with the following content:
<module xmlns="urn:jboss:module:1.5" name="com.mysql"> <resources> <resource-root path="mysql-connector-java-8.0.12.jar" /> </resources> <dependencies> <module name="javax.api"/> <module name="javax.transaction.api"/> </dependencies> </module>
Once that is done, you should have a folder that looks like this:
Now, we need to tell the server about the new JDBC driver. Open the configuration file you’re using (e.g. WILDFLY_HOME/standalone/configuration/standalone.xml), and navigate to the “<drivers>” section. You should see a reference to the H2 JDBC driver here. Add the following block after the “<driver>” entry for H2:
<driver name="mysql" module="com.mysql"> <driver-class>com.mysql.cj.jdbc.Driver</driver-class> <xa-datasource-class>com.mysql.cj.jdbc.MysqlXADataSource</xa-datasource-class> </driver>
After you’ve added that, bounce your server and you should see the following messages in the log file to let you know that the driver was loaded:
[org.jboss.as.connector.subsystems.datasources] (ServerService Thread Pool -- 36) WFLYJCA0005: Deploying non-JDBC-compliant driver class com.mysql.jdbc.Driver (version 5.1) [org.jboss.as.connector.deployers.jdbc] (MSC service thread 1-8) WFLYJCA0018: Started Driver service with driver-name = mysql
Don’t worry about the “non-compliant” message. It will still work.
That’s it! You should now be able to create a MySQL datasource.
If you’re wondering how to install JDBC drivers for other databases, the steps are very similar. The problem you’ll run into is figuring out what the driver-class, xa-datasource-class, driver name, and module should be. The easiest way to figure it out is to look at the DataSourceTemplates.java file in the official Wildfly Admin console source code. The list of drivers it contains is not exhaustive, but it does include all of the major ones.
Edit (Sept/2018): I’ve updated the module.xml file to use the new Driver and XA DataSource classes. These are, from my understanding, compatible with MySQL 5.x+.