JBoss Community

Data Source Configuration in AS 7

created by Andrig Miller in JBoss AS7 Development - View the full document

In older versions of the application server, data source configuration was tied to a *-ds.xml file schema that you would deploy in the deploy directory of your configuration.  In AS 7, the entire structure of the AS is different, and as you would expect, creating your own data sources is different as well.

 

There are two ways in which you can create your own data sources, but this article will focus on creating one, using MySQL as the example, using the new AS 7 module system.  In AS 7, the application server services themselves are no longer tied into the deployment.  If you look at the directory structure, you will actually see an empty "deployments" folder, after you unzip the archive.  That's because all of the AS services are now modules.  In fact, you can see what used to be called the DefaultDS, and used Hypersonic, is now a module, and its the H2 database.  You can see that in the modules directory directly under the jboss-7.0.0.<release> directory, where <release> is the binary release you downloaded, whether the current Beta, or future Beta's, CR's or the Final release.  So, let's talk about how we can create our own data source as a module, and then reference that in the configuration.

 

Under the root directory of the application server, is a directory called modules (e.g. jboss-7.0.0.<release>/modules).  In this example, I will create the MySQL module in the same tree as the H2 database.  The H2 database, which comes preconfigured, like the old DefaultDS with Hypersonic, is under the com/h2database/h2 directory, under the modules directory.  So, the first step is to create a directory structure simlar to that for MySQL.  I created, under com, a mysql directory, plus a main directory.  So, at this point it should like like the following:

 

jboss-7.0.0.<release>/modules/com/mysql/main

 

Under the main directory, you need to define your module with a module.xml file, and the actual jar file that contains your database driver.  In my case, the mysql-connector-java-5.1.15.jar file.  This is in contrast to putting the database driver jar file in the old lib directory under your configuration where you deployed your *-ds.xml file.  Also, the jar file must have a META-INF/services/java.sql.Driver file.  This is due to the way AS 7 will load the driver.  Fortunately, the MySQL JDBC driver jar file has this.  So, what's the content of the module.xml file.

 

It's fairly straightforward, and is as follows:

 

<?xml version="1.0" encoding="UTF-8"?>

 

<!--

  ~ JBoss, Home of Professional Open Source.

  ~ Copyright 2010, Red Hat, Inc., and individual contributors

  ~ as indicated by the @author tags. See the copyright.txt file in the

  ~ distribution for a full listing of individual contributors.

  ~

  ~ This is free software; you can redistribute it and/or modify it

  ~ under the terms of the GNU Lesser General Public License as

  ~ published by the Free Software Foundation; either version 2.1 of

  ~ the License, or (at your option) any later version.

  ~

  ~ This software is distributed in the hope that it will be useful,

  ~ but WITHOUT ANY WARRANTY; without even the implied warranty of

  ~ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU

  ~ Lesser General Public License for more details.

  ~

  ~ You should have received a copy of the GNU Lesser General Public

  ~ License along with this software; if not, write to the Free

  ~ Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA

  ~ 02110-1301 USA, or see the FSF site: http://www.fsf.org.

  -->

 

<module xmlns="urn:jboss:module:1.0" name="com.mysql">

  <resources>

    <resource-root path="mysql-connector-java-5.1.15.jar"/>

  </resources>

  <dependencies>

    <module name="javax.api"/>

  </dependencies>

</module>

 

As you can see from above, we give the module name, which in this example is com.mysql, which matches the directory structure we had created under the modules directory.  If we had followed exactly the way the H2 database module was created we would have created something like com/mysqldatabase/mysql, and in that case the module name would have been com.mysqldatabase.mysql.  I chose a shorted name in this case, but its just a matter of personal preference.

 

Besides the module name, we need to tell it where the implementation is, which is the resource-root tag with the path element.  In that path element, we simply put the jar name.  The path appears to be relative, and default to the main directory under the directory structure you created, which of course is com/mysql in our case.

 

Finally, you define any dependencies you might have.  In this case, as the case with all JDBC data sources, we would be dependent on the Java JDBC API's, which in this case in defined in another module called javax.api, which you can find under modules/javax/api/main as you would expect.

 

That's really all there is to creating the module, but it will not be started as a service by AS 7, unless its referenced in the configuration.  Now, just like everything else in AS 7, configuration is now completely different.  There are two main configurations.

 

The first configuration is called domain, and has a domain directory under the root directory of the AS 7 distribution.  This is a configuration that is geared toward multiple server instances and multiple server installations.  The second is standalone, which is geared for a single instance of the server running on a single server, as you would expect.  In regards to data source configuration, there really is no difference, as the datasource schema definition is the same in both cases.  So regardless of which one you may be using in your particular case, the configuration is the same.  For reference you can see the data source information here:

 

http://docs.jboss.org/ironjacamar/userguide/1.0/en-US/html/deployment.html#deployingds_descriptor

 

In either standalone.xml or domain.xml you add the reference to the MySQL module as follows:

 

                 <datasource jndi-name="java:/MySqlDS" pool-name="MySqlDS" enabled="true" use-java-context="true">

                    <connection-url>

                        jdbc:mysql://localhost:3306/EJB3

                    </connection-url>

                    <driver-class>

                        com.mysql.jdbc.Driver

                    </driver-class>

                    <module>

                        com.mysql.jdbc.Driver#5.1

                    </module>

                    <transaction-isolation>

                        TRANSACTION_READ_COMMITTED

                    </transaction-isolation>

                    <pool>

                        <min-pool-size>

                            200

                        </min-pool-size>

                        <max-pool-size>

                            300

                        </max-pool-size>

                        <prefill>

                            true

                        </prefill>

                        <use-strict-min>

                            false

                        </use-strict-min>

                    </pool>

                    <security>

                        <user-name>

                            test

                        </user-name>

                        <password>

                            test

                        </password>

                    </security>

                    <validation>

                        <validate-on-match>

                            false

                        </validate-on-match>

                        <background-validation>

                            false

                        </background-validation>

                        <useFastFail>

                            false

                        </useFastFail>

                    </validation>

                    <statement>

                        <prepared-statement-cache-size>

                            100

                        </prepared-statement-cache-size>

                        <share-prepared-statements/>

                    </statement>

                </datasource>

 

As you can see from above this looks remarkable similar to the old *-ds.xml file, and in fact, I carried over all the attributes I had in my old data source definition.  The thing to point out here, is that this is within an outer tag called <datasources>, so obviously there can be more than one, just like before, but unlike before, they are all included in the one configuration file, either standalone.xml or domain.xml depending on which you are using.  In each case the directory structure is as follows:

 

jboss-7.0.0.<release>/domain/configuration/domain.xml or

 

jboss-7.0.0.<release>/standalone/configuration/standalone.xml

 

The other important things to point out, besides the standard JDBC parameters, is the module tag above, and a new section called drivers which is within the "datasources" subsystem in the schema, but below the data sources themselves.

 

In the module tag above, you should specify the fully qualtified name of the JDBC driver class, appended with a pound sign (#), with the major and minor version number.  In my example above its 5.1, since I am using the 5.1.15 driver from MySQL.  It does not support the micro version number, so if you are tempted, as I was, to add it, it will cause your module to fail loading, which will then cascade to your application using this data source.  So, then the drivers tag.

 

            <drivers>

                <driver module="com.mysql"/>

            </drivers>

 

As you can see, what we put in this is the actual module name we defined in module.xml under jboss-7.0.0.<release>/modules/com/mysql/main/module.xml.

 

That's all there is to it.  I have also attached a zip archive of the MySQL module that I created, plus my standalone.xml file, that you can use to make your own.  If you want to create a MySQL module and data source, you can simply unzip the com.mysql.tar.gz under the modules directory of your AS 7 distribution, and cut and past the datasource tag in standalone.xml into your configuration, and of course change the database you are connecting to, plus the username and password, and other relevant parameters to suit your needs, and you will have a working MySQL data source.


Comment by going to Community

Create a new document in JBoss AS7 Development at Community