springmvc+mybatis下基於註解的Atomikos分佈式事務配置

一、首先下載配置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註解即可使用

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章