`

spring3.1+Atomikos 构建jta的分布式事务

 
阅读更多

spring3.1+Atomikos 构建jta的分布式事务

第一.下载Atomikos,需要以下这些包 

 

transactions-api.jar(必须)
transactions-hibernate2.jar
transactions-hibernate3.jar
transactions-jdbc-deprecated.jar
transactions-jdbc.jar(必须)
transactions-jms-deprecated.jar
transactions-jms.jar
transactions-jta.jar(必须)
transactions-osgi.jar(必须)
transactions.jar(必须)

   可以根据参与事务的资源和使用方式不同选择上面的不用jar。

第二.配置 
在applicationContext.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:aop="http://www.springframework.org/schema/aop"
	xmlns:task="http://www.springframework.org/schema/task"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context-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/aop
	http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
	http://www.springframework.org/schema/task 
	http://www.springframework.org/schema/task/spring-task-3.0.xsd">

	<context:property-placeholder
		location="classpath:jdbc.properties"
		ignore-unresolvable="true" />
		
		<!--数据源 couponDataSource-->
		<bean id="couponDataSource" class="com.atomikos.jdbc.AtomikosDataSourceBean"
		init-method="init" destroy-method="close">
		<description>mysql xa datasource</description>
		<property name="uniqueResourceName">  
			<value>couponDataSource</value>  
		</property>
		<property name="xaDataSourceClassName" value="com.mysql.jdbc.jdbc2.optional.MysqlXADataSource" />
		<property name="xaProperties">  
			<props>  
				<prop key="user">${luckybee.coupon.jdbc.username}</prop>  
				<prop key="password">${luckybee.coupon.jdbc.password}</prop>  
				<prop key="URL">${luckybee.coupon.jdbc.url}</prop>  
			</props>  
		</property>
		<property name="poolSize" value="3"/>  
	</bean>
	<bean id="couponJdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" autowire="default">
		<property name="dataSource">
			<ref bean="couponDataSource" />
		</property>
	</bean>
	
    <!--数据源 couponDataSource-->
	<bean id="shopDataSource" class="com.atomikos.jdbc.AtomikosDataSourceBean"
		init-method="init" destroy-method="close">
		<description>mysql xa datasource</description>
		<property name="uniqueResourceName">  
            <value>shopDataSource</value>  
        </property>
        <property name="xaDataSourceClassName" value="com.mysql.jdbc.jdbc2.optional.MysqlXADataSource" />
        <property name="xaProperties">  
            <props>  
                <prop key="user">${luckybee.shop.jdbc.username}</prop>  
                <prop key="password">${luckybee.shop.jdbc.password}</prop>  
                <prop key="URL">${luckybee.shop.jdbc.url}</prop>  
            </props>  
        </property>
        <property name="poolSize" value="3"/>   
	</bean>
	<bean id="shopJdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" autowire="default">
		<property name="dataSource">
			<ref bean="shopDataSource" />
		</property>
	</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>   
	<bean id="txManager" class="org.springframework.transaction.jta.JtaTransactionManager">
		<property name="transactionManager">
			<ref bean="atomikosTransactionManager"  />
		</property>
		<property name="userTransaction">
			<ref bean="atomikosUserTransaction"/>
		</property>
		<property name="allowCustomIsolationLevels" value="true"/>
	</bean>
	<tx:advice id="shopTxAdvice" transaction-manager="txManager">
		<tx:attributes>
			<tx:method name="get*" read-only="true" />
			<tx:method name="query*" read-only="true" />
			<tx:method name="find*" read-only="true" />
			<tx:method name="insert*" read-only="false" isolation="READ_COMMITTED" rollback-for="Exception" />
			<tx:method name="update*" read-only="false" isolation="READ_COMMITTED" rollback-for="Exception" />
			<tx:method name="delete*" read-only="false" isolation="READ_COMMITTED" rollback-for="Exception" />
			<tx:method name="execute*" read-only="false" isolation="READ_COMMITTED" rollback-for="Exception" />
			<tx:method name="add*" read-only="false" isolation="READ_COMMITTED" rollback-for="Exception" />
			<tx:method name="modify*" read-only="false" isolation="READ_COMMITTED" rollback-for="Exception" />
			<tx:method name="create*" read-only="false" isolation="READ_COMMITTED" rollback-for="Exception" />
			<tx:method name="increase*" read-only="false" isolation="READ_COMMITTED" rollback-for="Exception" />
			<tx:method name="subPoint*" read-only="false" isolation="READ_COMMITTED" rollback-for="Exception" />      
		</tx:attributes>
	</tx:advice>

	<aop:config>
		<aop:pointcut id="shopServiceOperation" expression="execution(* com.newstar.luckybee.*.service.*.*(..))" />
		<aop:advisor advice-ref="shopTxAdvice" pointcut-ref="shopServiceOperation" />
	</aop:config>
</beans>

 

第三.在src文件夹下面加入一个jta.properties文件. 
文件内容如下,这个文件是必须的.主是是设置atomikos启动的一些参数,比如日志的输出级别,日志文件的名称等. 

Java代码   收藏代码
  1. # SAMPLE PROPERTIES FILE FOR THE TRANSACTION SERVICE  
  2. # THIS FILE ILLUSTRATES THE DIFFERENT SETTINGS FOR THE TRANSACTION MANAGER  
  3. # UNCOMMENT THE ASSIGNMENTS TO OVERRIDE DEFAULT VALUES;  
  4.   
  5. # Required: factory implementation class of the transaction core.  
  6. # NOTE: there is no default for this, so it MUST be specified!   
  7. #   
  8. com.atomikos.icatch.service=com.atomikos.icatch.standalone.UserTransactionServiceFactory  
  9.   
  10.           
  11. # Set base name of file where messages are output   
  12. # (also known as the 'console file').  
  13. #  
  14. com.atomikos.icatch.console_file_name = tm.out  
  15.   
  16. # Size limit (in bytes) for the console file;  
  17. # negative means unlimited.  
  18. #  
  19. # com.atomikos.icatch.console_file_limit=-1  
  20.   
  21. # For size-limited console files, this option  
  22. # specifies a number of rotating files to   
  23. # maintain.  
  24. #  
  25. # com.atomikos.icatch.console_file_count=1  
  26.   
  27. # Set the number of log writes between checkpoints  
  28. #  
  29. # com.atomikos.icatch.checkpoint_interval=500  
  30.   
  31. # Set output directory where console file and other files are to be put  
  32. # make sure this directory exists!  
  33. #  
  34. # com.atomikos.icatch.output_dir = ./  
  35.   
  36. # Set directory of log files; make sure this directory exists!  
  37. #  
  38. # com.atomikos.icatch.log_base_dir = ./  
  39.   
  40. # Set base name of log file  
  41. this name will be  used as the first part of   
  42. # the system-generated log file name  
  43. #  
  44. com.atomikos.icatch.log_base_name = tmlog  
  45.   
  46. # Set the max number of active local transactions   
  47. # or -1 for unlimited.  
  48. #  
  49. # com.atomikos.icatch.max_actives = 50  
  50.   
  51. # Set the default timeout (in milliseconds) for local transactions  
  52. #  
  53. # com.atomikos.icatch.default_jta_timeout = 10000  
  54.   
  55. # Set the max timeout (in milliseconds) for local transactions  
  56. #  
  57. # com.atomikos.icatch.max_timeout = 300000  
  58.   
  59. # The globally unique name of this transaction manager process  
  60. # override this value with a globally unique name  
  61. #  
  62. com.atomikos.icatch.tm_unique_name = tm  
  63.       
  64. # Do we want to use parallel subtransactions? JTA's default  
  65. # is NO for J2EE compatibility  
  66. #  
  67. # com.atomikos.icatch.serial_jta_transactions=true  
  68.                       
  69. # If you want to do explicit resource registration then  
  70. # you need to set this value to false.  
  71. #  
  72. # com.atomikos.icatch.automatic_resource_registration=true    
  73.       
  74. # Set this to WARN, INFO or DEBUG to control the granularity  
  75. # of output to the console file.  
  76. #  
  77. com.atomikos.icatch.console_log_level=INFO  
  78.       
  79. # Do you want transaction logging to be enabled or not?  
  80. # If set to false, then no logging overhead will be done  
  81. # at the risk of losing data after restart or crash.  
  82. #  
  83. # com.atomikos.icatch.enable_logging=true  
  84.   
  85. # Should two-phase commit be done in (multi-)threaded mode or not?  
  86. # Set this to false if you want commits to be ordered according  
  87. # to the order in which resources are added to the transaction.  
  88. #  
  89. # NOTE: threads are reused on JDK 1.5 or higher.   
  90. # For JDK 1.4, thread reuse is enabled as soon as the   
  91. # concurrent backport is in the classpath - see   
  92. # http://mirrors.ibiblio.org/pub/mirrors/maven2/backport-util-concurrent/backport-util-concurrent/  
  93. #  
  94. # com.atomikos.icatch.threaded_2pc=false  
  95.   
  96. # Should shutdown of the VM trigger shutdown of the transaction core too?  
  97. #  
  98. # com.atomikos.icatch.force_shutdown_on_vm_exit=false  

第四.具体应用 
在dao中应用不同的JdbcTemplate即可。

http://gongjiayun.iteye.com/blog/1570111

 

分享到:
评论

相关推荐

    spring + JTA + atomikos 实现分布式事务

    通过理解JTA的基本原理,掌握Spring的事务管理机制,以及熟悉Atomikos的使用,开发者可以构建出高可用、强一致性的分布式系统。在实际操作中,还需要不断地调试和优化,以确保系统的稳定性和性能。

    Spring+MyBatis+Atomikos实现JTA分布式事务

    本文将深入探讨如何使用Spring、MyBatis和Atomikos实现JTA分布式事务。 首先,让我们理解JTA的概念。Java Transaction API(JTA)是Java EE规范之一,用于处理分布式环境中的事务。它允许应用程序在不同的资源管理...

    Springboot+Atomikos+Jpa+Mysql实现JTA分布式事务

    本文将详细讲解如何利用Spring Boot、Atomikos、JPA(Java Persistence API)以及MySQL来实现JTA(Java Transaction API)分布式事务。 首先,Spring Boot是一个轻量级的框架,它简化了基于Spring的应用程序开发...

    spring+atomikos+druid分布式事务

    使用spring + atomikos+druid配置的分布式事务demo,两种数据源配置方式都可以,使用junit测试没问题,案例中使用的mysql数据库是8.0.11版本,版本不同请自行修改pom.xml和jdbc.properties

    第二部分spring+hibernate+jta 分布式事务Demo

    Spring、Hibernate和JTA(Java Transaction API)是Java开发者在构建分布式事务解决方案时常用的三大技术。本示例“第二部分spring+hibernate+jta 分布式事务Demo”将帮助我们理解如何在这三个框架之间协同工作,...

    spring+atomikos+druid分布式事务Demo

    本示例"spring+atomikos+druid分布式事务Demo"聚焦于如何在Spring框架中利用Atomikos和Druid来处理分布式事务。接下来,我们将深入探讨这三个组件以及它们在实现分布式事务中的作用。 Spring是一个广泛使用的Java...

    java+spring+mybatis+mysql+RuoYi-atomikos-实现分布式事务.zip

    本项目"java+spring+mybatis+mysql+RuoYi-atomikos-实现分布式事务.zip"是一个基于若依(RuoYi)框架改造的多模块分布式事务解决方案,它利用了Atomikos这一强大的分布式事务管理器。以下将详细解析这个项目的知识点...

    Spring boot+Atomikos+JTA+Hibernate+mybatis+MySQL实现分布式事务+多数据源

    总之,本案例展示了如何在Spring Boot环境下,利用Atomikos、JTA、Hibernate和MyBatis,配合MySQL数据库,构建一个健壮的分布式事务处理系统。这样的系统不仅提高了系统的扩展性和可维护性,还确保了在分布式环境下...

    SSH + atomikos 打造Java分布式事务

    SSH + atomikos 打造Java分布式事务 web工程 直接导入myeclipse即可运行 数据库可参照mutilDataSource/model下的实体类进行创建(两个表)数据库使用的是oracle

    Spring3.1+Hibernate4.1+Atomikos3.9+MySql5.1+Tomcat5/6/7实现JTA事务管理

    标题中的“Spring3.1+Hibernate4.1+Atomikos3.9+MySql5.1+Tomcat5/6/7实现JTA事务管理”揭示了一个集成开发环境,用于构建分布式、事务一致性的Java应用程序。这个组合利用了Spring框架的依赖注入和AOP(面向切面...

    Spring boot+Atomikos+JTA+Hibernate+MySQL实现分布式事务+多数据源(可运行)

    本项目使用Spring Boot、Atomikos、JTA(Java Transaction API)、Hibernate和MySQL来实现分布式事务处理和多数据源管理,以确保在多个数据库操作之间保持事务的ACID特性。 首先,Spring Boot作为微服务开发的主流...

    SpringBoot+Atomikos分布式事务及多数据源动态切换,两种demo

    Spring Boot作为轻量级的Java开发框架,结合Atomikos这样的分布式事务管理器,可以有效地解决这些问题。本文将深入探讨如何在Spring Boot项目中实现Atomikos分布式事务以及动态数据源切换的两种示例。 首先,我们...

    Spring Boot+Druid+Mybatis实现JTA分布式事务

    5. 开启Spring事务管理:在Spring的配置类中,启用@EnableTransactionManagement注解,配置PlatformTransactionManager为Atomikos的事务管理器。 6. 编写业务代码:在需要进行分布式事务的Service层方法上,使用@...

    spring+druid+AtomikosDataSource实现多数据源切换及分布式事务控制

    在Spring中集成Atomikos,可以实现全局的分布式事务管理,确保在分布式环境下的数据一致性。 4. **配置多数据源** 在Spring的XML配置文件中,我们需要为每个数据源创建一个`AtomikosDataSourceBean`实例,并配置...

    spring4+atomikos实现分布式事务

    在这个案例中,我们将深入探讨如何使用Spring 4与Atomikos来实现跨数据库的分布式事务管理。 Atomikos是一款开源的事务处理监控器(Transaction Processing Monitor,TPM),它支持JTA(Java Transaction API)标准...

    Spring3.0+Hibernate+Atomikos多数据源分布式事务管理

    总的来说,Spring 3.0、Hibernate和Atomikos的组合提供了一种强大且灵活的方式来处理多数据源的分布式事务,使开发者能够构建高度可用和可扩展的企业级应用。通过深入理解这些技术的原理和实践,开发者可以更好地...

    Spring Boot + Druid + Mybatis + Atomikos 配置多数据源 并支持分布式事务

    本教程将探讨如何利用Spring Boot、Druid、Mybatis以及Atomikos来配置多数据源并实现分布式事务。 首先,Spring Boot是Java生态系统中的一个流行框架,它简化了设置和配置过程,使得开发人员可以快速启动新项目。在...

    【java】基于SpringBoot+Druid+MybatisPlus+Atomikos分布式事务_pgj.zip

    Atomikos提供了JTA(Java Transaction API)接口,可以与Spring的声明式事务管理无缝集成。 本压缩包文件“springboot-distributed-transaction-master”提供了一个SpringBoot项目的基础结构,其中包含了上述技术和...

Global site tag (gtag.js) - Google Analytics