Chinese Version: 开发基于JBoss AS 7.2.0的Java EE程序 - 02.如何配置MySql数据源 or 开发基于JBoss AS 7.2.0的Java EE程序 - 02.如何配置MySql数据源
Summary
This article describes how to make mysql's JBoss Module and how to configure mysql datasource for both jboss-as-7.1.1.Final and jboss-as-7.2.0.Final.
My jboss-as-7.1.1.Final and jboss-as-7.2.0.Final installation path:
- D:\java\jboss-as-7.1.1.Final
- D:\java\jboss-as-7.2.0.Final
Reference: Data Source Configuration in AS 7
1. How to make mysql's JBoss module
1.1 Get mysql driver jar
We can download and extract mysql driver package from www.mysql.com (Here, I use mysql-connector-java-5.1.21.zip, download from javaarm.com: mysql-connector-java-5.1.21.zip). Find the jar mysql-connector-java-5.1.21-bin.jar.
1.2 Create directories for mysql module in JBoss AS
(a) JBoss AS 7.1.1, please create directories: D:\java\jboss-as-7.1.1.Final\modules\com\mysql\main
(b) JBoss AS 7.2.0, please create directories: D:\java\jboss-as-7.2.0.Final\modules\system\layers\base\com\mysql\main
From now on, We use ${mysql_module_main} to refer to above mysql module directories.
1.3 copy mysql driver jar to ${mysql_module_main}
Now, let's copy mysql-connector-java-5.1.21-bin.jar to ${mysql_module_main}.
1.4 create module descriptor file for mysql - module.xml
Let's create a new file, module.xml, in ${mysql_module_main}.
(a) For JBoss AS 7.1.1, its content is:
<?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) For JBoss AS 7.2.0, its content is:
<?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>
Please pay attention to the module version marked red.
1.5 make mysql module index file
Create an empty file in ${mysql_module_main} with name ${mysql_driver_jar_name}.index. Here, this file's name is mysql-connector-java-5.1.21-bin.jar.index”. Its content is:
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
NOTE:
- The first and last line are blank lines.
- The first 3 lines(including the first blank line) are fixed.
- The rest (blue part) is from mysql-connector-java-5.1.21-bin.jar/META-INF/INDEX.LIST: after the line “mysql-connector-java-5.1.21-bin.jar”.
Attachments
(1) You can download the driver zip file from my site:
(2) I had created mysql module files/directories for jboss AS 7.1.1, you can download it from my site:
After extract it, you will get 3 files: mysql-connector-java-5.1.21-bin.jar, mysql-connector-java- 5.1.21-bin.jar.index and module.xml. Please throw them into ${mysql_module_main} directly.
NOTE:
As to jboss AS 7.2.0, please change <module xmlns="urn:jboss:module:1.0" name="com.mysql"> to <module xmlns="urn:jboss:module:1.1" name="com.mysql"> in module.xml.
2. MySql data source configuration
2.1 Precondition
Please finish first part "How to make mysql's JBoss module" described above.
2.2 mysql data source configuration in standalone.xml
Open ${JBOSS_HOME}\standalone\configuration\standalone.xml,
(a) For JBoss AS 7.1.1, please add new <datasource> and <driver> elements in <subsystem xmlns="urn:jboss:domain:datasources:1.0"> as bellow:
<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) For JBoss AS 7.2.0, please add new <datasource> and <driver> elements in <subsystem xmlns="urn:jboss:domain:datasources:1.1"> as bellow:
<subsystem xmlns="urn:jboss:domain:datasources:1.1">
<datasources>
... same as (a)
</datasources>
</subsystem>
3. Test
Let's run standalone.bat(windows) or standalone.sh(linux) located in ${JBOSS_HOME}\bin to start jboss AS 7. We will see log like this:
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
If we are developing EJB3 application, we can configure its persistence.xml like this:
(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>
NOTE: persistence-unit can be any valid string. We can use default unitName in @PersistenceContext() like this:
@Stateless
@Local(IHomePageColumnSession.class)
public class HomePageColumnSession implements IHomePageColumnSession{
@PersistenceContext()
private EntityManager em;
...
}
Comments