Tag Archives: mysql

MySQL Datasource: JNDI configuration in JBoss AS 7

This post covers the steps to configure a MySQL datasource in JBoss Application Server 7. This datasource can then be referenced via JNDI to connect your application to the underlying Database. I presume you already have a JBoss AS 7 up and running in your development environment. If not, do that first.

Download the MySQL JDBC database driver

  1. Download the latest JDBC driver for MySQL from http://dev.mysql.com/downloads/connector/j/
  2. Save the downloaded zip file and extract the connector jar.

Add the MySQL connector Driver as a Module in Server

  1. Goto: <JBoss-AS-7-Home>/modules/com
  2. Create folders mysql/main and copy the above downloaded JAR file(mysql-connector-java-5.1.21-bin.jar) in it.
  3. So your folder structure should now look like:<JBoss-AS-7-Home>/modules/com/mysql/main/mysql-connector-java-5.1.21-bin.jar
  4. Now that the Driver/Connector JAR is present, we need to define it for the Application Server.
  5. Goto: <JBoss-AS-7-Home>/modules/com/mysql/main folder where you copied the JAR file and create another file module.xml
  6. Paste the following code into it and save.
<?xml version="1.0" encoding="UTF-8"?>
<module xmlns="urn:jboss:module:1.0" name="com.mysql">
    <resource-root path="mysql-connector-java-5.1.21-bin.jar"/>
    <module name="javax.api"/>

Since I’m using mysql-connector-java-5.1.21-bin.jar version so I’ve kept the <resource-root path = “mysql-connector-java-5.1.21-bin.jar”/>  in case you have a different version then give the same in the path attribute above, highlighted in the code-snippet.

Add the Driver reference in the Server

  1. Goto: <JBoss-AS-7-Home>/standalone/configuration/standalone.xml
  2. In the standalone.xml file add the following line in the <drivers> node.
<driver name="mysql" module="com.mysql"/>

This configures the support for a MySQL datasource in your JBoss Application server.

If you wish you can add your datasource in the standalone.xml file itself. But I prefer to keep the datasource file in my Application.

Creating the Datasource

  1. Create an xml file your-datasource.xml under your WEB-INF folder.
  2. This is a sample datasource file. my-sample-ds.xml

<?xml version="1.0" encoding="UTF-8"?>

<datasources xmlns="http://www.jboss.org/ironjacamar/schema"
 xsi:schemaLocation="http://www.jboss.org/ironjacamar/schema http://docs.jboss.org/ironjacamar/schema/datasources_1_0.xsd">
 <!-- The datasource is bound into JNDI at this location. We reference
 this in META-INF/persistence.xml -->
 <datasource jndi-name="java:jboss/datasources/your-ds"
 pool-name="sample-pool" enabled="true"
 use-java-context="true" jta="true">

Referencing the datasource in your application

  1. Now that we have everything, we just need to refer this in our application.
  2. We can do this from persistence.xml if you’re using JPA standards.
  3. This is a sample persistence.xml which I keep under META-INF directory.

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"
 xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 <persistence-unit name="primary">

 <!-- Properties for Hibernate -->
 <property name="hibernate.show_sql" value="true" />
 <property name="hibernate.hbm2ddl.auto" value="update" />
 <property name="hibernate.use_sql_comments" value="true" />
 <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />



I used Hibernate above as my default JPA provider.

So this was all about creating and configuring a MySQL datasource in JBoss AS 7.