Thursday, April 12, 2012

Widgets

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


[xml]
<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>

[/xml]

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


[xml]
<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>
[/xml]

  • Add MySQL Driver details to the drivers tag


[xml]
<driver name="mysql" module="<strong>com.mysql</strong>">
<xa-datasource-class>com.mysql.jdbc.Driver</xa-datasource-class>
</driver>
[/xml]
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
[xml]
<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>
[/xml]
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
[xml]
<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>
[/xml]
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)

2 comments:

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

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

    ReplyDelete