设为首页 收藏本站
查看: 1177|回复: 0

[经验分享] spring+mybatis+Atomikos JTA事务配置说明

[复制链接]

尚未签到

发表于 2016-11-27 07:06:28 | 显示全部楼层 |阅读模式
一、概览
Atomikos是一个公司名字,旗下最著名的莫过于其Atomikos的事务管理器产品。产品分两个:一个是开源的TransactionEssentials,一个是商业的ExtremeTransactions。
TransactionEssentials的主要特征:


  • JTA/XA 事务管理 —— 提供事务管理和连接池
  • 不需要应用服务器 —— TransactionEssentials可以在任何Java EE应用服务器中运行,也就是不依赖于任何应用服务器
  • 开源 —— TransactionEssentials是遵守Apache版本2许可的开源软件
  • 专注于JDBC/JMS —— 支持所有XA资源,但是资源池和消息监听是专供JDBC和JMS的
  • 与Spring 和 Hibernate 集成 —— 提供了描述如何与Spring和Hibernate集成的文档

ExtremeTransactions 是基于TransactionEssentials之上的,增加了对非XA事务的支持,在servlet容器中提供了图形化管理控制面板。Atomikos还提供了基于订阅的支持服务,一份订阅可以得到访问ExtremeTransactions中额外功能的权限。
 
  二、什么是Atomikos TransactionsEssentials
  Atomikos TransactionsEssentials 是一个为Java平台提供增值服务的并且开源类事务管理器,以下是包括在这个开源版本中的一些功能:


  • 全面崩溃 / 重启恢复
  • 兼容标准的SUN公司JTA API
  • 嵌套事务
  • 为XA和非XA提供内置的JDBC适配器
  • 内置的JMS适配器XA-capable JMS队列连接器
  • 通过XA API兼容第三方适配器
  • 更好的整合您的项目
  • 集成Hibernate、mybatis等
  注释:XA:XA协议由Tuxedo首先提出的,并交给X/Open组织,作为资源管理器(数据库)与事务管理器的接口标准。目前,Oracle、Informix、DB2和Sybase等各大数据库厂家都提供对XA的支持。XA协议采用两阶段提交方式来管理分布式事务。XA接口提供资源管理器与事务管理器之间进行通信的标准接口。XA协议包括两套函数,以xa_开头的及以ax_开头的。
  三、如何使用Atomikos TransactionsEssentials
  Atomikos TransactionsEssentials 是一个可靠的库,可以加入到您的Java应用程序,也就是说为了使用这个产品,您必须添加一些jar文件(包括在dist和lib文件夹下)到您的应用程序或者应用程序服务器。
  请注意:Atomikos TransactionsEssentials是一个非常快速的嵌入式事务管理器,这就意味着,您不需要另外启动一个单独的事务管理器进程(不要查找任何的bin文件夹)。相反,您的应用服务器将有它自己的intra-VM事务管理器。
  配置需求:至少Java1.5 jdk,并且最少128M的内存
  性能优化:尽管这个软件有着很大的优势,但是想要更好的发挥其作用,可以按以下的方法优化:


  • 更高的内存,意味着更高的吞吐量(每秒的事务数目)
  • 使连接池尽可能的大
  • 一旦你不需要的连接请马上关闭它们。不要把你的应用程序放在缓存里,让内部连接池为你做这些,这将促使更高效的连接使用
  • 不要让活动的事务闲置:终止所有情况下的事务,尤其是在异常报错情况下的事务。这将减少数据库的锁定时间,并且最大效率的处理启用的使用。
  值得注意的是,在我们所有的压力测试中,Atomikos TransactionsEssentials比J2EE的web容器更高效的吞吐量。这些测量值包括日志记录的高效的事务状态,同样,在我们所有的测量中,包括XA和non-XA,高效的效率是一样的。
  四、spring+mybatis+ Atomikos实现JTA事务
  1.环境


  • Spring3.1.3
  • mybatis3.2.2
  • Atomikos3.4.2
  • jdk1.6
  • mysql5.1.51
  2.创建数据库环境,注意数据库引擎为InnoDB,只有这样才能支持事务
  3.依赖jar包
DSC0000.jpg
 4.配置

  jta.properties也可命名为transactions.properties。如果不配置这个文件,项目也能启动,因为几乎所有配置项都有默认值,最好还是配置了。
  jta.properties:

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 = INFO

  db.properties:

jdbc.xaDataSourceClassName=com.mysql.jdbc.jdbc2.optional.MysqlXADataSource
jdbc.url.a=jdbc:mysql://localhost:3306/trans_a?useUnicode=true&characterEncoding=utf-8&autoReconnect=true
jdbc.username.a=root
jdbc.password.a=
jdbc.uniqueResourceName.a=trans_a

jdbc.url.b=jdbc:mysql://localhost:3306/trans_b?useUnicode=true&characterEncoding=utf-8&autoReconnect=true
jdbc.username.b=root
jdbc.password.b=
jdbc.uniqueResourceName.b=trans_b

transactionManager.forceShutdown =true
transactionManager.transactionTimeout=3000

#*********************atomikos连接池参数设置*****************************
#连接池中保留的最小连接数
atomikos.minPoolSize=1
#连接池中保留的最大连接数
atomikos.maxPoolSize=3
#最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0
atomikos.maxIdleTime=60
  applicationContext-a.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- 数据库配置文件位置 -->
<context:property-placeholder location="classpath:db.properties" />
<bean id="dataSource" class="com.atomikos.jdbc.AtomikosDataSourceBean"
init-method="init" destroy-method="close">
<property name="uniqueResourceName" value="${jdbc.uniqueResourceName.a}" />
<property name="xaDataSourceClassName" value="${jdbc.xaDataSourceClassName}" />
<property name="xaProperties">
<props>
<prop key="user">${jdbc.username.a}</prop>
<prop key="password">${jdbc.password.a}</prop>
<prop key="URL">${jdbc.url.a}</prop>
</props>
</property>
<property name="minPoolSize" value="${atomikos.minPoolSize}" />
<property name="maxPoolSize" value="${atomikos.maxPoolSize}" />
<property name="maxIdleTime" value="${atomikos.maxIdleTime}" />
</bean>
<!-- 采用注释的方式配置bean -->
<context:annotation-config />
<!-- 配置要扫描的包 -->
<context:component-scan base-package="com.ssm"></context:component-scan>
<!--proxy-target-class="true"强制使用cglib代理   如果为false则spring会自动选择-->
<aop:aspectj-autoproxy  proxy-target-class="true"/>
<!-- 配置mybitasSqlSessionFactoryBean -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
</bean>
<!-- 配置SqlSessionTemplate -->
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>
<bean id="atomikosTransactionManager" class="com.atomikos.icatch.jta.UserTransactionManager"
init-method="init" destroy-method="close">
<property name="forceShutdown" value="${transactionManager.forceShutdown}" />
</bean>
<bean id="atomikosUserTransaction" class="com.atomikos.icatch.jta.UserTransactionImp">
<property name="transactionTimeout" value="${transactionManager.transactionTimeout}" />
</bean>
<bean id="transactionManager"
class="org.springframework.transaction.jta.JtaTransactionManager">
<property name="transactionManager">
<ref bean="atomikosTransactionManager" />
</property>
<property name="userTransaction">
<ref bean="atomikosUserTransaction" />
</property>
</bean>
<!-- 使用annotation注解方式配置事务 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
  applicationContext-b.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- 数据库配置文件位置 -->
<context:property-placeholder location="classpath:db.properties" />
<bean id="dataSource_b" class="com.atomikos.jdbc.AtomikosDataSourceBean"
init-method="init" destroy-method="close">
<property name="uniqueResourceName" value="${jdbc.uniqueResourceName.b}" />
<property name="xaDataSourceClassName" value="${jdbc.xaDataSourceClassName}" />
<property name="xaProperties">
<props>
<prop key="user">${jdbc.username.b}</prop>
<prop key="password">${jdbc.password.b}</prop>
<prop key="URL">${jdbc.url.b}</prop>
</props>
</property>
<property name="minPoolSize" value="${atomikos.minPoolSize}" />
<property name="maxPoolSize" value="${atomikos.maxPoolSize}" />
<property name="maxIdleTime" value="${atomikos.maxIdleTime}" />
</bean>

<bean id="sqlSessionFactory_b" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource_b" />
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
</bean>
<!-- 配置SqlSessionTemplate -->
<bean id="sqlSessionTemplate_b" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory_b" />
</bean>
</beans>

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-305898-1-1.html 上篇帖子: mybatis中TypeHandles使用与扩展 下篇帖子: MyBatis Generator产生的Example类
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表