当我们的项目中需要应用到多个数据源时,事务的管理就很重要了。而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的
分享到:
相关推荐
Spring多数据源分布式事务管理/springmvc+spring+atomikos[jta]+druid+mybatis
在Spring中使用JTA事务管理 1 通过集成JOTM,直接在Spring中使用JTA事务 1.1. 将JOTM以下类库添加到类路径中 1.2. 编写JOTM配置文件,放到类路径下 1.3. 在MySQL上建立两个数据库 1.4. 在Spring配置文件中配置JOTM ...
该文件是给什么都不会的小白,下载下来进行简单的配置就可以运行,可以到https://blog.csdn.net/qq_43314669/article/details/106733829 进行在线观看
Spring boot+Atomikos+JTA+Hibernate+MySQL实现分布式事务+多数据源,分别向两个不同的数据里面插入数据同时失败和成功,调用接口方式原理一样。
Spring+iBatis+JOTM实现JTA事务: 如何处理跨库事物:spring + jtom 的jta事务是个很好的选择. 这个源码示例非常不错,包括所有的源码和jar包,下载后eclipse 或 myeclipse 导入就能用。 里面有详细的说明和注释,...
博客上有很多类似的,很多都有点小错误。还是自己搭建测试的好用。resource中有sql,自己创建两个数据库导入sql,即可运行test测试
1.多数据源配置,采用方式:直接配置两个不同的数据源,不同的sessionFactory。 2.Spring+Jotm整合实现JTA分布式事务,应用场景如转账等,同一事务内完成db1用户加100元、db2用户减100元。 3.Spring+Junit4单元...
本用例基于 Spring Boot + Druid + Mybatis 配置多数据源,并采用 JTA 实现分布式事务。
Spring+iBatis+JOTM实现JTA事务 spring ibatis jotm 分布 事务 多数据源
分布式数据源,数据源的动态寻找,分布式事务JTA实现-spring-jta-mybatis
spring+jotm 多数据源事务管理(三)JNDI+Tomcat 首先需要将jotm相关jar包加入到tomcat中,如下: o jotm.jar o jotm_jrmp_stubs.jar o ow_carol.jar o jta.jar o jta-spec1_0_1.jar o jts1_0.jar o objectweb-...
spring boot下学习mybatis+mysql使用jta对多数据源事务进行整合
atomikos实现多数据源支持分布式事务管理(spring、tomcat、JTA) 结合spring 和durid进行配置,
springboot+Atomikos+jpa+mysql的JTA分布式事务实现,本案例涉及到2个数据库,预期结果,在同一个事务中,两个库的状态一致
例子虽小,可覆盖面广,设计spring载入.properties文件,spring配置jta和jotm分布式事务,设置数据源连接池,注解事务驱动。功能是在mysql上建立了两个数据库分别为dbone和dbtwo,在dbone里有表tb1,表中只有一个字段...
同一个服务里面多个数据源事务事务一致性
同一个服务里面多个数据源事务事务一致性
同一个服务里面多个数据源事务事务一致性
微服务使用seata保证事务事务一致性
SSM多数据源同包