Adding MySQL data source to JBoss 7.X

Today I spent almost 4-5 hours figuring how to configure MySQL for JBoss. Since I am new to JEE world it was a real struggle for me. So hopefully this post will help newbies like me…

  1. Download and extract  JBoss v7.1.1 (Download URL : http://www.jboss.org/jbossas/downloads/)
  2. Download and install MySQL (Download URL : http://dev.mysql.com/downloads/)
  3. Download MySQL JDBC driver (Download URL: http://dev.mysql.com/downloads/connector/j/)

Step 1 – Copy JDBC driver to JBoss folder

  • Extract the downloaded JDBC Driver (mysql-connector-java-5.1.19.zip) which contains source, readme files along with the driver JAR file (mysql-connector-java-5.1.19-bin.jar). For now we will be using only this JAR file.
  • Go to the folder JBOSS_HOME\modules\com and create a folder called mysql and inside mysql create another folder called main.

                 Note: JBOSS_HOME is your JBoss root folder in my case it is d:\D:\jboss-as-7.1.1.Final

  • Copy the driver JAR file (mysql-connector-java-5.1.19-bin.jar) in to this folder (D:\jboss-as-7.1.1.Final\modules\com\mysql\main)
  • Create a XML file called module.xml with the following content
<module xmlns="urn:jboss:module:1.1" name="<strong>com.mysql</strong>"><resources>
<resource-root path="mysql-connector-java-5.1.19-bin.jar"/>
</resources>
<dependencies>
<module name="javax.api"/>
<module name="javax.transaction.api"/>
<module name="javax.servlet.api" optional="true"/>
</dependencies>
</module>

Note: Make sure the module name in XML (marked in bold) follows the directory structure you have created else this won’t work.

Step 2 – Configure MySQL Driver

  • Go to the folder JBOSS_HOME\standalone\configuration and open the standalone.xml
  • Search for the text h2 until you find the datasources configuration which will look like
<datasources>
<datasource jndi-name="java:jboss/datasources/ExampleDS" pool-name="ExampleDS" enabled="true" use-java-context="true">
<connection-url>jdbc:h2:mem:test;DB_CLOSE_DELAY=-1</connection-url>
<driver>h2</driver>
<security>
<user-name>sa</user-name>
<password>sa</password>
</security>
</datasource>
<drivers>
<driver name="h2" module="com.h2database.h2">
<xa-datasource-class>org.h2.jdbcx.JdbcDataSource</xa-datasource-class>
</driver>
</drivers>
</datasources>
  • Add MySQL Driver details to the drivers tag
<driver name="mysql" module="<strong>com.mysql</strong>">
<xa-datasource-class>com.mysql.jdbc.Driver</xa-datasource-class>
</driver>

Note: the module name (marked in bold) should match the module name specified in the module.xml in Step 1.

Step 3 – Configure MySQL DataSource

Add the MySQL Data Store to the datasources tag

<datasource jndi-name="java:jboss/datasources/myds" pool-name="myds" enabled="true" use-java-context="true">
<connection-url>jdbc:mysql://localhost:3306/mydb</connection-url>
<driver><strong>mysql</strong></driver>
<security>
<user-name>root</user-name>
<password>mypassword</password>
</security>
</datasource>

Note: the driver name (marked in bold) should be the same as the driver name specified in Step 2.

The final XML data source tag should look like

<subsystem xmlns="urn:jboss:domain:datasources:1.0">
<datasources>
<datasource jndi-name="java:jboss/datasources/ExampleDS" pool-name="ExampleDS" enabled="true" use-java-context="true">
<connection-url>jdbc:h2:mem:test;DB_CLOSE_DELAY=-1</connection-url>
<driver>h2</driver>
<security>
<user-name>sa</user-name>
<password>sa</password>
</security>
</datasource>
<datasource jndi-name="java:jboss/datasources/myds" pool-name="myds" enabled="true" use-java-context="true">
<connection-url>jdbc:mysql://localhost:3306/mydb</connection-url>
<driver>mysql</driver>
<security>
<user-name>root</user-name>
<password>mypassword</password>
</security>
</datasource>
<drivers>
<driver name="h2" module="com.h2database.h2">
<xa-datasource-class>org.h2.jdbcx.JdbcDataSource</xa-datasource-class>
</driver>
<driver name="mysql" module="com.mysql">
<xa-datasource-class>com.mysql.jdbc.Driver</xa-datasource-class>
</driver>
</drivers>
</datasources>
</subsystem>

Note: Alternatively you can configure your datasource using the admin console by going to http://localhost:9990 and adding the datasource details (http://localhost:9990/console/App.html#datasources)

8755 Total Views 4 Views Today
  • nigel

    great, after searching for ages for this info you helped loads
    Thanks!

  • BC

    The xml you are supplying for both modules.xml and standalone.xml both contain HTML markup. Specifically, module=”com.mysql“>