<?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"
xmlns:dwr="http://www.directwebremoting.org/schema/spring-dwr"
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.directwebremoting.org/schema/spring-dwr http://www.directwebremoting.org/schema/spring-dwr-3.0.xsd">
<context:annotation-config/>
<context:component-scan base-package="code.main.bean"/>
<context:property-placeholder location="classpath:jdbc.properties"/>
<bean id="atomikosUserTransaction" class="com.atomikos.icatch.jta.UserTransactionImp">
<description>UserTransactionImp</description>
<property name="transactionTimeout" value="300"/>
</bean>
<bean id="atomikosTransactionManager" class="com.atomikos.icatch.jta.UserTransactionManager" init-method="init" destroy-method="close">
<description>UserTransactionManager</description>
<property name="forceShutdown" value="true"/>
</bean>
<bean id="transactionManager"
class="org.springframework.transaction.jta.JtaTransactionManager">
<description>JtaTransactionManager</description>
<property name="transactionManager" ref="atomikosTransactionManager"/>
<property name="userTransaction" ref="atomikosUserTransaction"/>
<property name="allowCustomIsolationLevels" value="true"/>
</bean>
<bean id="dataSource1" class="com.atomikos.jdbc.AtomikosDataSourceBean">
<property name="uniqueResourceName" value="mysql_jndi1"/>
<property name="xaDataSourceClassName" value="com.mysql.jdbc.jdbc2.optional.MysqlXADataSource"/>
<property name="poolSize" value="3"/>
<property name="maxPoolSize" value="100"/>
<property name="minPoolSize" value="1"/>
<property name="xaProperties">
<props>
<prop key="user">root</prop>
<prop key="password">1234</prop>
<prop key="url">jdbc:mysql://localhost:3306/atomikos1?useUnicode=true&characterEncoding=UTF-8</prop>
</props>
</property>
<property name="testQuery" value="select 1"/>
</bean>
<bean id="dataSource2" class="com.atomikos.jdbc.AtomikosDataSourceBean">
<property name="uniqueResourceName" value="mysql_jndi2"/>
<property name="xaDataSourceClassName" value="com.mysql.jdbc.jdbc2.optional.MysqlXADataSource"/>
<property name="poolSize" value="3"/>
<property name="maxPoolSize" value="100"/>
<property name="minPoolSize" value="1"/>
<property name="xaProperties">
<props>
<prop key="user">root</prop>
<prop key="password">1234</prop>
<prop key="url">jdbc:mysql://localhost:3306/atomikos2?useUnicode=true&characterEncoding=UTF-8</prop>
</props>
</property>
<property name="testQuery" value="select 1"/>
</bean>
<bean id="sessionFactory1"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource1" />
<property name="namingStrategy">
<bean class="org.hibernate.cfg.ImprovedNamingStrategy"/>
</property>
<property name="packagesToScan" value="code.main.bean.*" />
<property name="hibernateProperties">
<value>
hibernate.dialect=${hibernate.dialect}
hibernate.hbm2ddl.auto=${hibernate.hbm2ddl.auto}
hibernate.show_sql=${hibernate.show_sql}
hibernate.format_sql=${hibernate.format_sql}
</value>
</property>
</bean>
<bean id="sessionFactory2"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource2" />
<property name="namingStrategy">
<bean class="org.hibernate.cfg.ImprovedNamingStrategy"/>
</property>
<property name="annotatedClasses">
<list>
<value>code.main.bean.pojo.User</value>
</list>
</property>
<property name="hibernateProperties">
<value>
hibernate.dialect=${hibernate.dialect}
hibernate.hbm2ddl.auto=${hibernate.hbm2ddl.auto}
hibernate.show_sql=${hibernate.show_sql}
hibernate.format_sql=${hibernate.format_sql}
</value>
</property>
</bean>
<bean id="hibernateTemplate1" class="org.springframework.orm.hibernate3.HibernateTemplate" >
<property name="sessionFactory" ref="sessionFactory1" />
</bean>
<bean id="hibernateTemplate2" class="org.springframework.orm.hibernate3.HibernateTemplate" >
<property name="sessionFactory" ref="sessionFactory2" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
配置了2个mysql的数据库,新建了2个实体类:
package code.main.bean.pojo;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="t_user")
public class User {
private int id;
private String name;
private String password;
private String email;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Column(length=20)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Column(length=20)
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Column(length=100)
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
和
package code.main.bean.pojo;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="t_person")
public class Person {
private int id;
private String personName;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Column(length=20)
public String getPersonName() {
return personName;
}
public void setPersonName(String personName) {
this.personName = personName;
}
}
3、启动项目,生成数据库如下图(有2个数据库atomikos1和atomikos2,一个有2涨表,1个有一张表,请看上面配置文件中sessionFactory中的配置就能发现,一个是映射包,一个是映射单个类,配置方式有点不一样):
4、插入一些数据来测试一下,多个数据库的跨事务问题,
首先写了一个hibernate的dao,就是增删改查(略,但是有一点,因为配置文件中配置了2个sessionFactory,所以dao层要写2个实现类,对应不同的sessionFactory)。。。
第一个实现类注入的是hibernateTemplate1这个类,配置文件中有定义。
@Resource(name="hibernateTemplate1")
private HibernateTemplate template;
第一个实现类如下,也就是说,多个数据库就要对应多个实现类。
@Resource(name="hibernateTemplate2")
private HibernateTemplate template;
commonDao就是常用的操作的父接口,
我们自己的实体类的只要继承它就可以了。
5、service层的写法:
@Service("userService")
@Transactional
public class UserServiceImpl implements UserService {
@Resource(name="userDao1")
private UserDao dao1;
@Resource(name="userDao2")
private UserDao dao2;
public void save(User o){
User user = new User();
user.setEmail(o.getEmail());
user.setName(o.getName());
user.setPassword(o.getPassword());
User user1 = new User();
user1.setEmail(o.getEmail());
user1.setName(o.getName());
user1.setPassword(o.getPassword());
dao1.add(user1);
dao2.add(user);
}
}
注入2个userDao,看我的保存的方法:
注意:传进来一个user对象,本来可以直接保存的,但是我new了2个,分别保存,这是因为这里有一个小问题:
当你没有分别new的时候,id是自动分配的,只会分配一次,假如你的2个数据库保存的时候id不是同一个,往数据库里面插入的时候就为违反自动增长的那个顺序,从而报错。
6、controller的写法:
@Controller
@RequestMapping("/user")
public class UserController {
@Resource(name="userService")
private UserService userService;
@RequestMapping("toAdd")
public String toAdd(){
System.out.println("进入添加用户页面");
return "/user/add";
}
@RequestMapping("add")
public String add(User user) {
try {
userService.save(user);
} catch (Exception e) {
e.printStackTrace();
}
return "/user/add";
}
}
看了就行了,此处比较简单。
7、页面
<body>
<form action="${app}/user/add" method="post">
用户名: <input type="text" name="name"/>
密 码:<input type="text" name="password"/>
email: <input type="text" name="email"/>
<input type="submit" value="添加用户"/>
<form>
<hr/>
<a href="${app}/user/list">用户列表</a>
</body>
</html>
8、测试结果
保存的时候没有问题。
我为了测试2个数据库事务的问题,把其中一个数据库的表的email字段改成了唯一约束,插入的时候显然报错,后面一个数据库也没有插入成功, 说明事务起作用了。
截个图看下:
看,前6条数据都一样,插入式没有问题的。
后面我又用我自己的电脑,和别的机器上的数据库做了一下测试,第一张结果图中的部分数据就是测试的结果,
其实这个就已经做到了 不同的服务器上的数据库的事物同步。
后来听说jotm配置jndi也能实现多个数据库,我后来网上找了一点例子写了一下,发现保存数据没有问题,但是事物没有控制到,
估计是我写的不好,所以没有成功。
第一次写博客,写的不好,请不要骂我,谢谢~
相关推荐
Spring boot+Atomikos+JTA+Hibernate+MySQL实现分布式事务+多数据源,分别向两个不同的数据里面插入数据同时失败和成功,调用接口方式原理一样。
Spring+Mybatis+Atomikos实现JAVA初始化并控制多个数据源+分布式事务的一个DEMO,内涵源代码,以及一篇关于该内容的博客
Spring Boot:mybatis-plus + atomikos + druid 实现不同实例数据库的多数据源配置和分布式事务管理(demo项目),想到工作上可能会用到多数据源,但是自己在这方面并不是很熟悉,于是在网上查阅了很多文章,结果...
springboot+Atomikos+jpa+mysql的JTA分布式事务实现,本案例涉及到2个数据库,预期结果,在同一个事务中,两个库的状态一致
由于网上的多数据源事务的帖子大多是2010年以前的,现在spring都已经到4.X了,有些类已经弃用了。 原先很多都是用jotm实现的,但是由于spring的升级,totm的本地化实例那个类已经找不到了,所以我使用了atomikos。 ...
开发工具:MyEclipse10, 数据库:Mysql, demo中使用了2个数据源,2个不同的Mysql数据库。 注:不同数据库dataSource的配置是不一样的
spring+mybatis+mysql+atomikos,实现一个简单的多数据源事物控制程序
Atomikos,是一个基于Java的开源事务管理器,提供了事务管理和连接池,不需要应用服务器支持,支持JDBC和JMS事务,能提供对Spring,Hibernate的集成,有兴趣多了解的可以直接参考说明文当,有详细的介绍和使用说明。
开发工具:MyEclipse10 数据库:DB2,Mysql demo中使用了2个数据源,1个DB2数据库,1个Mysql数据库。 注:不同数据库dataSource的配置是不一样的
同一个服务里面多个数据源事务事务一致性
Spring-Boot-Api-Starter简介Spring-Boot-Api-Starter是一个基于SpringBoot ,快速构建RESTful API工程的脚手架,支持多数据源配置,分布式事务;快速生成各模块的基础代码,极大地提高了开发效率,使团队代码风格...
集成Atomikos支持分布式事务,以及支持多数据源配置(待集成) 集成Jetcache分布式缓存(最强大的缓存工具,没有一个) 完备的请求日志体系(按需打印,定位排查,敏感词过滤) 基于CompletableFu
JMay是什么JMay是一款基于Spring、SpringMVC、Mybatis、Atomikos集成框架,用于快速搭建跨数据库、分布式事务(XA规范)支持...4.多数据源支持,AOP动态数据源切换。基于Atomikos,分布式事务支持(需要数据库支持XA规范)
配置两个数据源 iii. 67.3. 使用Spring Data仓库 iv. 67.4. 从Spring配置分离@Entity定义 v. 67.5. 配置JPA属性 vi. 67.6. 使用自定义的EntityManagerFactory vii. 67.7. 使用两个EntityManagers viii. 67.8. 使用...