欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

spring+mybatis+atomikos 实现JTA事务

程序员文章站 2022-07-13 17:02:18
...

最近弄一个框架,使用spring3.0.5+mybatis3.0.5,需要访问多库,要应用分布式事务JTA,这是用atomikos 3.70版本,并把配置做一下记录。


配置多个数据源

 

<!-- 第一个数据库 -->
	<bean id="dataSource" class="com.atomikos.jdbc.SimpleDataSourceBean"
		init-method="init" destroy-method="close">
		<property name="uniqueResourceName" value="mysql/main" />
		<property name="xaDataSourceClassName"
			value="com.mysql.jdbc.jdbc2.optional.MysqlXADataSource" />
		<property name="xaDataSourceProperties"
			value="URL=${jdbc.url.a};user=${jdbc.username.a};password=${jdbc.password.a}" />
		<property name="exclusiveConnectionMode" value="true" />
		<property name="connectionPoolSize" value="10" />
		<property name="validatingQuery">
			<value>SELECT 1</value>
		</property>
	</bean>
	<!-- 第二个数据库 -->
	<bean id="dataSourceB" class="com.atomikos.jdbc.SimpleDataSourceBean"
		init-method="init" destroy-method="close">
		<property name="uniqueResourceName" value="mysql/news" />
		<property name="xaDataSourceClassName"
			value="com.mysql.jdbc.jdbc2.optional.MysqlXADataSource" />
		<property name="xaDataSourceProperties"
			value="URL=${jdbc.url.b};user=${jdbc.username.b};password=${jdbc.password.b}" />
		<property name="exclusiveConnectionMode" value="true" />
		<property name="connectionPoolSize" value="10" />
		<property name="validatingQuery">
			<value>SELECT 1</value>
		</property>
	</bean>

 

 

配置mybatis的SessionFactory

 

 

	<bean id="sqlSessionFactoryB" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="configLocation" value="classpath:mybatis/mybatis-config-b.xml" />
		<property name="dataSource" ref="dataSourceB" />
	</bean>

	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="configLocation" value="classpath:mybatis/mybatis-config.xml" />
		<property name="dataSource" ref="dataSource" />
	</bean>

 这里使用的是SessionFactory,不是org.springframework.orm.ibatis.SqlMapClientFactoryBean,在mybatis3中用SqlMapClientFactoryBean汇报com.ibatis.common.xml.NodeletException 异常。

 

configLocation 对应的mybatis配置,跟平时配置一样。

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
	<typeAliases>
		<typeAlias alias="User"  type="com.lantii.domain.User"/>
	</typeAliases>
	<mappers>
		<mapper resource="com/lantii/dao/UserMapper.xml" />
	</mappers>
</configuration>	

 事务这块用spring管理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="springTransactionManager"
		class="org.springframework.transaction.jta.JtaTransactionManager">
		<property name="transactionManager">
			<ref bean="atomikosTransactionManager" />
		</property>
		<property name="userTransaction">
			<ref bean="atomikosUserTransaction" />
		</property>
	</bean>

	<aop:aspectj-autoproxy />

	<aop:config  proxy-target-class="true">
		<aop:advisor pointcut="execution(* *com.lantii.service..*(..))"
			advice-ref="txAdvice" />
	</aop:config>

	<tx:advice id="txAdvice" transaction-manager="springTransactionManager">
		<tx:attributes>
			<tx:method name="get*"  propagation="REQUIRED"  read-only="true" />
			<tx:method name="find*"  propagation="REQUIRED"  read-only="true" />
			<tx:method name="has*"  propagation="REQUIRED"  read-only="true" />
			<tx:method name="locate*"  propagation="REQUIRED"  read-only="true" />
			<tx:method name="*"  propagation="REQUIRED" rollback-for="Exception"  />
		</tx:attributes>
	</tx:advice>

 

Mapper的管理及注入

<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
		<property name="sqlSessionFactory" ref="sqlSessionFactory" />
		<property name="mapperInterface" value="com.lantii.dao.UserMapper" />
	</bean>
	
	<bean id="roleMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
		<property name="sqlSessionFactory" ref="sqlSessionFactoryB" />
		<property name="mapperInterface" value="com.lantii.dao.RoleMapper" />
	</bean>

	<bean id="userService" class="com.lantii.service.UserServiceImpl">
		<property name="userMapper" ref="userMapper" />
		<property name="roleMapper" ref="roleMapper" />
	</bean>
 

atomikos的配置jta.properties,该文件放在应用classpath下面

com.atomikos.icatch.service=com.atomikos.icatch.standalone.UserTransactionServiceFactory
com.atomikos.icatch.console_file_name = tm.out
com.atomikos.icatch.log_base_name = tmlog
com.atomikos.icatch.tm_unique_name = com.atomikos.spring.jdbc.tm
com.atomikos.icatch.console_log_level=WARN
 

这几基本配置完毕,需要jar包

atomikos-util.jar

transactions.jar

transactions-jta.jar

transactions-jdbc-deprecated.jar

mybatis.jar

mybatis-spring.jar

cglib.2.2.2.jar

spring的jar包

 

在Service中,调用事务的方法不能try。。。catch事务的方法,否者不能回滚

如下带面就会出现不会滚问题

try{
				userMapper.addUser(user);
				roleMapper.addRole(role);
			}catch(Exception){
				
			}
 参考文章 http://www.iteye.com/topic/122700