JBoss AS 7 数据源(Data Source)配置 - 如何制作mysql的JBoss module并配置MySql数据源
如何制作mysql的JBoss module并配置MySql数据源
本文详细描述了如何制作 mysql的JBoss Module以及配置MySql数据源。本文大部分内容适应于两个版本的JBoss AS:jboss-as-7.1.1.Final和jboss-as-7.2.0.Final。在必要的时候,我们会分别描述不能同时适应这2个版本的JBoss AS的那部分内容。
本文中,jboss-as-7.1.1.Final和jboss-as-7.2.0.Final的安装路径分别为:
D:\java\jboss-as-7.1.1.Final
D:\java\jboss-as-7.2.0.Final
参考:
JBoss AS 7 数据源(Data Source)配置
1. 如何制作mysql的JBoss module
1.1 获取 mysql 二进制驱动jar包从
www.mysql.com 下载并解压 mysql 驱动包(本文为:mysql-connector-java-5.1.21.zip,本站下载:
链接),把其中的二进制驱动jar包找出来,本文为:mysql-connector-java-5.1.21-bin.jar。
1.2 在JBoss AS中创建mysql module的目录对于JBoss AS 7.1.1,请创建子目录:D:\java\jboss-as-7.1.1.Final\modules\com\mysql\main
对于JBoss AS 7.2.0,请创建子目录:D:\java\jboss-as-7.2.0.Final\modules\system\layers\base\com\mysql\main
我们在下面的描述中用 ${mysql_module_main} 代表mysql module目录。
1.3 把mysql 驱动jar拷贝到${mysql_module_main}目录
把 1.1 中得到的
mysql-connector-java-5.1.21-bin.jar 文件拷贝到 ${mysql_module_main} 目录中。
1.4 创建 mysql module的描述文件 module.xml在 ${mysql_module_main}目录下,新建一个叫做 module.xml 的文件
(a) 对于JBoss AS 7.1.1,其内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<module xmlns="urn:jboss:module:1.0" name="com.mysql">
<resources>
<resource-root path="mysql-connector-java-5.1.21-bin.jar"/>
</resources>
<dependencies>
<module name="javax.api"/>
</dependencies>
</module>
(b) 对于JBoss AS 7.2.0,其内容如下:
<?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.21-bin.jar"/>
</resources>
<dependencies>
<module name="javax.api"/>
</dependencies>
</module>
注意红色的jboss module版本信息和蓝色的mysql 驱动文件名。
1.5 制作mysql驱动索引文件在 ${mysql_module_main} 目录下,创建一个空文件,名字为:驱动jar包 加上 ".index"后缀,本文中,该文件叫做“mysql-connector-java-5.1.21-bin.jar.index”,其内容如下:
META-INF
META-INF/services
com
com/mysql
com/mysql/jdbc
com/mysql/jdbc/authentication
com/mysql/jdbc/configs
com/mysql/jdbc/exceptions
com/mysql/jdbc/exceptions/jdbc4
com/mysql/jdbc/integration
com/mysql/jdbc/integration/c3p0
com/mysql/jdbc/integration/jboss
com/mysql/jdbc/interceptors
com/mysql/jdbc/jdbc2
com/mysql/jdbc/jdbc2/optional
com/mysql/jdbc/jmx
com/mysql/jdbc/log
com/mysql/jdbc/profiler
com/mysql/jdbc/util
org
org/gjt
org/gjt/mm
org/gjt/mm/mysql
最前面一行和最后面一行是空行!
前3行(包括一个空行)是固定的
其余内容(蓝色部分)来自 mysql-connector-java-5.1.21-bin.jar/META-INF/INDEX.LIST 文件中“mysql-connector-java-5.1.21-bin.jar”之后的内容。
附件(1) 本文从www.mysql.com下载的原始驱动zip包:
http://javaarm.com/file/jboss/wiki/DataSourceConfigurationInAS7/jboss-mysql.5.1.21-module/mysql-connector-java-5.1.21.zip
(2) 本文 为 jboss AS 7.1.1 做的mysql module:
http://javaarm.com/file/jboss/wiki/DataSourceConfigurationInAS7/jboss-mysql.5.1.21-module/jboss-mysql.5.1.21-module.zip
把 该文件解压,得到三个文件“mysql-connector-java-5.1.21-bin.jar”,“mysql-connector-java- 5.1.21-bin.jar.index”和“module.xml” 全部放入 ${mysql_module_main} 目录即可。
注意:
对于 jboss AS 7.2.0 请把“module.xml”中的 <module xmlns="urn:jboss:module:
1.0" name="com.mysql"> 改成 <module xmlns="urn:jboss:module:
1.1" name="com.mysql">
2. 配置MySql数据源
2.1前提
请按照上述描述,制作好MySql的JBoss module.
2.2 standalone.xml 中配置MySql数据源打开 ${JBOSS_HOME}\standalone\configuration\standalone.xml 文件,
(a) 对于JBoss AS 7.1.1,请在<subsystem xmlns="urn:jboss:domain:datasources:
1.0">元素中,增加<datasource>元素和<driver>元素, 如下:
<subsystem xmlns="urn:jboss:domain:datasources:1.0">
<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 jndi-name="java:jboss/datasources/ybxiangForumMySqlDataSource" pool-name="MySqlDS" enabled="true" use-java-context="true">
<connection-url>jdbc:mysql://localhost:3306/ybxiangforum9</connection-url>
<driver>com.mysql</driver>
<transaction-isolation>TRANSACTION_READ_COMMITTED</transaction-isolation>
<pool>
<min-pool-size>4</min-pool-size>
<max-pool-size>8</max-pool-size>
<prefill>true</prefill>
</pool>
<security>
<user-name>root</user-name>
<password>ybxiang123</password>
</security>
<statement>
<prepared-statement-cache-size>32</prepared-statement-cache-size>
<share-prepared-statements>true</share-prepared-statements>
</statement>
</datasource>
<drivers>
<driver name="h2" module="com.h2database.h2">
<xa-datasource-class>org.h2.jdbcx.JdbcDataSource</xa-datasource-class>
</driver>
<driver name="com.mysql" module="com.mysql">
<xa-datasource-class>com.mysql.jdbc.jdbc2.optional.MysqlXADataSource</xa-datasource-class>
</driver>
</drivers>
</datasources>
</subsystem>
(b) 对于JBoss AS 7.2.0,请在<subsystem xmlns="urn:jboss:domain:datasources:
1.1">元素中,增加<datasource>元素和<driver>元素, 如下:
<subsystem xmlns="urn:jboss:domain:datasources:1.1">
<datasources>
... 同上
</datasources>
</subsystem>
注意,我们保留了原来的"com.h2database.h2"的配置。
3. 测试以standalone模式启动JBoss AS 7,也就是运行 ${JBOSS_HOME}\bin下的standalone.bat或者standalone.sh。JBoss AS 7.1.1 日志如下:
22:38:55,468 INFO [org.jboss.modules] JBoss Modules version 1.1.1.GA
22:38:59,609 INFO [org.jboss.msc] JBoss MSC version 1.0.2.GA
22:38:59,843 INFO [org.jboss.as] JBAS015899: JBoss AS 7.1.1.Final "Brontes" starting
22:39:13,281 INFO [org.jboss.as.server] JBAS015888: Creating http management service using socket-binding (management-http)
22:39:13,312 INFO [org.xnio] XNIO Version 3.0.3.GA
22:39:13,421 INFO [org.xnio.nio] XNIO NIO Implementation Version 3.0.3.GA
22:39:13,453 INFO [org.jboss.remoting] JBoss Remoting version 3.2.3.GA
22:39:13,984 INFO [org.jboss.as.logging] JBAS011502: Removing bootstrap log handlers
22:39:14,015 INFO [org.jboss.as.clustering.infinispan] (ServerService Thread Pool -- 43) JBAS010280: Activating Infinispan subsystem.
22:39:14,078 INFO [org.jboss.as.security] (ServerService Thread Pool -- 56) JBAS013101: Activating Security Subsystem
22:39:14,062 INFO [org.jboss.as.naming] (ServerService Thread Pool -- 50) JBAS011800: Activating Naming Subsystem
22:39:14,187 INFO [org.jboss.as.webservices] (ServerService Thread Pool -- 60) JBAS015537: Activating WebServices Extension
22:39:14,250 INFO [org.jboss.as.osgi] (ServerService Thread Pool -- 51) JBAS011940: Activating OSGi Subsystem
22:39:14,625 INFO [org.jboss.as.security] (MSC service thread 1-2) JBAS013100: Current PicketBox version=4.0.7.Final
22:39:15,171 INFO [org.jboss.as.connector] (MSC service thread 1-1) JBAS010408: Starting JCA Subsystem (JBoss IronJacamar 1.0.9.Final)
22:39:17,062 INFO [org.jboss.as.naming] (MSC service thread 1-7) JBAS011802: Starting Naming Service
22:39:17,078 INFO [org.jboss.as.mail.extension] (MSC service thread 1-6) JBAS015400: Bound mail session [java:jboss/mail/Default]
22:39:17,953 INFO [org.jboss.as.connector.subsystems.datasources] (ServerService Thread Pool -- 39) JBAS010403: Deploying JDBC-compliant driver class org.h2.Driver (version 1.3)
22:39:17,953 INFO [org.jboss.as.connector.subsystems.datasources] (ServerService Thread Pool -- 39) JBAS010404: Deploying non-JDBC-compliant driver class com.mysql.jdbc.Driver (version 5.1)
22:39:19,750 INFO [org.apache.coyote.http11.Http11Protocol] (MSC service thread 1-4) Starting Coyote HTTP/1.1 on http-localhost-127.0.0.1-8080
22:39:22,375 INFO [org.jboss.as.connector.subsystems.datasources] (MSC service thread 1-3) JBAS010400: Bound data source [java:jboss/datasources/ExampleDS]
22:39:22,375 INFO [org.jboss.as.connector.subsystems.datasources] (MSC service thread 1-5) JBAS010400: Bound data source [java:jboss/datasources/NmsMySqlDS]
22:39:23,015 INFO [org.jboss.ws.common.management.AbstractServerConfig] (MSC service thread 1-7) JBoss Web Services - Stack CXF Server 4.0.2.GA
22:39:24,703 INFO [org.jboss.as.server.deployment.scanner] (MSC service thread 1-3) JBAS015012: Started FileSystemDeploymentService for directory D:\java\jboss-as-7.1.1.Final\standalone\deployments
22:39:24,875 INFO [org.jboss.as.remoting] (MSC service thread 1-1) JBAS017100: Listening on localhost/127.0.0.1:4447
22:39:24,875 INFO [org.jboss.as.remoting] (MSC service thread 1-3) JBAS017100: Listening on /127.0.0.1:9999
22:39:25,703 INFO [org.jboss.as] (Controller Boot Thread) JBAS015951: Admin console listening on http://127.0.0.1:9990
22:39:25,703 INFO [org.jboss.as] (Controller Boot Thread) JBAS015874: JBoss AS 7.1.1.Final "Brontes" started in 34407ms - Started 137 of 213 services (74 services are passive or on-demand)
4. persistence.xml如果我们开发的是EJB3程序,那么我们可以将该程序的persistence.xml配置如下:
(a) JBoss AS 7.1.1
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"
xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="ybxiang-forum-persistence-unit
">
<jta-data-source>java:jboss/datasources/ybxiangForumMySqlDataSource</jta-data-source>
<properties>
<!-- Properties for Hibernate -->
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
</properties>
</persistence-unit>
</persistence>
(b) JBoss AS 7.2.0
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="ybxiang-forum-persistence-unit">
<jta-data-source>java:jboss/datasources/ybxiangForumMySqlDataSource</jta-data-source>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="hibernate.show_sql" value="false"/>
<property name="hibernate.format_sql" value="true"/>
</properties>
</persistence-unit>
</persistence>
注意:persistence-unit的名字可以随便指定,在@PersistenceContext()使用空白的unitName就可以了:
@Stateless
@Local(IHomePageColumnSession.class)
public class HomePageColumnSession implements IHomePageColumnSession{
@PersistenceContext()
private EntityManager em;
...
}
--转自