JTA实现跨数据库操作
EJB3.0实现跨数据库进行存取
环境:MySQL5.1/JBoss5.1
1.加入跨数据库的数据源,由于JBoss没有提供mysql的配置文件,所以要手工加入:
mysql-xa-ds.xml:(注意:不是mysql-ds.xml)
<?xml version="1.0" encoding="UTF-8"?>
<!--
See http://www.jboss.org/community/wiki/Multiple1PC for information
about local-tx-datasource
-->
<!-- $Id: mysql-ds.xml 88948 2009-05-15 14:09:08Z jesper.pedersen $ -->
<!--
Datasource config for MySQL using 3.0.9 available from:
http://www.mysql.com/downloads/api-jdbc-stable.html
-->
<datasources>
<xa-datasource>
<jndi-name>MySqlXADS1</jndi-name>
<!-- uncomment to enable interleaving <interleaving/> -->
<xa-datasource-class>com.mysql.jdbc.jdbc2.optional.MysqlXADataSource
</xa-datasource-class>
<xa-datasource-property name="Url">jdbc:mysql://localhost/ejb?useUnicode=true&characterEncoding=UTF-8
</xa-datasource-property>
<xa-datasource-property name="user">root
</xa-datasource-property>
<xa-datasource-property name="password">root
</xa-datasource-property>
<user-name>root</user-name>
<password>root</password>
<!--
<transaction-isolation>TRANSACTION_READ_COMMITTED
</transaction-isolation>
-->
<!--
This disables transaction interleaving (which BTW, most DB vendors
don't support)
-->
<!-- <track-connection-by-tx/>-->
<!-- <isSameRM-override-value>false </isSameRM-override-value>-->
<!--pooling parameters-->
<min-pool-size>5</min-pool-size>
<max-pool-size>20</max-pool-size>
<blocking-timeout-millis>5000</blocking-timeout-millis>
<idle-timeout-minutes>15</idle-timeout-minutes>
<exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.MySQLExceptionSorter
</exception-sorter-class-name>
<!--
corresponding type-mapping in the standardjbosscmp-jdbc.xml
(optional)
-->
<metadata>
<type-mapping>mySQL</type-mapping>
</metadata>
</xa-datasource>
<xa-datasource>
<jndi-name>MySqlXADS2</jndi-name>
<!-- uncomment to enable interleaving <interleaving/> -->
<xa-datasource-class>com.mysql.jdbc.jdbc2.optional.MysqlXADataSource
</xa-datasource-class>
<xa-datasource-property name="Url">jdbc:mysql://localhost/ejb2?useUnicode=true&characterEncoding=UTF-8
</xa-datasource-property>
<xa-datasource-property name="user">root
</xa-datasource-property>
<xa-datasource-property name="password">root
</xa-datasource-property>
<user-name>root</user-name>
<password>root</password>
<!--
<transaction-isolation>TRANSACTION_READ_COMMITTED
</transaction-isolation>
-->
<!--
This disables transaction interleaving (which BTW, most DB vendors
don't support)
-->
<!-- <track-connection-by-tx/>-->
<!-- <isSameRM-override-value>false </isSameRM-override-value>-->
<!--pooling parameters-->
<min-pool-size>5</min-pool-size>
<max-pool-size>20</max-pool-size>
<blocking-timeout-millis>5000</blocking-timeout-millis>
<idle-timeout-minutes>15</idle-timeout-minutes>
<exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.MySQLExceptionSorter
</exception-sorter-class-name>
<!--
corresponding type-mapping in the standardjbosscmp-jdbc.xml
(optional)
-->
<metadata>
<type-mapping>mySQL</type-mapping>
</metadata>
</xa-datasource>
</datasources>
2.加入数据库驱动jar到JBOSS_HOME/server/default/lib下
mysql-connector-java-5.0.2.jar
注意:不要加入过低的版本,如mysql-connector-java-2.0.14.jar,否则JBoss找不到相关类也不能进行加载
3.persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence 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_1_0.xsd" version="1.0">
<persistence-unit name="JTA_DatabasesPU" transaction-type="JTA">
<jta-data-source>java:/MySqlXADS1</jta-data-source>
<class>com.pojo.User</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
<property name="hibernate.hbm2ddl.auto" value="create"/>
</properties>
</persistence-unit>
<persistence-unit name="JTA_DatabasesPU2" transaction-type="JTA">
<jta-data-source>java:/MySqlXADS2</jta-data-source>
<class>com.pojo.Person</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
<property name="hibernate.hbm2ddl.auto" value="create"/>
</properties>
</persistence-unit>
</persistence>
4.Bean的实现类:
@Stateless
@Remote(UserManager.class)
public class UserManagerImpl implements UserManager {
@PersistenceContext(unitName="JTA_DatabasesPU")
private EntityManager em;
@PersistenceContext(unitName="JTA_DatabasesPU2")
private EntityManager em2;
public void addDatas() {
User user = new User();
user.setId(1);
user.setName("Chuyang");
em.persist(user);
Person person = new Person();
person.setId(1);
person.setName("WuMeng");
em2.persist(person);
//测试跨数据库回滚
//throw new RuntimeException("测试跨数据库回滚 异常");
}
}
5.然后在建立一个测试类进行测试就可以了。
注:代码省略了实体bean,接口等部分