- 浏览: 534901 次
- 性别:
- 来自: 北京
最新评论
-
lvye351:
zk磁盘满也会有此症状,登录不了,zk server端日志一样 ...
zookeeper的Will not attempt to authenticate using SASL (无法定位登录配置) -
zhu547043537:
1.查看zookeeper是否启动成功的。2.查看防火墙端口是 ...
zookeeper的Will not attempt to authenticate using SASL (无法定位登录配置) -
panghaoyu:
实现一个智能提示功能需要ajax、数据库、jsp/php、算法 ...
solr搜索智能提示Suggest -
xchd:
您好,我在solrconfig.xml里面设置了,查询的时候使 ...
solr搜索打分规制排序 -
pengyufight:
我现在已经把jwchat项目的源码导入MyEclipse10中 ...
openfire+jwchat+nginx搭建XMPP的webim
最近弄一个框架,使用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
评论
4 楼
lavafree
2012-11-28
ghost0224 写道
现在不是有dataSource,和dataSourceB两个数据库吗,那我如何切换当前操作的数据库呢?比如我要先后对两个数据库进行操作,执行完第一个数据库后如何切换到第二个库上呢?
对于不同的对象,比如A对象Dao层注入dataSourceA,操作A库,B的Dao里面注入dataSourceB,操作B库,那样在Service层直接调用a,b方法,就可以保持事物一致!
3 楼
ghost0224
2012-11-28
现在不是有dataSource,和dataSourceB两个数据库吗,那我如何切换当前操作的数据库呢?比如我要先后对两个数据库进行操作,执行完第一个数据库后如何切换到第二个库上呢?
2 楼
lavafree
2012-11-28
ghost0224 写道
如何切换数据源呢
你的切换指的是什么?
1 楼
ghost0224
2012-11-28
如何切换数据源呢
发表评论
-
zookeeper的Will not attempt to authenticate using SASL (无法定位登录配置)
2012-11-12 17:21 181082今天不小心重启了一下zookeeper服务,发现跑的好好的系统 ... -
solr搜索打分规制排序
2012-09-26 15:04 24306solr使用了Lucene的内核,也继承了Lucene的 ... -
atomikos DataSource
2012-05-26 18:19 1790用atomikos做jta事务,以前用SimpleDataSo ... -
一个resin启动多个服务
2012-05-07 16:05 2625resin启动两个服务 1,212 views, Ga ... -
JavaBean的get set规范害死人
2012-01-15 23:45 3145规范中另一个特别的地方就是: 1.第二个字母为大写的属性名要 ... -
openfire+jwchat+nginx搭建XMPP的webim
2011-11-16 11:04 13530openfire+jwchat+nginx搭建XMPP的 ... -
用maven 发布到tomcat
2011-10-17 16:22 17891.pom.xml安装tomcat-maven-plugin ... -
jsoup很不错的html解析器
2011-08-09 16:24 1305最近使用了一下jsoup来做html的解析,感觉很不错,生成d ... -
protobuf的使用
2011-08-02 15:00 9052google下载protobuf,http://code.go ... -
springmvc+freemarker与servlet性能对比
2011-06-17 16:53 3927为了更好的测试公平性,刚才的message.flt去空格改为 ... -
spring mvc +freemarker不错的mvc搭配
2011-06-17 15:52 36950spring mvc现在如火如荼,闲来无事也摆弄一番。 ... -
10000个球中随机取出1000个球
2011-03-05 21:43 1986前几天看到一个算法题,说有10000个球,从中随机取出1000 ... -
TTserver java客户端访问
2011-03-04 10:25 1972package tokyotyrant.example; ... -
linux下图片添加中文乱码解决
2011-02-18 10:49 1384在gif图片上添加文字,在window下没什么问题,但是放到l ... -
对图片添加文字的封装类
2010-12-14 15:57 1232package lava.util; import ... -
linux 下resin web服务搭建
2010-10-28 14:54 26421.安装jdk 我这里安装的是 Oracle JRockit ... -
编码的细节带来大问题
2010-10-20 15:23 1395今天同事吧快钱支付的接口做完了,部署服到服务器,快钱一直提示m ... -
resin下发布基于jax-ws的webservice
2010-08-28 20:54 3408刚学习了一下webservice,基于jax-ws建立w ... -
Linux下安装JBOSS
2010-08-22 13:00 1501以前一直使用Resin,resin小巧,jsp解析快,用的不错 ...
相关推荐
NULL 博文链接:https://injavawetrust.iteye.com/blog/2308552
本用例基于 Spring Boot + Druid + Mybatis 配置多数据源,并采用 JTA 实现分布式事务。
springboot+Atomikos+jpa+mysql的JTA分布式事务实现,本案例涉及到2个数据库,预期结果,在同一个事务中,两个库的状态一致
NULL 博文链接:https://lizhao6210-126-com.iteye.com/blog/1958831
NULL 博文链接:https://onlyor.iteye.com/blog/1483289
atomikos的jar包,我这里也发表了一份Spring3.2+mybatis3.0+atomikos的jta系统搭建的博客,喜欢的朋友也可以支持一下。
Spring多数据源分布式事务管理/springmvc+spring+atomikos[jta]+druid+mybatis
SpringBoot集成Atomikos使用Oracle数据库mybatisSpringBoot集成Atomikos使用Oracle数据库mybatisSpringBoot集成Atomikos使用Oracle数据库mybatisSpringBoot集成Atomikos使用Oracle数据库mybatis
JMay是什么JMay是一款基于Spring、SpringMVC、Mybatis、Atomikos集成框架,用于快速搭建跨数据库、分布式事务(XA规范)支持的Java web项目.JMay有哪些功能1.基于spring,如沐春风的体验;2.扩展Mybatis-Generator,...
:测试spring初始注解:@Async,覆盖串行线程池自定义线程池,无返回值纯初始化,有返回值Future,有返回值CompletableFuture :整合redis,7大数据类型:string,list,set,zset,hash,geo,hyperloglog :...
配置灵活,简单方便.JTA:atomikos. 分布式事务,多数据源事务全靠他.Cache:spring-cache. 统一接口,注解使用,simple,redis... 自动切换.Scheduler:quartz. 开源稳定,支持集群.自家:hsweb-commons :通用工具类hsweb-...