atomikos支持一个分布式事务,结合spring,可以很好的满足一个应用访问多个库的需要。
atomikos 结合spring做配置也很简单
1.配置datasource
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | <!-- 第一个数据库 --> <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
1 2 3 4 5 6 7 8 9 | <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配置,跟平时配置一样。
1 2 3 4 5 6 7 8 9 10 | <?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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | <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的管理及注入
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <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下面
1 2 3 4 5 | 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事务的方法,否者不能回滚
如下带面就会出现不会滚问题
1 2 3 4 5 6 | try{ userMapper.addUser(user); roleMapper.addRole(role); }catch(Exception){
} |
当然也可以不用mybatis,个人喜欢用Spring JdbcTemplate
转载于:https://blog.51cto.com/1306733/1721585