Pages

Tuesday, June 25, 2013

JBoss 7 : Data Source Configuration

Configuring a Data Source in JBoss 7 is different when compared with the JBoss 5 version. In this article we will see how we can configure a Data Source for My Sql Database. Here are the sequences of steps,

1.Configure the Module for the My Sql Data base.
As we are aware that JBoss 7 is based on the modular class loading model. for this we need to create a new module for the my Sql Connector Jars.

Download the My Sql connector jars.
Create a Folder in mysql in /modules/system/layers/base/com and create main directory in mysql like

/modules/system/layers/base/com/mysql/main

Now copy the my Sql jar file to the /modules/system/layers/base/com/mysql/main location.
Once we copied create a file called “module.xml” and add contents

<?xml version="1.0" encoding="UTF-8"?>
<module xmlns="urn:jboss:module:1.0" name="com.mysql">
<resources>
<resource-root path="mysql-connector-java-5.1.25-bin.jar"/>
</resources>
<dependencies>
<module name="javax.api"/>
</dependencies>
</module>

This is the module.xml file which tells about the modules to be loaded and also their dependencies. This file exists beside the jar file.

The new module directory should now have the following contents:

module.xml
mysql-connector-java-5.1.17-bin.jar

2.Create a Driver Reference

Now the module has been created, we need to make a reference to it from the main application server configuration file:

cd $JBOSS_HOME/standalone/configuration/
vi standalone-full.xml

Find the ‘drivers’ element and add a new driver to it:

<drivers>
<driver name="mysql" module="com.mysql"/>
<driver name="h2" module="com.h2database.h2">
<xa-datasource-class>org.h2.jdbcx.JdbcDataSource</xa-datasource-class>
</driver>
</drivers>

mySql Driver is now added to the main Configuration File.

3.Create the Datasource
Once the Driver reference is done , the next step is to configure the Data Source.

Go into the configuration directory and open the main configuration file:

cd $JBOSS_HOME/standalone/configuration
vi standalone-full.xml

in the file , find <datasources> element and we can see <datasource> element under this.we can add our data Source like

<datasource jndi-name="java:/mysqlDB" pool-name="my_pool" enabled="true" jta="true" use-java-context="true" use-ccm="true">
<connection-url> jdbc:mysql://localhost:3306/test </connection-url>
<driver> mysql </driver>
<security>
<user-name>root</user-name>
<password> </password>
</security>
<statement>
<prepared-statement-cache-size>
100
</prepared-statement-cache-size>
<share-prepared-statements/>
</statement>
</datasource>

once the Data Source is added. save and re start the Server.

4.Is that Deployed?
We can check the server logs to make sure that the Data Source is bounded. We can see the logs like

15:50:27,058 INFO [org.jboss.as.connector.subsystems.datasources] (MSC service thread 1-4) JBAS010400: Bound data source [java:/mysqlDB]

This Clearly says the mySqlDB is bound.

We can see similar message when we un deploy the Data Source like


16:00:41,993 INFO [org.jboss.as.connector.subsystems.datasources] (MSC service thread 1-1) JBAS010409: Unbound data source [java:/mysqlDB]