atomikos在Tomcat中无法正确执行

Lindp 发布于 2015/06/17 08:50
阅读 695
收藏 1

请教一个关于分布式事务的问题,项目中使用到了多数据源,导致普通的事务无效,在网上找到了atomikos可以帮助Tomcat完成多数据源事务,但是配置完成后我在junit测试中可以正确执行回滚,但是将项目发布到Tomcat中后事务就失效了,请问atomikos在Tomcat中运行需要特殊的配置吗 ?

上代码

Spring.XML

<?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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

    <!-- 加载jdbc配置文件 -->
    <context:property-placeholder location="classpath:jdbc.properties"/>

    <!-- 扫描所有com.ht包下的注解 -->
    <context:component-scan base-package="com.ht"/>

    <!-- 物业核心文件配置 -->
    <bean id="wycoreDataSource" class="com.atomikos.jdbc.AtomikosDataSourceBean"
          init-method="init" destroy-method="close">
        <property name="uniqueResourceName" value="wycoreDataSource"/>
        <property name="xaDataSourceClassName" value="${wycore.jdbc.driver}"/>
        <property name="xaProperties">
            <props>
                <prop key="url">${wycore.jdbc.url}</prop>
                <prop key="user">${wycore.jdbc.username}</prop>
                <prop key="password">${wycore.jdbc.password}</prop>
            </props>
        </property>
        <property name="minPoolSize" value="10"/>
        <property name="maxPoolSize" value="100"/>
        <property name="borrowConnectionTimeout" value="30"/>
        <property name="testQuery" value="select 1"/>
        <property name="maintenanceInterval" value="60"/>
    </bean>

    <bean id="wycoreSqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="wycoreDataSource"/>
        <property name="configLocation" value="classpath:mybatis.xml"/>
        <property name="mapperLocations" value="classpath:mapper/**/*.xml"/>
    </bean>

    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="sqlSessionFactoryBeanName" value="wycoreSqlSessionFactory"/>
        <property name="basePackage" value="com.ht.wy.*.dao"/>
    </bean>

    <!-- Atomikos分布式事务实现配置 -->
    <bean id="atomikosTransactionManager" class="com.atomikos.icatch.jta.UserTransactionManager"
          init-method="init" destroy-method="close">
        <property name="forceShutdown" value="true"/>
    </bean>

    <bean id="atomikosUserTransaction" class="com.atomikos.icatch.jta.UserTransactionImp">
        <property name="transactionTimeout" value="300"/>
    </bean>

    <bean id="jtaTransactionManager"
          class="org.springframework.transaction.jta.JtaTransactionManager">
        <property name="transactionManager" ref="atomikosTransactionManager"/>
        <property name="userTransaction" ref="atomikosUserTransaction"/>
    </bean>

    <!-- 注解加载的事务 -->
    <tx:annotation-driven transaction-manager="jtaTransactionManager"/>

</beans>

业务逻辑代码,在类的顶部加入 @Service @Transactional注解

public int addChargeDeposit(ChargeDeposit chargeDeposit) {
        chargeDepositMapper.addChargeDeposit(chargeDeposit);
        // 生成日志
        ChargeDepositLog chargeDepositLog = ChargeDepositLog.copy(chargeDeposit);
        chargeDepositLog.setDepositLogId(UUIDGenerate.generate());
        chargeDepositLog.setRemark(chargeDeposit.getRemark());
        chargeDepositLog.setCreateUser(chargeDeposit.getCreateUser());
        chargeDepositLogMapper.addChargeDepositLog(chargeDepositLog); // 此方法报错
        return 1;
    }



加载中
返回顶部
顶部