Wednesday, April 9, 2014

Adding a MySQL datasource to JBoss AS 7.1.1

Prerequisites:
JBoss AS 7.1.1
mysql-connector-java-your_version-bin.jar

My Environment:
Ubuntu 12.04
java version "1.6.0_45"

1. Extract the JBoss server and I call it "JBOSS_HOME" here onwards.
2. Create "com/mysql/main" directory structure in JBOSS_HOME/modules directory.
3. Copy mysql-connector-java--bin.jar to the above created directory.
4. Create a file called module.xml which has following content in the same directory. Update the jar version accordingly.

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






















The created directory structure including files is as depicted above.

5. Modify JBOSS_HOME/standalone/configuration/standalone.xml as follows:
    Add mysql datasource and the driver configs as given below under datasources tag.

<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 jta="true" jndi-name="java:/jboss/datasources/dsJaasProject" pool-name="my_pool" enabled="true" use-java-context="true" use-ccm="true">
    <connection-url>jdbc:mysql://localhost:3306/jaasProject</connection-url>
    <driver>mysql</driver>
    <security>
        <user-name>root</user-name>
        <password>root</password>
    </security>
    <statement>
        <prepared-statement-cache-size>100</prepared-statement-cache-size>
        <share-prepared-statements>true</share-prepared-statements>
    </statement>
</datasource>
<drivers>
    <driver name="mysql" module="com.mysql">
        <xa-datasource-class>com.mysql.jdbc.jdbc2.optional.MysqlXADataSource</xa-datasource-class>
    </driver>
    <driver name="h2" module="com.h2database.h2">
        <xa-datasource-class>org.h2.jdbcx.JdbcDataSource</xa-datasource-class>
    </driver>
</drivers>
</datasources>
                   
 6. Now start the server from JBOSS_HOME/bin directory as follows:
             ./standalone.sh

You would notice that there will be a file called "mysql-connector-java-your_version-bin.jar.index" generated inside JBOSS_HOME/module/com/mysql/main directory.

1 comment:

Anonymous said...

I really appreciate information shared above. It’s of great help. If someone want to learn Online (Virtual) instructor led live training in JBoss, kindly contact us http://www.maxmunus.com/contact
MaxMunus Offer World Class Virtual Instructor led training on JBoss. We have industry expert trainer. We provide Training Material and Software Support. MaxMunus has successfully conducted 100000+ trainings in India, USA, UK, Australlia, Switzerland, Qatar, Saudi Arabia, Bangladesh, Bahrain and UAE etc.
For Demo Contact us.
Nitesh Kumar
MaxMunus
E-mail: nitesh@maxmunus.com
Skype id: nitesh_maxmunus
Ph:(+91) 8553912023
http://www.maxmunus.com/