`
huangjinping
  • 浏览: 49504 次
  • 性别: Icon_minigender_2
  • 来自: 广州
社区版块
存档分类
最新评论

Spring多数据源JTA事务

阅读更多

    当我们的项目中需要应用到多个数据源时,事务的管理就很重要了。而Spring的JTA事务就很好的帮助了我们进行了多数据源的事务处理。
通过集成JOTM,直接在Spring中使用JTA事务
    JOTM(Java Open Transaction Manager)是ObjectWeb的一个开源JTA实现,它本身也是开源应用程序服务器JOnAS(Java Open Application Server)的一部分,为其提供JTA分布式事务的功能。
    Spring 2.0附带的依赖类库中虽然包含jotm类库,但是并不完整,你可以到http://jotm.objectweb.org下载完全版的JOTM。
    Spring为JOTM提供了一org.springframework.transaction.jta.JotmFactoryBean支持类,通过该支持类可以方便地创建JOTM本地实例。
    下面我们通过实例来使用JTA事务:
1.从http://www.findjar.com中找到以下jar包添加到你的项目中:
jotm-core-2.1.5.jar
jotm-2.0.10.jar
carol-3.0.7.jar
carol-interceptors-1.0.1.jar
xapool-1.5.0.jar
jotm-carol-1.5.3.jar
jotm-core-2.1.5.jar
jotm-jrmp-stubs-2.0.10.jar
transactions-api-3.2.3.jar
atomikos-util-3.2.3.jar
transactions-jta-3.2.3.jar
transactions-3.2.3.jar

connector.jar

2.添加JOTM配置文件carol.properties,放到类路径下:
两种方法:(1).从carol-3.0.7.jar中复制carol-defaults.properties到项目中,将文件名改为carol.properties即可
(2).自已手动编写,配置文件内容如下:
    #JNDI调用协议
    carol.protocols=jrmp
    #不使用CAROL JNDI封装器
    carol.start.jndi=false
    #不启动命名服务器
    carol.start.ns=false
    # Indicate if a protocol-independent environment is used.
    # Otherwise the environment of the default protocol is used.
    carol.multi.env = true
3.编写spring配置文件

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
xmlns:dwr="http://www.directwebremoting.org/schema/spring-dwr" 
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:ehcache="http://www.springmodules.org/schema/ehcache" 
xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-2.5.xsd 
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-2.5.xsd 
    http://www.springmodules.org/schema/ehcache 
    http://www.springmodules.org/schema/cache/springmodules-ehcache.xsd 
    http://www.directwebremoting.org/schema/spring-dwr 
    http://www.directwebremoting.org/schema/spring-dwr-3.0.xsd" 
default-autowire="byName" default-lazy-init="true"> 
<!-- ①JOTM本地实例--> 
<bean id="jotm" class="org.springframework.transaction.jta.JotmFactoryBean" /> 
<!--    ②JTA事务管理器--> 
<bean id="txManager" class="org.springframework.transaction.jta.JtaTransactionManager"><!--②-1:指定userTransaction属性--> 
<property name="userTransaction" ref="jotm" />  
</bean> 
<!--③XAPool配置,内部包含了一个XA数据源,对应topicdb数据库--> 
<bean id="topicDS" class="org.enhydra.jdbc.pool.StandardXAPoolDataSource" 
destroy-method="shutdown"> 
<property name="dataSource"> 
<!--③-1:内部XA数据源--> 
<bean class="org.enhydra.jdbc.standard.StandardXADataSource" 
destroy-method="shutdown"> 
<property name="transactionManager" ref="jotm" /> 
<property name="driverName" value="com.mysql.jdbc.Driver" /> 
<property name="url" value="jdbc:MySQL://localhost:3306/topicdb" /> 
</bean> 
</property> 
<property name="user" value="root" /> 
<property name="password" value="root" /> 
</bean> 
<!--④按照③相似的方式配置另一个XAPool,对应postdb数据库,--> 
<bean id="postDS" class="org.enhydra.jdbc.pool.StandardXAPoolDataSource" 
destroy-method="shutdown"> 
<property name="dataSource"> 
<bean class="org.enhydra.jdbc.standard.StandardXADataSource" 
destroy-method="shutdown"> 
<property name="transactionManager" ref="jotm" /> 
<property name="driverName" value="com.mysql.jdbc.Driver" /> 
<property name="url" value="jdbc:mysql://localhost:3306/postdb" /> 
</bean> 
</property> 
<property name="user" value="root" /> 
<property name="password" value="root" /> 
</bean> 
<!--⑤配置访问topicDB数据源的Spring JDBC模板--> 
<bean id="topicTemplate" 
class="org.springframework.jdbc.core.JdbcTemplate"> 
<property name="dataSource" ref="topicDS" /> 
</bean> 


<!--⑥配置访问postDB数据源的Spring JDBC模板--> 
<bean id="postTemplate" 
class="org.springframework.jdbc.core.JdbcTemplate"> 
<property name="dataSource" ref="postDS" /> 
 </bean> 

<!--⑦基于topicTemplate数据源的topicDao--> 
<bean id="topicDao" class="com.spring.service.impl.TopicDaoImpl"> 
<property name="jdbcTemplate" ref="topicTemplate" /> 
</bean> 
<!--⑧基于postTemplate数据源的postDao--> 
<bean id="postDao" class="com.spring.service.impl.PostDaoImpl"> 
<property name="jdbcTemplate" ref="postTemplate" /> 
</bean> 
<!--⑨进行跨数据库JTA事务的业务类--> 
<bean id="bbtForum" class="com.spring.service.impl.BbtForumImpl"> 
<property name="topicDao" ref="topicDao" /> 
<property name="postDao" ref="postDao" /> 
</bean> 
<!--⑩对BbtForumImpl业务类中的@Transaction注解进行驱动--> 
<tx:annotation-driven transaction-manager="txManager" /> 

</beans>

 

 上面的配置文件中是操作数据库表是用jdbcTemplate,这个我觉得不好用,缺点是不能用Hql语句,而下面的spring配置文件中使用的是HibernateDaoSupport,内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:dwr="http://www.directwebremoting.org/schema/spring-dwr"
	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:ehcache="http://www.springmodules.org/schema/ehcache"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-2.5.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
    http://www.springmodules.org/schema/ehcache
    http://www.springmodules.org/schema/cache/springmodules-ehcache.xsd
    http://www.directwebremoting.org/schema/spring-dwr
    http://www.directwebremoting.org/schema/spring-dwr-3.0.xsd"
	default-autowire="byName" default-lazy-init="true">

	<bean id="lobHandler" class="org.springframework.jdbc.support.lob.DefaultLobHandler"
		lazy-init="true" />
	<!-- ///////////////////JOTM本地实例 ////////////////////////-->
	<bean id="jotm" class="org.springframework.transaction.jta.JotmFactoryBean" />

	<!-- ///////////////////JTA事务管理器 ////////////////////////-->
	<bean id="txManager"
		class="org.springframework.transaction.jta.JtaTransactionManager">
		<property name="userTransaction" ref="jotm" />
	</bean>


	<!-- ///////////////////jta管理数据源一 ////////////////////////-->
	<bean id="oneDS" class="org.enhydra.jdbc.pool.StandardXAPoolDataSource"
		destroy-method="shutdown">
		<property name="dataSource">
			<bean class="org.enhydra.jdbc.standard.StandardXADataSource"
				destroy-method="shutdown">
				<property name="transactionManager" ref="jotm" />
				<property name="driverName" value="com.mysql.jdbc.Driver" />
				<property name="url" value="jdbc:mysql://127.0.0.1:3306/topicdb" />
			</bean>
		</property>
		<property name="maxSize">
			<value>10</value>
		</property>
		<property name="user">
			<value>root</value>
		</property>
		<property name="password">
			<value>root</value>
		</property>
	</bean>


	<!-- ///////////////////jta管理数据源二 ////////////////////////-->
	<bean id="twoDS" class="org.enhydra.jdbc.pool.StandardXAPoolDataSource"
		destroy-method="shutdown">
		<property name="dataSource">
			<bean class="org.enhydra.jdbc.standard.StandardXADataSource"
				destroy-method="shutdown">
				<property name="transactionManager" ref="jotm" />
				<property name="driverName" value="com.mysql.jdbc.Driver" />
				<property name="url" value="jdbc:mysql://127.0.0.1:3306/postdb" />
			</bean>
		</property>
		<property name="maxSize">
			<value>10</value>
		</property>
		<property name="user">
			<value>root</value>
		</property>
		<property name="password">
			<value>root</value>
		</property>
	</bean>

	<bean id="userTarget" class="com.spring.service.impl.UserServiceTeImpl">
		<property name="baseDao">
			<ref bean="baseDao1" />
		</property>
	</bean>
	<bean id="userService" class="org.springframework.aop.framework.ProxyFactoryBean">
		<property name="proxyInterfaces">
			<value>com.spring.service.UserServiceTe
			</value>
		</property>
	</bean>

	<bean id="sessionFactory1"
		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="mappingResources">
			<list>
				<value>com/spring/mappings/Post.hbm.xml</value>
			</list>
		</property>
		<property name="lobHandler" ref="lobHandler" />
		<property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />
		<property name="dataSource" ref="twoDS"></property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
				<prop key="hibernate.connection.isolation">3</prop>
				<prop key="show_sql">true</prop>
				<prop key="format_sql">true</prop>
			</props>
		</property>
	</bean>

	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="mappingResources">
			<list>
				<value>com/spring/mappings/Topic.hbm.xml</value>
			</list>
		</property>
		<property name="lobHandler" ref="lobHandler" />
		<property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />
		<property name="dataSource" ref="oneDS"></property>
	</bean>
	<!-- HIBERNATE数据库管理实现类 -->
	<bean id="baseDao1" class="com.spring.dao.BaseDaoImpl">
		<property name="sessionFactory">
			<ref bean="sessionFactory1" />
		</property>
	</bean>

	<bean id="baseDao" class="com.spring.core.dao.impl.BaseDaoImpl">
		<property name="sessionFactory">
			<ref bean="sessionFactory" />
		</property>
	</bean>
	<tx:annotation-driven transaction-manager="txManager"
		proxy-target-class="true" />
</beans>

 

完成上面的步骤后,你就可以进行测试了。我自己测试的是OK的

分享到:
评论

相关推荐

    Springboot 动态多数据源 jta分布式事务

    本资源针对的是Spring Boot动态多数据源和JTA(Java Transaction API)分布式事务的实现,对于初学者来说非常实用。下面我们将深入探讨这些知识点。 首先,让我们了解一下Spring Boot的多数据源。在许多业务场景下...

    Spring多数据源分布式事务管理

    在大型分布式系统中,往往需要处理多个数据源,这就涉及到了Spring多数据源的配置和管理。同时,为了保证数据的一致性,分布式事务的管理也是必不可少的。在这个场景下,Atomikos作为一款开源的JTA(Java ...

    多数据源事务jta测试

    1. **配置数据源**:为每个数据库创建独立的数据源,并确保它们支持JTA事务。 2. **注册XAResource**:将每个数据源作为XAResource注册到事务协调器。 3. **开始事务**:通过UT接口启动一个新的全局事务。 4. **执行...

    Spring配置JTA事务管理

    本篇文章将深入探讨如何在Spring中配置JTA事务管理,以实现跨数据库和资源的事务一致性。 首先,让我们了解JTA的基本概念。JTA是一个规范,它定义了接口和API,使得应用程序可以控制跨越多个数据存储(如数据库、...

    springboot整合jta实现多数据源事务管理

    在Spring Boot应用中,整合JTA(Java Transaction API)实现多数据源事务管理是一个常见的需求,特别是在分布式系统中,为了确保数据的一致性和完整性。本文将深入探讨如何配置和使用Spring Boot与JTA来管理多个...

    springboot整合JTA组件,多数据源事务管理

    XA协议是数据库层面的一套分布式事务管理的规范,JTA是XA协议在Java中的实现,多个数据库或是消息厂商实现JTA接口,开发人员只需要调用SpringJTA接口即可实现JTA事务管理功能。 JTA事务比JDBC事务更强大。一个JTA事务...

    springboot多数据源即分布式事务解决方案

    在现代企业级应用开发中,随着业务规模的扩大和数据量的增长,往往需要处理来自多个数据源的数据。SpringBoot作为一款轻量级的框架,提供了便捷的多数据源配置和分布式事务管理方案,使得开发者能够高效地管理和操作...

    在Spring中使用JTA事务管理

    在Spring框架中,JTA(Java Transaction API)事务管理是一种用于处理分布式事务的高级机制,它允许应用程序在多个数据源之间进行协调的事务操作。本文将详细介绍如何在Spring中使用JTA事务管理,包括通过集成JOTM...

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

    Atomikos提供的实现使得JTA能够在不同的数据源和持久化层之间进行协调,确保ACID(原子性、一致性、隔离性和持久性)属性。 4. **源码分析**: 深入理解这个Demo,我们可以查看Atomikos的源码,了解其如何处理事务...

    spring+mybatis+jta实现多数据源的分布式事物(代码可以直接运行)

    本示例项目"spring+mybatis+jta实现多数据源的分布式事务"提供了一种解决方案,利用Spring框架、MyBatis持久层框架以及Java Transaction API (JTA)来处理跨多个数据源的事务一致性。以下是对这一技术栈及其应用的...

    springboot多数据源即分布式事务解决方案,添加对多线程的支持

    本教程将深入探讨如何在Spring Boot环境下实现多数据源操作及分布式事务管理,并加入对多线程的支持。 首先,我们来理解多数据源的概念。在大型系统中,往往需要连接多个数据库,如主库、从库、测试库等。Spring ...

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

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

    分布式数据源,数据源的动态寻找,分布式事务JTA实现-spring-jta-mybatis.zip

    在分布式事务场景下,Spring的声明式事务管理可以简化代码,使得在处理多数据源事务时,只需在方法上添加@Transactional注解,事务的开始、提交、回滚等操作将自动进行。 具体到这个项目"spring-jta-mybatis-master...

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

    Atomikos是一款开源的JTA事务管理器,它支持分布式事务处理,使得在多个数据源之间进行事务操作成为可能。JTA是Java平台中的标准API,用于管理跨越多个资源(如数据库或消息队列)的事务。Atomikos通过实现JTA规范,...

    多数据源 更新 spring jta java jotm

    总结来说,Spring、JTA和JOTM的组合提供了在多数据源环境中高效且可靠的事务处理能力。通过Spring的`AbstractRoutingDataSource`和JTA,我们可以轻松地管理多个数据源,并通过JOTM保证分布式事务的一致性。在实际...

    JTA事务源码示例

    Spring+iBatis+JOTM实现JTA事务: 如何处理跨库事物:spring + jtom 的jta事务是个很好的选择. 这个源码示例非常不错,包括所有的源码和jar包,下载后eclipse 或 myeclipse 导入就能用。 里面有详细的说明和注释,...

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

    在现代企业级应用开发中,分布式事务处理和多数据源管理是常见的需求。Spring Boot作为轻量级的Java开发框架,结合Atomikos这样的分布式事务管理器,可以有效地解决这些问题。本文将深入探讨如何在Spring Boot项目中...

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

    AtomikosDataSource支持JTA事务,这意味着我们可以在同一个事务中操作多个数据源。Spring的`@Transactional`注解可以用来开启和管理这些分布式事务,确保事务的ACID特性。 7. **代码示例** 创建多数据源的配置...

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

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

    使用springboot+jta+atomikos配置多数据源事务

    本教程将详细介绍如何使用Spring Boot结合JTA(Java Transaction API)和Atomikos来配置多数据源事务。 首先,我们需要理解JTA的含义。JTA是Java平台的标准,用于管理跨多个数据源的分布式事务。它允许应用程序在一...

Global site tag (gtag.js) - Google Analytics