Using JPA 2.1 With Wildfly To Access a MySQL Database On Ubuntu Server

  Java, JBoss, JPA, Tutorial, Wildfly

Define a Datasource In Wildfly

Contents

Usually the application will not directly interact with the JDBC driver. Instead, the underlying runtime (Wildfly) provides access to a database through a datasource which provides an efficient way to share and manage a discrete connection (or pool of connections).DataSources have a few advantages over JDBC connections. Most notably, DataSources usually have a global scope within your application server, which means that the administrator creates DataSources to the enterprise information systems and therefore frees the application software developers from knowing about the connections details. A change in the database connection is easy because only the DataSource needs to be changed.

Creating a DataSource in Wildfly is easy. What can get a little complicated is the configuration of the JDBC driver. Wildfly offers two ways of JDBC driver configuration:

  1. Deployment – The recommended way to install a JDBC driver into WildFly is to deploy it as a regular JAR deployment.  The reason for this is that when you run WildFly in domain mode, deployments are automatically propagated to all servers to which the deployment applies; thus distribution of the driver JAR is one less thing for you to worry about!Any JDBC 4-compliant driver will automatically be recognized and installed into the system by name and version. See this to learn more about using non JDBC 4-compliant drivers.
  2. JDBC Driver as a Core Module – For better start up server behavior, install the JDBC driver as a core module.  Please note that the module path is different in JBoss AS7. In Wildfly, it is <WILDFLY_HOME>/modules/system/layers/base/mysql/mysql-connector-java/main.

We go for the JDBC Driver as a core module. See this for some background information about the required data. The following is the sequence of steps to configure the core module:

  1. Download the mysql-driver mysql-connector-java-5.1.22.jar from any Maven Repository e.g.: http://repo1.maven.org/maven2/mysql/mysql-connector-java/6.0.2/mysql-connector-java-6.0.2.jar
  2. Create the MySQL module path: <WILDFLY_HOME>/modules/system/layers/base/mysql/mysql-connector-java/main
  3. Copy  the mysql-driver mysql-connector-java-5.1.22.jar to the directory you just created.
  4. Add a file module.xml with the following content:
    <?xml version="1.0" encoding="UTF-8"?>
    <module xmlns="urn:jboss:module:1.1" name="mysql.mysql-connector-java">
      <resources>
        <resource-root path="mysql-connector-java-5.1.22.jar"/>
        <!-- Insert resources here -->
       </resources>
       <dependencies>
         <module name="javax.api"/>
         <module name="javax.transaction.api"/>
         <module name="javax.servlet.api" optional="true"/>
       </dependencies>
    </module>

5. Add Driver and DataSource elements in Wildfly standalone.xml configuration file:

The Wildfly configuration file is located at <WILDFLY_HOME>/standalone/configuration/standalone.xml.  Edit this file by locating the DataSource configuration (find <subsystem xmlns="urn:jboss:domain:datasources:2.0">) and adding the required sections as below:

<datasources>
   <datasource
      jndi-name="java:jboss/datasources/mysqlDS"
      pool-name="mysqlDS"
      enabled="true"
      use-java-context="true">
      <connection-url>jdbc:mysql://localhost:3306/wildfly?useUnicode=yes&amp;characterEncoding=UTF-8</connection-url>
      <driver>com.mysql</driver>
      <transaction-isolation>TRANSACTION_READ_COMMITTED</transaction-isolation>
      <pool>
         <min-pool-size>10</min-pool-size>
         <max-pool-size>100</max-pool-size>
         <prefill>true</prefill>
      </pool>
      <security>
         <user-name>wildfly</user-name>
         <password>wildflypwd</password>
      </security>
      <statement>
         <prepared-statement-cache-size>32</prepared-statement-cache-size>
         <share-prepared-statements>true</share-prepared-statements>
      </statement>
   </datasource>
   <drivers>
      <driver name="com.mysql" module="mysql.mysql-connector-java">
         <xa-datasource-class>com.mysql.jdbc.jdbc2.optional.MysqlXADataSource</xa-datasource-class>
      </driver>
   </drivers>
</datasources>

Please note that the connection-url in the XML above refers to a locally installed MySQL server. If your MySQL instance is running on another server, you need to specify at least it’s IP address with port number (default 3306) here. Also don’t forget to specify the correct credentials, and ensure that the database is encoded in UTF-8. Then restart Wildfly.

Alternatively, you could use the Wildfly CLI to create these entries:

/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)

Even more simple would be to use the Wildfly admin web app.  Presuming the Wildfly instance is running you can login to your administration portal, and the datasource administration specifically at;

 http://localhost:9990/console/App.html#datasources

LEAVE A COMMENT