Installing MySQL JDBC driver into Wildfly

Goal

To install a MySQL JDBC driver into Wildfly

Description

This recipe explains the basic steps one needs to do in order to install MySQL JDBC driver into Wildfly application server and how to make it available for use in a given application.

How to

  1. The first thing we need to do is create the module.xml file for the driver. In order to do so, create the following folders com/mysql/main inside $WILDFLY_HOME/modules/system/layers/base (we will be using Wildfly 10.1 but it is or should be similar to other versions of Wildfly) and add a file named module.xml inside of the main folder with the following content:
    <?xml version="1.0" encoding="UTF-8"?>
    <module xmlns="urn:jboss:module:1.0" name="com.mysql">
      <resources>
        <resource-root path="mysql-connector-java-5.1.21.jar"/>
      </resources>
      <dependencies>
        <module name="javax.api"/>
        <module name="javax.transaction.api"/>
      </dependencies>
    </module>
    
  2. Next, copy the corresponding MySQL JDBC driver version into the same main folder, in this case, a file named mysql-connector-java-5.1.21.jar. In this recipe, we will be using version 5.1.21 of the driver but the previous steps are similar in other versions of the driver. Just make sure that the attribute path in the module.xml file matches the name of the file containing the MySQL driver.
  3. Now, edit your standalone.xml file in $WILDFLY_HOME/standalone/configuration to register the previously installed driver:
    ...
      <drivers>
        <driver name="mysql" module="com.mysql">
          <xa-datasource-class>
            com.mysql.jdbc.jdbc2.optional.MysqlXADataSource
          </xa-datasource-class>
        </driver>
      </drivers>
    ...
    

Explanations

Adding the driver to the classpath of Wildfly as a module and registering the driver’s datasource class makes it available to be used at runtime in any application that needs/uses MySQL JDBC driver as its own persistent store. Assuming that we would be using JPA in a project, we could define a dependency on a JTA datasource such as:

<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
version="2.1">
  <persistence-unit name="pu" transaction-type="JTA">
    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
    <jta-data-source>java:/jdbc/pu</jta-data-source>
    ...
  </persistence-unit>
</persistence>

And all we would need to configure our application in the Wildfly installation would be define the corresponding datasource in the standalone.xml file, such as the following:

...
  <datasources>
    <xa-datasource jndi-name="java:/jdbc/pu" pool-name="pupool" enabled="true" use-java-context="true">
      <xa-datasource-property name="ServerName">localhost</xa-datasource-property>
      <xa-datasource-property name="DatabaseName">mydb</xa-datasource-property>
      <driver>mysql</driver>
      <security>
        <user-name>root</user-name>
        <password>*****</password>
      </security>
      <validation>
        <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"/>
      </validation>
    </xa-datasource>
  </datasources>
...
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s