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…
- Download and extract JBoss v7.1.1 (Download URL : http://www.jboss.org/jbossas/downloads/)
- Download and install MySQL (Download URL : http://dev.mysql.com/downloads/)
- 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"/> |
5 |
<module name="javax.api"/> |
6 |
<module name="javax.transaction.api"/> |
7 |
<module name="javax.servlet.api" optional="true"/> |
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
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> |
6 |
<user-name>sa</user-name> |
7 |
<password>sa</password> |
11 |
<driver name="h2" module="com.h2database.h2"> |
12 |
<xa-datasource-class>org.h2.jdbcx.JdbcDataSource</xa-datasource-class> |
- 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> |
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"> |
3 |
<driver><strong>mysql</strong></driver> |
5 |
<user-name>root</user-name> |
6 |
<password>mypassword</password> |
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"> |
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> |
7 |
<user-name>sa</user-name> |
8 |
<password>sa</password> |
11 |
<datasource jndi-name="java:jboss/datasources/myds" pool-name="myds" enabled="true" use-java-context="true"> |
13 |
<driver>mysql</driver> |
15 |
<user-name>root</user-name> |
16 |
<password>mypassword</password> |
20 |
<driver name="h2" module="com.h2database.h2"> |
21 |
<xa-datasource-class>org.h2.jdbcx.JdbcDataSource</xa-datasource-class> |
23 |
<driver name="mysql" module="com.mysql"> |
24 |
<xa-datasource-class>com.mysql.jdbc.Driver</xa-datasource-class> |
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)