atomikos + druid + spring 多數據源分佈式事務

1.atomikos相關jar包下載

官網下載:https://www.atomikos.com/Main/ExtremeTransactionsDownloadFormhtml

下載地址:http://www.atomikos.com/Main/InstallingExtremeTransactionsgit

transactions-4.0.3.EVAL.jar、transactions-api-4.0.3.EVAL.jar、transactions-jdbc-4.0.3.EVAL.jar、transactions-jta-4.0.3.EVAL.jar、atomikos-util-4.0.3.EVAL.jar,還須要jta-1.1.jar、druid-1.0.5.jar、spring-4.1.6github

2.spring配置文件application-db.xml配置

<!--建立jdbc數據源 -->
	<bean id="dataSource" class="com.alibaba.druid.pool.xa.DruidXADataSource"
		init-method="init" destroy-method="close">
		<property name="driverClassName" value="${jdbc.driver}" />
		<property name="url" value="${jdbc.url}" />
		<property name="password" value="${jdbc.password}" />
		<property name="username" value="${jdbc.username}" />
	
		<!-- 配置初始化大小、最小、最大 -->
		<property name="initialSize" value="3" />
		<property name="minIdle" value="3" />
		<property name="maxActive" value="100" />
	
		<!-- 配置獲取鏈接等待超時的時間 -->
		<property name="maxWait" value="60000" />
	
		<!-- 配置間隔多久才進行一次檢測,檢測須要關閉的空閒鏈接,單位是毫秒 -->
		<property name="timeBetweenEvictionRunsMillis" value="60000" />
	
		<!-- 配置一個鏈接在池中最小生存的時間,單位是毫秒 -->
		<property name="minEvictableIdleTimeMillis" value="300000" />
	
		<property name="validationQuery" value="SELECT 'x'" />
		<property name="testWhileIdle" value="true" />
		<property name="testOnBorrow" value="false" />
		<property name="testOnReturn" value="false" />
	
		<!-- 打開PSCache,而且指定每一個鏈接上PSCache的大小(Oracle使用) <property name="poolPreparedStatements" 
			value="true" /> <property name="maxPoolPreparedStatementPerConnectionSize" 
			value="20" /> -->
	
		<!-- 配置監控統計攔截的filters -->
		<property name="filters" value="stat" />
	</bean>
	
	<bean class="com.atomikos.jdbc.AtomikosDataSourceBean" init-method="init" destroy-method="close" id="dataSource01">
		<property name="uniqueResourceName" value="cdc2" />
        <property name="xaDataSource" ref="dataSource"/>
    </bean>

	<!-- 官方教程:http://mybatis.github.io/mybatis-3/zh/index.html -->
	<!-- 建立SqlSessionFactory,同時指定數據源 -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource01" />
		<!-- <property name="mapperLocations" value="classpath:hello/mapper/*.xml"/> -->
		<property name="mapperLocations" value="classpath:com/hk/web/**/mapper/*.xml"/>
		<!-- <property name="typeAliasesPackage" value="com.tiantian.mybatis.model" /> -->
	</bean>
	
	<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate" scope="prototype">
       <constructor-arg index="0" ref="sqlSessionFactory" />
    </bean>
    
    <!--  -->
    <bean id="dataSource2" class="com.alibaba.druid.pool.xa.DruidXADataSource"
		init-method="init" destroy-method="close">
		<property name="driverClassName" value="${jdbc.driver2}" />
		<property name="url" value="${jdbc.url2}" />
		<property name="password" value="${jdbc.password2}" />
		<property name="username" value="${jdbc.username2}" />
	
		<property name="initialSize" value="3" />
		<property name="minIdle" value="3" />
		<property name="maxActive" value="100" />
	
		<property name="maxWait" value="60000" />
	
		<property name="timeBetweenEvictionRunsMillis" value="60000" />
		<property name="minEvictableIdleTimeMillis" value="300000" />
		<property name="validationQuery" value="SELECT 'x'" />
		<property name="testWhileIdle" value="true" />
		<property name="testOnBorrow" value="false" />
		<property name="testOnReturn" value="false" />
		<property name="filters" value="stat" />
	</bean>
	
	<bean class="com.atomikos.jdbc.AtomikosDataSourceBean" init-method="init" destroy-method="close" id="dataSource02">
		<property name="uniqueResourceName" value="cdc" />
        <property name="xaDataSource" ref="dataSource2"/>
    </bean>

	<bean id="sqlSessionFactory2" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource02" />
		<property name="mapperLocations" value="classpath:com/hk/web/**/mapper/*.xml"/>
	</bean>
	
	<bean id="sqlSession2" class="org.mybatis.spring.SqlSessionTemplate" scope="prototype">
       <constructor-arg index="0" ref="sqlSessionFactory2" />
    </bean>
    
    <!-- atomikos事務管理器 -->  
    <bean id="atomikosTransactionManager" class="com.atomikos.icatch.jta.UserTransactionManager"
		init-method="init" destroy-method="close">
		<description>UserTransactionManager</description>
		<property name="forceShutdown">
			<value>true</value>
		</property>
	</bean>

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

	<!-- spring 事務管理器 -->
	<bean id="transactionManager"
		class="org.springframework.transaction.jta.JtaTransactionManager">
		<property name="transactionManager" ref="atomikosTransactionManager" />
		<property name="userTransaction" ref="atomikosUserTransaction" />
		<property name="allowCustomIsolationLevels" value="true" />
	</bean>

題外話:

有能力的能夠去實現org.springframework.transaction.support.AbstractPlatformTransactionManager,代替org.springframework.jdbc.datasource.DataSourceTransactionManagerweb

相關文章
相關標籤/搜索