Atomikos 分布式数据源,spring,mybatis

1、简介:多数据源配置,支持回滚

2、spring-mybatis.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" 
	xmlns:jdbc="http://www.springframework.org/schema/jdbc"
	xmlns:task="http://www.springframework.org/schema/task" 
	xmlns:cache="http://www.springframework.org/schema/cache"
	xmlns:p="http://www.springframework.org/schema/p" 
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="http://www.springframework.org/schema/jdbc 
	http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
        http://www.springframework.org/schema/task 
        http://www.springframework.org/schema/task/spring-task.xsd
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/cache 
        http://www.springframework.org/schema/cache/spring-cache.xsd
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop 
		http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
	">
	<context:property-placeholder location="classpath:config.properties" />
<tx:annotation-driven />


	<bean id="abstractXADataSource" class="com.atomikos.jdbc.AtomikosDataSourceBean"
		init-method="init" destroy-method="close" abstract="true">
		<property name="xaDataSourceClassName"
			value="com.mysql.jdbc.jdbc2.optional.MysqlXADataSource" />
		<property name="poolSize" value="10" />
		<property name="minPoolSize" value="10" />
		<property name="maxPoolSize" value="30" />
		<property name="borrowConnectionTimeout" value="60" />
		<property name="reapTimeout" value="20" />
		<!-- 最大空闲时间 -->
		<property name="maxIdleTime" value="60" />
		<property name="maintenanceInterval" value="60" />
		<property name="loginTimeout" value="60" />
		<property name="testQuery">
			<value>select 1</value>
		</property>
	</bean>

	<bean id="qadataSource" parent="abstractXADataSource">
		<!-- value只要两个数据源不同就行,随便取名 -->
		<property name="uniqueResourceName" value="mysql/sitestone1" />
		<property name="xaDataSourceClassName"
			value="com.mysql.jdbc.jdbc2.optional.MysqlXADataSource" />
		<property name="xaProperties">
			<props>
				<prop key="URL">${jdbcUrl}</prop>
				<prop key="user">${user}</prop>
				<prop key="password">${password}</prop>
				<prop key="pinGlobalTxToPhysicalConnection">true</prop>
			</props>
		</property>
	</bean>

	<bean id="devdataSource" parent="abstractXADataSource">
		<!-- value只要两个数据源不同就行,随便取名 -->
		<property name="uniqueResourceName" value="mysql/sitestone" />
		<property name="xaDataSourceClassName"
			value="com.mysql.jdbc.jdbc2.optional.MysqlXADataSource" />
		<property name="xaProperties">
			<props>
				<prop key="URL">${jdbc_url}</prop>
				<prop key="user">${user}</prop>
				<prop key="password">${password}</prop>
				<prop key="pinGlobalTxToPhysicalConnection">true</prop>
			</props>
		</property>
	</bean>


	<!-- myBatis文件 -->
	<bean id="qasqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="mapperLocations" value="classpath:com/tyc/mapping/*.xml" />
		<property name="dataSource" ref="qadataSource" />
	</bean>
	<bean id="devsqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="mapperLocations" value="classpath:com/tyc/mappingc/*.xml" />
		<property name="dataSource" ref="devdataSource" />
	</bean>
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="com.tyc.dao" />
		<property name="sqlSessionFactoryBeanName" value="qasqlSessionFactory" />
	</bean>
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="com.tyc.daoc" />
		<property name="sqlSessionFactoryBeanName" value="devsqlSessionFactory" />
	</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 bean="atomikosTransactionManager" />
		</property>
		<property name="userTransaction">
			<ref bean="atomikosUserTransaction" />
		</property>
		<!-- 必须设置,否则程序出现异常 JtaTransactionManager does not support custom isolation 
			levels by default -->
		<property name="allowCustomIsolationLevels" value="true" />
	</bean>


	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="delete*" rollback-for="Exception" />
			<tx:method name="save*" rollback-for="Exception" />
			<tx:method name="update*" rollback-for="Exception" />
			<tx:method name="*" read-only="true" rollback-for="Exception" />
		</tx:attributes>
	</tx:advice>


	<aop:config>
		<aop:pointcut id="serviceOperation" expression="execution(* *..service*..*(..))" />
		<aop:advisor pointcut-ref="serviceOperation" advice-ref="txAdvice" />
	</aop:config>




</beans>
3、config.properties

jdbc_url=jdbc:mysql://localhost:3306/tyc?useUnicode=true&characterEncoding=UTF-8
jdbcUrl=jdbc:mysql://localhost:3306/tyc1?useUnicode=true&characterEncoding=UTF-8
jdbc_username=root
user=root
jdbc_password=password
password=password
driverClass=com.mysql.jdbc.Driver

4、lib说明

       

atomikos-util-3.9.2.jar
cglib-2.1.jar
transactions-3.9.2.jar
transactions-api-3.9.2.jar
transactions-jdbc-3.9.2.jar
transactions-jta-3.9.2.jar
其他spring、mybaits等lib不再累述。


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值