Spring JTA 事务小记

实验一:MySQL 5.0
采用atomikos的jta事务(要感谢 http://andyao.javaeye.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" class="com.atomikos.jdbc.SimpleDataSourceBean"
init-method="init" 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&amp;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" class="com.atomikos.jdbc.SimpleDataSourceBean"
init-method="init" 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&amp;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" class="org.springframework.jdbc.support.lob.DefaultLobHandler" />

<!-- 第一个数据库的sqlMapClient -->
<bean id="sqlMapClient1" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation">
<!-- 包含第一个数据库表的map -->
<value>classpath:SqlMapConfig.xml</value>
</property>
<property name="dataSource" ref="dataSource" />
<property name="lobHandler" ref="lobHandler" />
</bean>
<!-- 第二个数据库的sqlMapClient -->
<bean id="sqlMapClient2" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation">
<!-- 包含第一个数据库表的map -->
<value>classpath:SqlMapConfig2.xml</value>
</property>
<property name="dataSource" ref="dataSourceB" />
<property name="lobHandler" ref="lobHandler" />
</bean>

<!-- Optional: add a log administrator -->
<bean id="localLogAdministrator"
class="com.atomikos.icatch.admin.imp.LocalLogAdministrator"/>

<bean id="userTransactionService"
  class="com.atomikos.icatch.config.UserTransactionServiceImp"
  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"
      class="com.atomikos.icatch.jta.UserTransactionManager"
      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"
      class="com.atomikos.icatch.jta.UserTransactionImp" 
      depends-on="userTransactionService">
   <property name="transactionTimeout" value="300" />
</bean>
   <!-- Configure the Spring framework to use JTA transactions from Atomikos -->
<bean id="JtaTransactionManager"
      class="org.springframework.transaction.jta.JtaTransactionManager"
      depends-on="userTransactionService">
   <property name="transactionManager" ref="AtomikosTransactionManager" />
   <property name="userTransaction" ref="AtomikosUserTransaction" />
</bean>

<bean id="user1Dao" class="com.crm.code.dao.impl.User1DaoImpl">
<property name="sqlMapClient">
<ref bean="sqlMapClient1"/>
</property>
</bean>
<bean id="user2Dao" class="com.crm.code.dao.impl.User2DaoImpl">
<property name="sqlMapClient">
<ref bean="sqlMapClient2"/>
</property>
</bean>
<bean id="user12Service" class="com.crm.code.service.impl.User12ServiceImpl">
<property name="user1Dao">
<ref bean="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" class="org.springframework.transaction.jta.JotmFactoryBean"/>   
   <bean id="txManager" class="org.springframework.transaction.jta.JtaTransactionManager"> 
         <property name="userTransaction" ref="jotm"/>
   </bean>          
   
    <bean id="dataSourceA" class="org.enhydra.jdbc.pool.StandardXAPoolDataSource" 
    destroy-method="shutdown"> 
        <property name="dataSource">
             <bean class="org.enhydra.jdbc.standard.StandardXADataSource" 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" class="org.enhydra.jdbc.pool.StandardXAPoolDataSource" 
    destroy-method="shutdown"> 
        <property name="dataSource">
             <bean class="org.enhydra.jdbc.standard.StandardXADataSource" 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-class="true" />
          
<!-- 第一个数据库的sqlMapClient -->
<bean id="sqlMapClient1" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation">
<!-- 包含第一个数据库表的map -->
<value>classpath:SqlMapConfig_ora1.xml</value>
</property>
<property name="dataSource" ref="dataSourceA" />
</bean>
<!-- 第二个数据库的sqlMapClient -->
<bean id="sqlMapClient2" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation">
<!-- 包含第一个数据库表的map -->
<value>classpath:SqlMapConfig_ora2.xml</value>
</property>
<property name="dataSource" ref="dataSourceB" />
</bean>


<bean id="product1Dao" class="com.crm.code.dao.impl.Product1DaoImpl">
<property name="sqlMapClient">
<ref bean="sqlMapClient1"/>
</property>
</bean>
<bean id="product2Dao" class="com.crm.code.dao.impl.Product2DaoImpl">
<property name="sqlMapClient">
<ref bean="sqlMapClient2"/>
</property>
</bean>
<bean id="product12Service" class="com.crm.code.service.impl.Product12ServiceImpl">
<property name="product1Dao">
<ref bean="product1Dao" />
</property>
<property name="product2Dao">
<ref bean="product2Dao" />
</property>
</bean>
</beans>

posted on 2009-12-09 17:29 飞熊 阅读(2187) 评论(0)  编辑  收藏 所属分类: JTA


只有注册用户登录后才能发表评论。


网站导航:
 
<2024年5月>
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678

导航

统计

常用链接

留言簿(1)

随笔分类

随笔档案

文章分类

文章档案

收藏夹

搜索

最新评论

阅读排行榜

评论排行榜