读书人

Spring JTA 事宜小记

发布时间: 2012-10-30 16:13:36 作者: rapoo

Spring JTA 事务小记
最近一个项目要跨多数据,配多数据源的,其中就用到了事务,毫无疑问我选择的是Spring的声明式JTA事务。我的环境是JBOSS+ORACLE 9I
自己私下做了些实验,不过还是成功了
实验一:MySQL 5.0
采用atomikos的jta事务(要感谢 http://andyao.iteye.com/)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">

<bean id="dataSource" destroy-method="close">
<property name="uniqueResourceName">
<value>mysql/main</value>
</property>
<property name="xaDataSourceClassName">
<!--使用Mysql XADataSource(mysql>=5.0, Connector/J>=5.0才可以支持XADatasource)-->
<value>com.mysql.jdbc.jdbc2.optional.MysqlXADataSource</value>
</property>
<property name="xaDataSourceProperties">
<value>URL=jdbc:mysql://localhost:3306/crm?useUnicode=true&characterEncoding=UTF-8;user=root;password=root</value>
</property>
<property name="exclusiveConnectionMode">
<value>true</value>
</property>
<property name="connectionPoolSize">
<value>3</value>
</property>
<property name="validatingQuery">
<value>SELECT 1</value>
</property>
</bean>
<!-- 第二个数据库 -->
<bean id="dataSourceB" destroy-method="close">
<property name="uniqueResourceName">
<value>mysql/news</value>
</property>
<property name="xaDataSourceClassName">
<!--
使用Mysql XADataSource(mysql>=5.0, Connector/J>=5.0才可以支持XADatasource)
-->
<value>com.mysql.jdbc.jdbc2.optional.MysqlXADataSource</value>
</property>
<property name="xaDataSourceProperties">
<value>URL=jdbc:mysql://localhost:3306/crm2?useUnicode=true&characterEncoding=UTF-8;user=root;password=root</value>
</property>
<property name="exclusiveConnectionMode">
<value>true</value>
</property>
<property name="connectionPoolSize">
<value>3</value>
</property>
<property name="validatingQuery">
<value>SELECT 1</value>
</property>
</bean>

<bean id="lobHandler" />

<!-- 第一个数据库的sqlMapClient -->
<bean id="sqlMapClient1" ref="dataSource" />
<property name="lobHandler" ref="lobHandler" />
</bean>
<!-- 第二个数据库的sqlMapClient -->
<bean id="sqlMapClient2" ref="dataSourceB" />
<property name="lobHandler" ref="lobHandler" />
</bean>

<!-- Optional: add a log administrator -->
<bean id="localLogAdministrator"


init-method="init" destroy-method="shutdownForce">
<constructor-arg>
<!-- IMPORTANT: specify all Atomikos properties here -->
<props>
<prop key="com.atomikos.icatch.service">com.atomikos.icatch.standalone.UserTransactionServiceFactory</prop>
</props>
</constructor-arg>
<property name="initialLogAdministrators">
<list>
<ref bean="localLogAdministrator"/>
</list>
</property>
</bean>
<!--Construct Atomikos UserTransactionManager,needed to configure Spring -->
<bean id="AtomikosTransactionManager"

init-method="init" destroy-method="close"
depends-on="userTransactionService">
<!--when close is called,should we force transactions to terminate or not?-->
<property name="forceShutdown" value="false" />
</bean>
<!--Also use Atomikos UserTransactionImp, needed to configure Spring-->
<bean id="AtomikosUserTransaction"
value="300" />
</bean>
<!-- Configure the Spring framework to use JTA transactions from Atomikos -->
<bean id="JtaTransactionManager"

depends-on="userTransactionService">
<property name="transactionManager" ref="AtomikosTransactionManager" />
<property name="userTransaction" ref="AtomikosUserTransaction" />
</bean>

<bean id="user1Dao" />
</property>
<property name="user2Dao">
<ref bean="user2Dao" />
</property>
</bean>

</beans>
这样是成功的 可是切换oracle9i时悲剧发生了
--- Cause: com.atomikos.datasource.ResourceException: resume for XID oracle.jdbc.xa.OracleXid@145f939 raised -3: the XA resource detected an internal error
Caused by: com.ibatis.common.jdbc.exception.NestedSQLException:
--- The error occurred in ibatis/Product1.xml.
--- The error occurred while executing update.
--- Check the insert into boss_product (PROD_ID, PARENT_ID, APP_ID, PROD_NAME, PROD_CODE, DEFAULT_VER_PROD_ID, DATA_PATH, GMT_CREATED, GMT_MODIFIED, CREATOR, MODIFIER, IS_DELETED) values (seq_boss_product.nextval, 1, 88, ?, ?, 10, 'aaa', sysdate, sysdate, 'aavv', 'aacb', 'n') .

官方说oracle连接问题 哎。。。无语了
换了一种JTA事务机制 通过JOTM
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">

<bean id="jotm" ref="jotm"/>
</bean>

<bean id="dataSourceA" destroy-method="shutdown">
<property name="transactionManager" ref="jotm"/>
<property name="driverName" value="oracle.jdbc.driver.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:@10.2.224.44:1521:trade"/>
</bean>
</property>
<property name="user" value="crm_aep"/>
<property name="password" value="crm_aep"/>
</bean>

<bean id="dataSourceB" destroy-method="shutdown">
<property name="transactionManager" ref="jotm"/>
<property name="driverName" value="oracle.jdbc.driver.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:@10.2.226.24:1521:voucher"/>
</bean>
</property>
<property name="user" value="boss"/>
<property name="password" value="boss"/>
</bean>

<tx:annotation-driven transaction-manager="txManager" proxy-target-/>

<!-- 第一个数据库的sqlMapClient -->
<bean id="sqlMapClient1" ref="dataSourceA" />
</bean>
<!-- 第二个数据库的sqlMapClient -->
<bean id="sqlMapClient2" ref="dataSourceB" />
</bean>


<bean id="product1Dao" />
</property>
<property name="product2Dao">
<ref bean="product2Dao" />
</property>
</bean>
</beans>

成功了。。。
很好很好 哈哈哈 1 楼 挪威的幽灵 2009-06-01 事务这个东西有点多

读书人网 >软件架构设计

热点推荐