Binding MySql DataSources in Jboss EAP 7.0

来源:互联网 发布:android 创建数据库 编辑:程序博客网 时间:2024/06/08 10:18

Data Source Configuration in AS 7

  • Using @DataSourceDefinition to configure a DataSource
  • Defining a Managed DataSource
    • Installing the JDBC Driver
      • Installing a JDBC driver as a deployment
        • Modify the JAR
      • Installing a JDBC driver as a module
    • Defining the DataSource itself
  • Links

 

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.

Using @DataSourceDefinition to configure a DataSource

 

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.

 

Defining a Managed DataSource

 

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.

 

Installing the JDBC Driver

 

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.

Installing a JDBC driver as a deployment

 

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.

 

  1. cp jdbc.jar <as7>/standalone/deployments  

 

Any JDBC 4-compliant driver will automatically be recognized (JBoss asks the driver if it is Type 4 compliant) and installed into the system by name and version.  A JDBC JAR is identified using the Java service provider mechanism.  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.

 

If your JDBC driver JAR is not JDBC 4-compliant, it can be made deployable in one of a few ways.

 

Note on MySQL driver and JDBC Type 4 compliance: while the MySQL driver (at least up to 5.1.18) is designed to be a Type 4 driver, its jdbcCompliant() method always return false. The reason is that the driver does not pass SQL 92 full compliance tests, says MySQL. Thus, you will need to install the MySQL JDBC driver as a module (see below).

Modify the JAR

The most straightforward solution is to simply modify the JAR and add the missing file.  You can do this from your command shell by:

  1. Change to, or create, an empty temporary directory.
  2. Create a "META-INF" subdirectory.
  3. Create a "META-INF/services" subdirectory.
  4. Create a "META-INF/services/java.sql.Driver" file which contains one line - the fully-qualified class name of the JDBC driver.
  5. Use the "jar" command-line tool to update the JAR like this:

 

  1. jar -uf jdbc-driver.jar META-INF/services/java.sql.Driver  

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).

Installing a JDBC driver as a module

 

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 this directory, you need to define your module with a module.xml file, and copy over 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:

 

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <!--  
  4.   ~ JBoss, Home of Professional Open Source.  
  5.   ~ Copyright 2010, Red Hat, Inc., and individual contributors  
  6.   ~ as indicated by the @author tags. See the copyright.txt file in the  
  7.   ~ distribution for a full listing of individual contributors.  
  8.   ~  
  9.   ~ This is free software; you can redistribute it and/or modify it  
  10.   ~ under the terms of the GNU Lesser General Public License as  
  11.   ~ published by the Free Software Foundation; either version 2.1 of  
  12.   ~ the License, or (at your option) any later version.  
  13.   ~  
  14.   ~ This software is distributed in the hope that it will be useful,  
  15.   ~ but WITHOUT ANY WARRANTY; without even the implied warranty of  
  16.   ~ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU  
  17.   ~ Lesser General Public License for more details.  
  18.   ~  
  19.   ~ You should have received a copy of the GNU Lesser General Public  
  20.   ~ License along with this software; if not, write to the Free  
  21.   ~ Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  
  22.   ~ 02110-1301 USA, or see the FSF site: http://www.fsf.org.  
  23.   -->  
  24.   
  25. <module xmlns="urn:jboss:module:1.0" name="com.mysql">  
  26.   <resources>  
  27.     <resource-root path="mysql-connector-java-5.1.21.jar"/>  
  28.   </resources>  
  29.   <dependencies>  
  30.     <module name="javax.api"/>  
  31.   </dependencies>  
  32. </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.

 

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.

 

Defining the DataSource itself

 

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:

 

<datasources>

   <datasource jndi-name="java:jboss/datasources/MySqlDS" pool-name="MySqlDS">

      <connection-url>jdbc:mysql://localhost:3306/EJB3</connection-url>

         <driver>com.mysql</driver>

      <transaction-isolation>TRANSACTION_READ_COMMITTED</transaction-isolation>

      <pool>

        <min-pool-size>10</min-pool-size>

        <max-pool-size>100</max-pool-size>

        <prefill>true</prefill>

      </pool>

      <security>

        <user-name>test</user-name>

        <password>test</password>

      </security>

      <statement>

        <prepared-statement-cache-size>32</prepared-statement-cache-size>

        <share-prepared-statements/>

      </statement>

    </datasource>

    <drivers>

      <driver name="com.mysql" module="com.mysql">

        <xa-datasource-class>com.mysql.jdbc.jdbc2.optional.MysqlXADataSource</xa-datasource-class>

      </driver>

    </drivers>

</datasources>

 

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:

 

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 thedriver 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 driver tag above, you should specify the unique name that you gave the driver definition in thedrivers section.

 

In case you installed driver as a deployment, thedriver tag in datasource definition should match the deployment file name.

 

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

 

That's all there is to it.  The MySQL module is attached as an example.

 

Links

 

  • https://docs.jboss.org/author/display/AS7/Admin+Guide#AdminGuide-DataSources
  • http://www.ironjacamar.org/doc/userguide/1.1/en-US/html_single/index.html#deployingds_descriptor
0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 猎人荒野的呼唤打中一枪后怎么办 凯恩帝数控超程报警怎么办 凯恩帝数控车床x向超程怎么办 别人问你借账号怎么办 微信成夜间模式怎么办 网络电视突然黑屏了怎么办 1adac连接线坏了怎么办 4g的标志没有了怎么办 苹果手机亏电了怎么办 苹果手机亏电了充不进电怎么办 比值审敛法 ρ=1怎么办 比值审敛法中p=1怎么办 电脑被老友重装系统搞坏了怎么办 vivo手机听筒声音小怎么办 小孩调皮幼儿园不收怎么办 小孩听力残疾幼儿园不收怎么办 幼儿园不收外省小孩怎么办 孩子刚上幼儿园哭闹老师怎么办 孕妇糖耐指标高怎么办 貂蝉经常没蓝怎么办 儿童声导抗c型怎么办 酱牛肉不烂怎么办拯救 卤牛肉一切就碎怎么办 牛肉煮的太烂了怎么办 牛肉卤的太烂怎么办 牛肉炖得太软怎么办 牛肉炖的很硬怎么办 牛肉做的有点硬怎么办 牛肉做老了还能怎么办 牛肉粒 炒老了怎么办 牛肉炖出来很硬怎么办 煮的牛肉太硬怎么办 在产蛋鸡体重偏轻怎么办 产蛋鸡不上高峰怎么办 蛋鸡天热下蛋少怎么办 成年鸡嗉子鼓涨怎么办 黄牛拼失败了钱怎么办 磁力泥粘衣服上怎么办 鼻涕泥弄衣服上怎么办 水晶橡皮泥弄在衣服上怎么办 橡皮泥弄到衣服上怎么办