spring4+mybatis3+atomikos3.9多数据源分布式事务控制

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zeroctu/article/details/53116351

spring4+mybatis3+atomikos3.9分布式事务控制

pom文件关键jar一览

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>4.2.3.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>com.atomikos</groupId>
            <artifactId>transactions-jdbc</artifactId>
            <version>3.9.3</version>
        </dependency>
        <dependency>
            <groupId>com.atomikos</groupId>
            <artifactId>transactions-jta</artifactId>
            <version>3.9.3</version>
        </dependency>
        <dependency>
            <groupId>com.atomikos</groupId>
            <artifactId>transactions</artifactId>
            <version>3.9.3</version>
        </dependency>
        <dependency>
            <groupId>com.atomikos</groupId>
            <artifactId>transactions-api</artifactId>
            <version>3.9.3</version>
        </dependency>
        <dependency>
            <groupId>com.atomikos</groupId>
            <artifactId>atomikos-util</artifactId>
            <version>3.9.3</version>
        </dependency>
        <dependency>
            <groupId>javax.transaction</groupId>
            <artifactId>jta</artifactId>
            <version>1.1</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.3.0</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.2.3</version>
        </dependency>

xml配置

<!--数据源1-->
    <bean id="DataSourceOne" class="com.atomikos.jdbc.AtomikosDataSourceBean" destroy-method="close">
        <property name="uniqueResourceName" value="one" />
        <property name="xaDataSourceClassName" value="com.mysql.jdbc.jdbc2.optional.MysqlXADataSource"/>
        <property name="xaProperties">
            <props>
                <prop key="url">${db.url.one}</prop>
                <prop key="user">${db.user.one}</prop>
                <prop key="password">${db.password.one}</prop>
            </props>
        </property>
        <property name="minPoolSize" value="5" />
        <property name="maxPoolSize" value="20" />
        <property name="maxIdleTime" value="60" />
        <property name="reapTimeout" value="20000" />
    </bean>

    <!--数据源2-->
    <bean id="DataSourceTwo" class="com.atomikos.jdbc.AtomikosDataSourceBean" destroy-method="close">
        <property name="uniqueResourceName" value="two" />
        <property name="xaDataSourceClassName" value="com.mysql.jdbc.jdbc2.optional.MysqlXADataSource"/>
        <property name="xaProperties">
            <props>
                <prop key="url">${db.url.two}</prop>
                <prop key="user">${db.user.two}</prop>
                <prop key="password">${db.password.two}</prop>
            </props>
        </property>
        <property name="minPoolSize" value="5" />
        <property name="maxPoolSize" value="20" />
        <property name="maxIdleTime" value="60" />
        <property name="reapTimeout" value="20000" />
    </bean>

    <bean id="mysqlSessionFactory_one" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="DataSourceOne" />
        <property name="mapperLocations" value="classpath*:META-INF/sqlxml/one/*.xml"/>
    </bean>

    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.test.dao.one"/>
        <property name="sqlSessionFactoryBeanName" value="mysqlSessionFactory_one"/>
    </bean>

    <bean id="mysqlSessionFactory_two" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="DataSourceTwo" />
        <property name="mapperLocations" value="classpath*:META-INF/sqlxml/two/*.xml"/>
    </bean>

    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.test.dao.two"/>
        <property name="sqlSessionFactoryBeanName" value="mysqlSessionFactory_two"/>
    </bean>

    <!-- 事务配置 -->
    <bean id="userTransactionService" class="com.atomikos.icatch.config.UserTransactionServiceImp"
          init-method="init" destroy-method="shutdownForce">
        <constructor-arg>
            <props>
                <prop key="com.atomikos.icatch.service">com.atomikos.icatch.standalone.UserTransactionServiceFactory
                </prop>
            </props>
        </constructor-arg>
    </bean>

    <bean id="atomikosTransactionManager" class="com.atomikos.icatch.jta.UserTransactionManager"
          init-method="init" destroy-method="close" depends-on="userTransactionService">
        <property name="forceShutdown" value="false" />
    </bean>
    <bean id="atomikosUserTransaction" class="com.atomikos.icatch.jta.UserTransactionImp"
          depends-on="userTransactionService">
        <property name="transactionTimeout" value="300" />
    </bean>
    <bean id="jtaTransactionManager"
          class="org.springframework.transaction.jta.JtaTransactionManager"
          depends-on="userTransactionService">
        <property name="transactionManager" ref="atomikosTransactionManager" />
        <property name="userTransaction" ref="atomikosUserTransaction" />
    </bean>

    <tx:annotation-driven transaction-manager="jtaTransactionManager" proxy-target-class="true"/>

    <aop:aspectj-autoproxy proxy-target-class="true" expose-proxy="true"/>

    <tx:advice id="txAdvice" transaction-manager="jtaTransactionManager">
        <tx:attributes>
            <tx:method name="insert*" propagation="REQUIRED"/>
            <tx:method name="save*" propagation="REQUIRED"/>
            <tx:method name="add*" propagation="REQUIRED"/>
            <tx:method name="create*" propagation="REQUIRED"/>
            <tx:method name="update*" propagation="REQUIRED"/>
            <tx:method name="edit*" propagation="REQUIRED"/>
            <tx:method name="delete*" propagation="REQUIRED"/>
            <tx:method name="remove*" propagation="REQUIRED"/>
            <tx:method name="get*" read-only="true"/>
            <tx:method name="find*" read-only="true"/>
            <tx:method name="query*" read-only="true"/>
            <tx:method name="*" propagation="SUPPORTS" read-only="true"/>
        </tx:attributes>
    </tx:advice>
    <aop:config>
        <aop:pointcut id="leePointcut"
                      expression="execution(* com.test.service.impl.*Impl.*(..)) "/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="leePointcut"/>
    </aop:config>

可参考(http://www.codexiu.cn/mysql/blog/10637/)

展开阅读全文

没有更多推荐了,返回首页