August 13, 2012

How to install MySQL DataSource on JBoss EAP 6 and AS 7.

The interior of the JBoss EAP 6 and the community version JBoss AS 7, which EAP version is based on, has changed a lot in the latest version. In this blog I will walk you through how to install a MySQL DataSource, and by doing that I will touch two important differences with the new version of JBoss.
  • The changed classloading to a more OSGI like architecture.
  • The simplification of have only one configuration file.
First we will create a new module. A module in this sense is a package of classes that will be available to all our application through your JBoss node. This is a big differences compared with older JBoss version where all deployed archive where directly available to other deployed application when deployed in the deployment root folder.

You find module in $JBOSS_HOME/modules folder. To create a new module you need to do three things:
  1. Create a folder hierarchy for files.
  2. Copy module jar files.
  3. Create module configuration file – module.xml.
$ mkdir -p $JBOSS_HOME/modules/com/mysql/main
$ cp mysql-connector-java-5.1.19.jar $JBOSS_HOME/modules/com/mysql/mainmysql-connector-java-5.1.19.jar
$ touch $JBOSS_HOME/modules/com/mysql/main/module.xml
Now edit the module.xml file:
<?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.19.jar"/>
    </resources>
    <dependencies>
        <module name="javax.api"/>
    </dependencies>
</module>
Now you are ready to use configure your DataSource. Here I will use the JBoss Standalone and not the Domain configuration. In short the main differences between Module and Standalone JBoss configuration is if you want to reuse your configuration through several JBoss instance you shoud use the Domain configuration approach. In this blog I will use the Standalone configuration, but the differences of doing the other way around is not big.

The configuration of the JBoss can be done in several way: 1. CLI 2. Web interface 3. or manually through editing xml file. Which way you choose is up to you, here I will show the result.

$JBOSS_HOME/standalone/configuration/standalone.xml:
        <subsystem xmlns="urn:jboss:domain:datasources:1.1">
            <datasources>
                ...
                <datasource jndi-name="java:jboss/datasources/YourProjectDS" pool-name="YourProjectDS" enabled="true" use-java-context="true">
                    <connection-url>jdbc:mysql://localhost:3306/YourProjectDB</connection-url>
                    <driver>mysql</driver>
                    <security>
                        <user-name>uid</user-name>
                        <password>pwd</password>
                    </security>
                </datasource>
                <drivers>
                    ...
                    <driver name="mysql" module="com.mysql"/>
                </drivers>
            </datasources>
        </subsystem>

No comments: