Adding a MySQL datasource to JBoss AS 7

This post assumes you are using a Unix like system with a bash shell and have already downloaded and extracted the JBoss AS 7 application server.

Retrieve the MySQL Connector

The connector can be downloaded from: http://dev.mysql.com/downloads/connector/j/. The current version at the time of writing is 5.1.17, further sections of this guide assume that version number.

Unzip the connector to your downloads folder and save the location of the main JAR in a variable which we can refer to later:

cd ~/Downloads
unzip mysql-connector-java-5.1.17.zip
export MYSQL_JAR=~/Downloads/mysql-connector-5.1.17/mysql-connector-java-5.1.17-bin.jar

Add a Module to AS 7

AS 7 uses a module system to provide isolation in class loading. We need to create a new module which contains the MySQL Connector J JAR. Move to the the AS installation directory and create the folder structure for the new module:

export JBOSS_HOME=~/Development/jboss-as-web-7.0.0.Final
cd $JBOSS_HOME
mkdir -p modules/com/mysql/main

Copy the driver jar to the new directory and move to that directory:

cp $MYSQL_JAR $JBOSS_HOME/modules/com/mysql/main
cd $JBOSS_HOME/modules/com/mysql/main

Define the module in XML. This is the key part of the process:

vi module.xml

If the version of the jar has changed, remember to update it here:

<?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.17-bin.jar"/>
  </resources>
  <dependencies>
    <module name="javax.api"/>
  </dependencies>
</module>

The new module directory should now have the following contents:

module.xml
mysql-connector-java-5.1.17-bin.jar

Create a Driver Reference

Now the module has been created, we need to make a reference to it from the main application server configuration file:

cd $JBOSS_HOME/standalone/configuration
vi standalone.xml

Find the ‘drivers’ element and add a new driver to it:

<drivers>
    <driver name="mysql" module="com.mysql"/>
    <driver name="h2" module="com.h2database.h2">
        <xa-datasource-class>
            org.h2.jdbcx.JdbcDataSource
        </xa-datasource-class>
    </driver>
</drivers>

The ‘h2’ driver is part of the default JBoss configuration. The new driver that needs adding is named ‘mysql’.

Add the Datasource

Go into the configuration directory and open the main configuration file:

cd $JBOSS_HOME/standalone/configuration
vi standalone.xml

Find the datasources element and add a new datasource inside:

<datasource
        jndi-name="java:/mydb" pool-name="my_pool"
        enabled="true" jta="true"
        use-java-context="true" use-ccm="true">
    <connection-url>
        jdbc:mysql://localhost:3306/mydb
    </connection-url>
    <driver>
        mysql
    </driver>
    <security>
        <user-name>
            root
        </user-name>
        <password>
 
        </password>
    </security>
    <statement>
        <prepared-statement-cache-size>
            100
        </prepared-statement-cache-size>
        <share-prepared-statements/>
    </statement>
</datasource>

Start the Application Server

Now try running the application server. Make sure you run initially as a user which has write access to the modules directory where you placed the MySQL connector JAR. This is because the appication server seems to generate an index of the directories inside the JAR.

cd $JBOSS_HOME
bin/standalone.xml

Hopefully, amongst the application server’s console output you should see the following:

20:31:33,843 INFO  [org.jboss.as.connector.subsystems.datasources] (MSC service thread 1-1) Bound data source [java:/mydb]

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

20 thoughts on “Adding a MySQL datasource to JBoss AS 7”

  1. Hello i do everything as you say and i have got error :
    New missing/unsatisfied dependencies:
    service jboss.jdbc-driver.mysql (missing)

  2. THANK YOU so much for putting this up. I’ve been hitting a wall for the last 4 hours trying to get JBoss to pick up my driver; all I could find were out of date blog posts from years ago. Pointing out the modules.xml and related configuration is so useful.

  3. hey all, i have the same thing as lukas too:

    New missing/unsatisfied dependencies:
    service jboss.jdbc-driver.mysql (missing)

    What should I do?

    1. Hi Hentor,

      That error message is quite general in saying it could not load the driver. In the new module directory, has JBoss created an index file for the jar? If it has, you should find a file like:


      jboss-as-web-7.0.2.Final/modules/com/mysql/main/mysql-connector-java-5.1.18-bin.jar.index

      Have you tried following the instructions from scratch on a completely fresh install of JBoss to make sure nothing else is causing a problem?

      Dave

    2. Hi, I had the same problem with db2jcc.jar driver.
      I used the jdbc4 compatible db2jcc4.jar and this fix the problem.

  4. For those who got the error: New missing/unsatisfied dependencies: service jboss.jdbc-driver.mysql (missing)

    Maybe you performed the above operations as the root user and so created files and directories that are owned by the root user. Those files can not be modified by the jboss user that runs the jboss process and they cause all kinds of errors.

    If this is true, then change the ownership of the files in jboss directory with:
    chown -R jboss.jboss $JBOSS_HOME

    1. Thank you , Torben. The issue was that the files in my modules section were owned by the root user. I changed them to the jboss user and now it works fine.

  5. The problem for me can’t be resolved.I do everything as supposed to do them.And i get the same error.I was searching around and i found out that i f you have a space at the begging of the module.xml file the module can’t be loaded.But nothing of what mentioned
    everywhere falls in my occasion.When I set up the datasource by installing the jar as a deployment the datasource works perfect.If i setup the jar as module i get the unsatisfied dependencies thing.Anyone manage to solve/find the root of the cause.I have requested information in the jboss fora.

  6. Please help me i am new to java and Jboss .

    18:06:39,842 INFO [org.jboss.as.connector.subsystems.datasources] (MSC service thread 1-1) JBAS010400: Bound data source [java:/mydb]

    After running a test page i got:

    ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/Assets].[jsp]] (http–127.0.0.1-8080-1) Servlet.service() for servlet jsp threw exception: javax.servlet.jsp.JspException: Unable to get connection, DataSource invalid: “java.sql.SQLException: No suitable driver found for java:/mydb”

  7. Works with mysql-connector-java-5.1.18-bin.jar
    Works with mysql-connector-java-5.1.6-bin.jar

    Does not work when you have a typo in module.xml!!!!!! (I did not have -bin in the filename and it failed)

    Thanks!

  8. I just have copied com.h2database, changed the files with mysql, edited the files using ultra edit without converting files to dos format and it works fine the same content as mentioned above

Comments are closed.