Configurando Datasource Module no JBoss 7

April 12, 2012, Posted in J2EE Comments: 0 Comments and 0 Reactions

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
1 <module xmlns="urn:jboss:module:1.1" name="<strong>com.mysql</strong>"><resources>
2 <resource-root path="mysql-connector-java-5.1.19-bin.jar"/>
3 </resources>
4 <dependencies>
5 <module name="javax.api"/>
6 <module name="javax.transaction.api"/>
7 <module name="javax.servlet.api" optional="true"/>
8 </dependencies>
9 </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
1 <datasources>
2 <datasource jndi-name="java:jboss/datasources/ExampleDS" pool-name="ExampleDS" enabled="true" use-java-context="true">
3 <connection-url>jdbc:h2:mem:test;DB_CLOSE_DELAY=-1</connection-url>
4 <driver>h2</driver>
5 <security>
6 <user-name>sa</user-name>
7 <password>sa</password>
8 </security>
9 </datasource>
10 <drivers>
11 <driver name="h2" module="com.h2database.h2">
12 <xa-datasource-class>org.h2.jdbcx.JdbcDataSource</xa-datasource-class>
13 </driver>
14 </drivers>
15 </datasources>
  • Add MySQL Driver details to the drivers tag
1 <driver name="mysql" module="<strong>com.mysql</strong>">
2 <xa-datasource-class>com.mysql.jdbc.Driver</xa-datasource-class>
3 </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

1 <datasource jndi-name="java:jboss/datasources/myds" pool-name="myds" enabled="true" use-java-context="true">
2 <connection-url>jdbc:mysql://localhost:3306/mydb</connection-url>
3 <driver><strong>mysql</strong></driver>
4 <security>
5 <user-name>root</user-name>
6 <password>mypassword</password>
7 </security>
8 </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

1 <subsystem xmlns="urn:jboss:domain:datasources:1.0">
2 <datasources>
3 <datasource jndi-name="java:jboss/datasources/ExampleDS" pool-name="ExampleDS" enabled="true" use-java-context="true">
4 <connection-url>jdbc:h2:mem:test;DB_CLOSE_DELAY=-1</connection-url>
5 <driver>h2</driver>
6 <security>
7 <user-name>sa</user-name>
8 <password>sa</password>
9 </security>
10 </datasource>
11 <datasource jndi-name="java:jboss/datasources/myds" pool-name="myds" enabled="true" use-java-context="true">
12 <connection-url>jdbc:mysql://localhost:3306/mydb</connection-url>
13 <driver>mysql</driver>
14 <security>
15 <user-name>root</user-name>
16 <password>mypassword</password>
17 </security>
18 </datasource>
19 <drivers>
20 <driver name="h2" module="com.h2database.h2">
21 <xa-datasource-class>org.h2.jdbcx.JdbcDataSource</xa-datasource-class>
22 </driver>
23 <driver name="mysql" module="com.mysql">
24 <xa-datasource-class>com.mysql.jdbc.Driver</xa-datasource-class>
25 </driver>
26 </drivers>
27 </datasources>
28 </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)