<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<body link="#355491" alink="#4262a1" vlink="#355491" style="background: #e2e2e2; margin: 0; padding: 20px;">
<div>
        <table cellpadding="0" bgcolor="#FFFFFF" border="0" cellspacing="0" style="border: 1px solid #dadada; margin-bottom: 30px; width: 100%; -moz-border-radius: 6px; -webkit-border-radius: 6px;">
                <tbody>
                        <tr>
                                <td>
                                        <table border="0" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF" style="border: solid 2px #ccc; background: #dadada; width: 100%; -moz-border-radius: 6px; -webkit-border-radius: 6px;">
                                                <tbody>
                                                        <tr>
                                                                <td bgcolor="#000000" valign="middle" height="58px" style="border-bottom: 1px solid #ccc; padding: 20px; -moz-border-radius-topleft: 3px; -moz-border-radius-topright: 3px; -webkit-border-top-right-radius: 5px; -webkit-border-top-left-radius: 5px;">
                                                                        <h1 style="color: #333333; font: bold 22px Arial, Helvetica, sans-serif; margin: 0; display: block !important;">
                                                                        <!-- To have a header image/logo replace the name below with your img tag -->
                                                                        <!-- Email clients will render the images when the message is read so any image -->
                                                                        <!-- must be made available on a public server, so that all recipients can load the image. -->
                                                                        <a href="http://community.jboss.org/index.jspa" style="text-decoration: none; color: #E1E1E1">JBoss Community</a></h1>
                                                                </td>
                                                        </tr>
                                                        <tr>
                                                                <td bgcolor="#FFFFFF" style="font: normal 12px Arial, Helvetica, sans-serif; color:#333333; padding: 20px; -moz-border-radius-bottomleft: 4px; -moz-border-radius-bottomright: 4px; -webkit-border-bottom-right-radius: 5px; -webkit-border-bottom-left-radius: 5px;"><h3 style="margin: 10px 0 5px; font-size: 17px; font-weight: normal;">
Data Source Configuration in AS 7
</h3>
<span style="margin-bottom: 10px;">
modified by <a href="http://community.jboss.org/people/mvecera">Martin Vecera</a> in <i>JBoss AS7 Development</i> - <a href="http://community.jboss.org/docs/DOC-16657">View the full document</a>
</span>
<hr style="margin: 20px 0; border: none; background-color: #dadada; height: 1px;">
<div class="jive-rendered-content"><p><div class="toc" style="border: 1px dashed black; padding: 10px;"><ul><li>
<a class="jive-link-anchor-small" href="#Using_DataSourceDefinition_to_configure_a_DataSource">Using @DataSourceDefinition to configure a DataSource</a>
</li>
<li>
<a class="jive-link-anchor-small" href="#Defining_a_Managed_DataSource">Defining a Managed DataSource</a>
</li>
<ul><li>
<a class="jive-link-anchor-small" href="#Installing_the_JDBC_Driver">Installing the JDBC Driver</a>
</li>
<ul><li>
<a class="jive-link-anchor-small" href="#Installing_a_JDBC_driver_as_a_deployment">Installing a JDBC driver as a deployment</a>
</li>
<ul><li>
<a class="jive-link-anchor-small" href="#Modify_the_JAR">Modify the JAR</a>
</li>
</ul><li>
<a class="jive-link-anchor-small" href="#Installing_a_JDBC_driver_as_a_module">Installing a JDBC driver as a module</a>
</li>
</ul><li>
<a class="jive-link-anchor-small" href="#Defining_the_DataSource_itself">Defining the DataSource itself</a>
</li>
</ul><li>
<a class="jive-link-anchor-small" href="#Links">Links</a>
</li>
</ul></div></p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>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.</p><h1 id="Using_DataSourceDefinition_to_configure_a_DataSource">Using @DataSourceDefinition to configure a DataSource</h1><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>Since Java EE6, the new annotation "@DataSourceDefinition" has existed to allow users to configure a data source directly from within their application.  (Note that this annotation bypasses the management layer and as such it is recommended only for development and testing purposes.)  This annotation requires that a data source implementation class (generally from a JDBC driver JAR) be present on the class path (either by including it in your application, or deploying it as a top-level JAR and referring to it via MANIFEST.MF's Class-Path attribute) and be named explicitly.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><h1 id="Defining_a_Managed_DataSource">Defining a Managed DataSource</h1><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>In order for a data source to be managed by the application server (and thus take advantage of the management and connection pooling facilities it provides), you must perform two tasks.  First, you must make the JDBC driver available to the application server; then you can configure the data source itself.  Once you have performed these tasks you can use the data source via standard JNDI injection.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><h2 id="Installing_the_JDBC_Driver">Installing the JDBC Driver</h2><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>The JDBC driver can be installed into the container in one of two ways: either as a deployment or as a core module.  There are pros and cons to each approach, which will be outlined below.</p><h3 id="Installing_a_JDBC_driver_as_a_deployment">Installing a JDBC driver as a deployment</h3><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>The recommended way to install a JDBC driver into the application server is to simply deploy it as a regular JAR deployment.  The reason for this is that when you run your application server in domain mode, deployments are automatically propagated to all servers to which the deployment applies; thus distribution of the driver JAR is one less thing for administrators to worry about.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><pre class="jive-pre"><code class="jive-code">cp jdbc.jar <as7>/standalone/deployments
</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>Any JDBC 4-compliant driver will automatically be recognized and installed into the system by name and version.  A JDBC JAR is identified using the Java service provider mechaism.  Such JARs will contain a text a file named "META-INF/services/java.sql.Driver", which contains the name of the class(es) of the Drivers which exist in that JAR. </p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>If your JDBC driver JAR is not JDBC 4-compliant, it can be made deployable in one of a few ways.</p><h4 id="Modify_the_JAR">Modify the JAR</h4><p>The most straightforward solution is to simply modify the JAR and add the missing file.  You can do this from your command shell by:</p><ol><li>Change to, or create, an empty temporary directory.</li><li>Create a "META-INF" subdirectory.</li><li>Create a "META-INF/services" subdirectory.</li><li>Create a "META-INF/services/java.sql.Driver" file which contains one line - the fully-qualified class name of the JDBC driver.</li><li>Use the "jar" command-line tool to update the JAR like this:</li></ol><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><pre class="jive-pre"><code class="jive-code">jar -uf jdbc-driver.jar META-INF/services/java.sql.Driver
</code></pre><h3></h3><p><span><span>Please note that you need to correctly setup datasource's driver tag when installing the driver as a deployment (see below in the datasource configuration section).</span></span></p><h3 id="Installing_a_JDBC_driver_as_a_module">Installing a JDBC driver as a module</h3><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>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:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p><span style="font-family: courier new,courier;">jboss-7.0.0.<release>/modules/com/mysql/main</span></p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>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.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>It's fairly straightforward, and is as follows:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p><span style="font-family: courier new,courier;"><?xml version="1.0" encoding="UTF-8"?></span></p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p><span style="font-family: courier new,courier;"><!--</span></p><p><span style="font-family: courier new,courier;">  ~ JBoss, Home of Professional Open Source.</span></p><p><span style="font-family: courier new,courier;">  ~ Copyright 2010, Red Hat, Inc., and individual contributors</span></p><p><span style="font-family: courier new,courier;">  ~ as indicated by the @author tags. See the copyright.txt file in the</span></p><p><span style="font-family: courier new,courier;">  ~ distribution for a full listing of individual contributors.</span></p><p><span style="font-family: courier new,courier;">  ~</span></p><p><span style="font-family: courier new,courier;">  ~ This is free software; you can redistribute it and/or modify it</span></p><p><span style="font-family: courier new,courier;">  ~ under the terms of the GNU Lesser General Public License as</span></p><p><span style="font-family: courier new,courier;">  ~ published by the Free Software Foundation; either version 2.1 of</span></p><p><span style="font-family: courier new,courier;">  ~ the License, or (at your option) any later version.</span></p><p><span style="font-family: courier new,courier;">  ~</span></p><p><span style="font-family: courier new,courier;">  ~ This software is distributed in the hope that it will be useful,</span></p><p><span style="font-family: courier new,courier;">  ~ but WITHOUT ANY WARRANTY; without even the implied warranty of</span></p><p><span style="font-family: courier new,courier;">  ~ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU</span></p><p><span style="font-family: courier new,courier;">  ~ Lesser General Public License for more details.</span></p><p><span style="font-family: courier new,courier;">  ~</span></p><p><span style="font-family: courier new,courier;">  ~ You should have received a copy of the GNU Lesser General Public</span></p><p><span style="font-family: courier new,courier;">  ~ License along with this software; if not, write to the Free</span></p><p><span style="font-family: courier new,courier;">  ~ Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA</span></p><p><span style="font-family: courier new,courier;"><span>  ~ 02110-1301 USA, or see the FSF site: </span><a class="jive-link-external-small" href="http://www.fsf.org" target="_blank">http://www.fsf.org</a><span>.</span></span></p><p><span style="font-family: courier new,courier;">  --></span></p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p><span style="font-family: courier new,courier;"><module xmlns="urn:jboss:module:1.0" name="com.mysql"></span></p><p><span style="font-family: courier new,courier;">  <resources></span></p><p><span style="font-family: courier new,courier;">    <resource-root path="mysql-connector-java-5.1.15.jar"/></span></p><p><span style="font-family: courier new,courier;">  </resources></span></p><p><span style="font-family: courier new,courier;">  <dependencies></span></p><p><span style="font-family: courier new,courier;">    <module name="javax.api"/></span></p><p><span style="font-family: courier new,courier;">  </dependencies></span></p><p><span style="font-family: courier new,courier;"></module></span></p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p><span style="font-family: arial,helvetica,sans-serif;">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. </span></p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p><span style="font-family: arial,helvetica,sans-serif;">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.</span></p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p><span style="font-family: arial,helvetica,sans-serif;">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.</span></p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p><span style="font-family: arial,helvetica,sans-serif;">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.</span></p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><h2 id="Defining_the_DataSource_itself">Defining the DataSource itself</h2><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p><span style="font-family: arial,helvetica,sans-serif;">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:</span></p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p><span style="font-family: arial,helvetica,sans-serif;"><a class="jive-link-external-small" href="http://docs.jboss.org/ironjacamar/userguide/1.0/en-US/html/deployment.html#deployingds_descriptor">http://docs.jboss.org/ironjacamar/userguide/1.0/en-US/html/deployment.html#deployingds_descriptor</a></span></p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p><span style="font-family: arial,helvetica,sans-serif;">In either standalone.xml or domain.xml you add the reference to the MySQL module as follows:</span></p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p><span style="font-family: courier new,courier;"><datasources></span></p><p><span style="font-family: courier new,courier;">   <datasource jndi-name="java:jboss/datasources/MySqlDS" pool-name="MySqlDS"></span></p><p><span style="font-family: courier new,courier;">      <connection-url>jdbc:mysql://localhost:3306/EJB3</connection-url></span></p><p><span style="font-family: courier new,courier;"><strong>         <driver></strong><strong>com.mysql</strong><strong></driver></strong></span></p><p><span style="font-family: courier new,courier;">      <transaction-isolation>TRANSACTION_READ_COMMITTED</transaction-isolation></span></p><p><span style="font-family: courier new,courier;">      <pool></span></p><p><span style="font-family: courier new,courier;">        <min-pool-size>10</min-pool-size></span></p><p><span style="font-family: courier new,courier;">        <max-pool-size>100</max-pool-size></span></p><p><span style="font-family: courier new,courier;">        <prefill>true</prefill></span></p><p><span style="font-family: courier new,courier;">      </pool></span></p><p><span style="font-family: courier new,courier;">      <security></span></p><p><span style="font-family: courier new,courier;">        <user-name>test</user-name></span></p><p><span style="font-family: courier new,courier;">        <password>test</password></span></p><p><span style="font-family: courier new,courier;">      </security></span></p><p><span style="font-family: courier new,courier;">      <statement></span></p><p><span style="font-family: courier new,courier;">        <prepared-statement-cache-size>32</prepared-statement-cache-size></span></p><p><span style="font-family: courier new,courier;">        <share-prepared-statement/></span></p><p><span style="font-family: courier new,courier;">      </statement></span></p><p><span style="font-family: courier new,courier;">    </datasource></span></p><p><span style="font-family: courier new,courier;">    <drivers></span></p><p><span style="font-family: courier new,courier;">      <driver name="com.mysql" module="com.mysql"></span></p><p><span style="font-family: courier new,courier;">        <xa-datasource-class>com.mysql.jdbc.jdbc2.optional.MysqlXADataSource</xa-datasource-class></span></p><p><span style="font-family: courier new,courier;">      </driver><br/></span></p><p><span style="font-family: courier new,courier;">    </drivers></span></p><p><span style="font-family: courier new,courier;"></datasources><br/></span></p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p><span style="font-family: arial,helvetica,sans-serif;">The <datasources> tag can contain multiple datasource and XA datasource definitions.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:</span></p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p><span style="font-family: courier new,courier;">jboss-7.0.0.<release>/domain/configuration/domain.xml or</span></p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p><span style="font-family: courier new,courier;">jboss-7.0.0.<release>/standalone/configuration/standalone.xml</span></p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p><span style="font-family: arial,helvetica,sans-serif;">The other important things to point out, besides the standard JDBC parameters, is the <strong>driver</strong> tag above, and a new section called drivers which is within the "datasources" subsystem in the schema, but below the data sources themselves.</span></p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p><span style="font-family: arial,helvetica,sans-serif;">In the <strong>driver</strong> tag above, you should specify the unique name that you gave the driver definition in the <strong>drivers</strong> section. </span></p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p><span style="font-family: arial,helvetica,sans-serif;">In case you installed driver as a deployment, the <strong>driver</strong> tag in datasource definition should match the deployment file name.<br/></span></p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>As you can see, what we put in this is the actual module name we defined in module.xml under <span style="font-family: courier new,courier;">jboss-7.0.0.<release>/modules/com/mysql/main/module.xml.</span></p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>That's all there is to it.  The MySQL module is attached as an example.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><h1 id="Links"><span style="font-family: arial,helvetica,sans-serif;">Links</span></h1><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><ul><li style="text-align: start;"><span style="font-family: arial,helvetica,sans-serif;"><a class="jive-link-external-small" href="https://docs.jboss.org/author/display/AS7/Admin+Guide#AdminGuide-DataSources">https://docs.jboss.org/author/display/AS7/Admin+Guide#AdminGuide-DataSources</a></span></li><li style="text-align: start;"><span style="font-family: arial,helvetica,sans-serif;"><a class="jive-link-external-small" href="http://docs.jboss.org/ironjacamar/userguide/1.0/en-US/html/deployment.html#deployingds_descriptor">http://docs.jboss.org/ironjacamar/userguide/1.0/en-US/html/deployment.html#deployingds_descriptor</a></span></li></ul><p style="text-align: start;"><span style="font-family: arial,helvetica,sans-serif;"><br/></span></p></div>
<div style="background-color: #f4f4f4; padding: 10px; margin-top: 20px;">
<p style="margin: 0;">Comment by <a href="http://community.jboss.org/docs/DOC-16657">going to Community</a></p>
        <p style="margin: 0;">Create a new document in JBoss AS7 Development at <a href="http://community.jboss.org/choose-container!input.jspa?contentType=102&containerType=14&container=2225">Community</a></p>
</div></td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</div>
</body>
</html>