一、首先下载配置atomikos所需jar包,并将其添加至项目中:jar包下载
二、配置atomikos的初始化配置文件transactions.properties,添加至项目的classpath目录下,不进行配置则会引用默认值:
com.atomikos.icatch.console_file_name = xam.out
com.atomikos.icatch.log_base_name = xamlog.log
com.atomikos.icatch.service = com.atomikos.icatch.standalone.UserTransactionServiceFactory
三、配置springmvc的config文件,与atomikos进行整合:
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
">
<!-- 启用注解 -->
<context:annotation-config />
<!-- 启用spring注解扫描并指定包所在的位置 -->
<context:component-scan base-package="com.fujfu.*" />
<!-- 设置了此bean可以以${XXX}的方式读取mysql.properties
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>classpath:config/mysql.properties</value>
</property>
</bean> -->
<!-- 富宝袋jdbc dataSource -->
<bean id="fujfuDataSource" class="com.alibaba.druid.pool.xa.DruidXADataSource" init-method="init" destroy-method="close">
<!-- 基本属性 url、user、password -->
<property name="url" value="jdbc:mysql://10.128.199.232:3306/fujfu?useUnicode=true&characterEncoding=UTF-8"/>
<property name="username" value="furonge" />
<property name="password" value="12345678" />
<!-- 配置初始化大小、最小、最大 -->
<property name="initialSize" value="0" />
<property name="minIdle" value="1" />
<property name="maxIdle" value="20" />
<property name="maxActive" value="20" />
<!-- 配置获取连接等待超时的时间 -->
<property name="maxWait" value="60000"/>
<!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
<property name="timeBetweenEvictionRunsMillis" value="60000" />
<!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
<property name="minEvictableIdleTimeMillis" value="60000" />
<property name="validationQuery" value="SELECT 1" />
<property name="testWhileIdle" value="true" />
<property name="testOnBorrow" value="false" />
<property name="testOnReturn" value="false" />
<property name="removeAbandoned" value="true" />
<property name="removeAbandonedTimeout" value="1800" />
<property name="logAbandoned" value="true" />
<property name="filters" value="mergeStat" />
</bean>
<!-- account jdbc dataSource -->
<bean id="accountDataSource" class="com.alibaba.druid.pool.xa.DruidXADataSource" init-method="init" destroy-method="close">
<!-- 基本属性 url、user、password -->
<property name="url" value="jdbc:mysql://10.128.199.232:3306/account?useUnicode=true&characterEncoding=UTF-8" />
<property name="username" value="furonge" />
<property name="password" value="12345678" />
<!-- 配置初始化大小、最小、最大 -->
<property name="initialSize" value="0" />
<property name="minIdle" value="1" />
<property name="maxIdle" value="20" />
<property name="maxActive" value="20" />
<!-- 配置获取连接等待超时的时间 -->
<property name="maxWait" value="60000"/>
<!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
<property name="timeBetweenEvictionRunsMillis" value="60000" />
<!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
<property name="minEvictableIdleTimeMillis" value="60000" />
<property name="validationQuery" value="SELECT 1" />
<property name="testWhileIdle" value="true" />
<property name="testOnBorrow" value="false" />
<property name="testOnReturn" value="false" />
<property name="removeAbandoned" value="true" />
<property name="removeAbandonedTimeout" value="1800" />
<property name="logAbandoned" value="true" />
<property name="filters" value="mergeStat" />
</bean>
<!-- sqlSessionFactory配置 -->
<bean id="fuyhuiSqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="fujfuDataSource" />
<!-- 自动扫描mapping.xml文件 -->
<property name="mapperLocations">
<array>
<value>classpath*:com/fujfu/dao/*/mapping/*.xml</value>
<value>classpath*:com/fujfu/dao/app/fkd/*/mapping/*.xml</value>
<value>classpath*:com/fujfu/dao/app/version/mapping/*.xml</value>
</array>
</property>
</bean>
<bean id="accountSqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="accountDataSource" />
<!-- 自动扫描mapping.xml文件 -->
<property name="mapperLocations">
<array>
<value>classpath*:com/fujfu/dao/comuanda/mapping/*.xml</value>
</array>
</property>
</bean>
<!-- 扫描com.fujfu下的所有接口,然后创建各自接口的动态代理类 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage"
value="com.fujfu.dao.admin,
com.fujfu.dao.app,
com.fujfu.dao.common,
com.fujfu.dao.focus,
com.fujfu.dao.news,
com.fujfu.dao.user" />
<property name="sqlSessionFactory" ref="fuyhuiSqlSessionFactory"/>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.fujfu.dao.comuanda" />
<property name="sqlSessionFactory" ref="accountSqlSessionFactory"/>
</bean>
<!-- atomikos事务管理器 -->
<bean id="atomikosTransactionManager" class="com.atomikos.icatch.jta.UserTransactionManager" init-method="init" destroy-method="close">
<property name="forceShutdown">
<value>true</value>
</property>
</bean>
<bean id="atomikosUserTransaction" class="com.atomikos.icatch.jta.UserTransactionImp">
<property name="transactionTimeout" value="300" />
</bean>
<bean id="springJTATransactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">
<property name="transactionManager" ref="atomikosTransactionManager"/>
<property name="userTransaction" ref="atomikosUserTransaction" />
<property name="allowCustomIsolationLevels" value="true"/>
</bean>
<tx:annotation-driven transaction-manager="springJTATransactionManager"/>
<!-- 这个content表示从指定的包去寻找组件<bean>("\"表示所有包),就不需要在此定义组件了<bean> -->
<context:component-scan base-package="\" />
</beans>
到此,配置完毕。在service层的方法中使用@Transcation注解即可使用