Distributed transactions with multiple databases, Spring Boot, Spring Data JPA and Atomikos

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

A couple of weeks ago I was evaluating the possibility to use Spring Boot, Spring Data JPA and Atomikos for distributed transactions involving multiple databases. After looking at the Spring blog article (which involves one database and ActiveMQ) and having done some attempts, I could not get it to work with two databases. The configuration seemed fine, but the Entity Manager did not get notified when persisting my entities. So I wrote this question on StackOverflow, which has been answered directly by Dave Syer and Oliver Gierke. This post is to share and discuss the solution.

Description of the case and entities model

We want to be able to save two entities at the same time into two different databases; the operation must be transactional. So, in this example, we have a Customer entity, which is persisted in the first database, and an Order entity which is persisted in the second database. The two entities are very simple, as they serve only as a demonstration.

The resulting implementation is the following. It's worth noting that they belong to two different packages, for two main reasons:

  1. it's a logical separation that gives order to the project
  2. each repository will scan packages containing only entities that it will be going to manage
package com.at.mul.domain.customer;import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;import lombok.Data;
import lombok.EqualsAndHashCode;@Entity
@Table(name = "customer")
@Data
@EqualsAndHashCode(exclude = { "id" })
public class Customer {@Id@GeneratedValue(strategy = GenerationType.AUTO)private Integer id;@Column(name = "name", nullable = false)private String name;@Column(name = "age", nullable = false)private Integer age;}
package com.at.mul.domain.order;import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;import lombok.Data;
import lombok.EqualsAndHashCode;@Entity
@Table(name = "orders")
@Data
@EqualsAndHashCode(exclude = { "id" })
public class Order {@Id@GeneratedValue(strategy = GenerationType.AUTO)private Integer id;@Column(name = "code", nullable = false)private Integer code;@Column(name = "quantity", nullable = false)private Integer quantity;}
See Lombok for annotations like @Data and @EqualsAndHashCode

Write repositories interfaces

Also in this case it's standard, the only thing to notice is that I put the two interfaces in two different packages. The reason is explained in the next step.

package com.at.mul.repository.customer;import org.springframework.data.jpa.repository.JpaRepository;import com.at.mul.domain.customer.Customer;public interface CustomerRepository extends JpaRepository<Customer, Integer> {}
package com.at.mul.repository.order;import org.springframework.data.jpa.repository.JpaRepository;import com.at.mul.domain.order.Order;public interface OrderRepository extends JpaRepository<Order, Integer> {}

Write configuration classes

This is where it becomes interesting. The @DependsOn("transactionManager") annotation is not mandatory, but I needed this to get rid of several warnings at tests (or application) startup, like WARNING: transaction manager not running? in the logs. The next annotation @EnableJpaRepositories is more important:
  • it specifies which are the packages to scan for annotated components (repository interfaces), and in my case I wanted only repositories related to the customer (and conversely to the order).
  • it specifies which is the entity manager to be used to manage entities, in my case the customerEntityManager for customer related operations and orderEntityManager for order related operations
  • it specifies the transaction manager to be used, in my case the transactionManager defined in the MainConfig class. This needs to be the same for every @EnableJpaRepositories to get distributed transactions working
package com.at.mul;import java.util.HashMap;import javax.sql.DataSource;import org.h2.jdbcx.JdbcDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;import com.at.mul.repository.customer.CustomerDatasourceProperties;
import com.atomikos.jdbc.AtomikosDataSourceBean;@Configuration
@DependsOn("transactionManager")
@EnableJpaRepositories(basePackages = "com.at.mul.repository.customer", entityManagerFactoryRef = "customerEntityManager", transactionManagerRef = "transactionManager")
@EnableConfigurationProperties(CustomerDatasourceProperties.class)
public class CustomerConfig {@Autowiredprivate JpaVendorAdapter jpaVendorAdapter;@Autowiredprivate CustomerDatasourceProperties customerDatasourceProperties;@Bean(name = "customerDataSource", initMethod = "init", destroyMethod = "close")public DataSource customerDataSource() {JdbcDataSource h2XaDataSource = new JdbcDataSource();h2XaDataSource.setURL(customerDatasourceProperties.getUrl());AtomikosDataSourceBean xaDataSource = new AtomikosDataSourceBean();xaDataSource.setXaDataSource(h2XaDataSource);xaDataSource.setUniqueResourceName("xads1");return xaDataSource;}@Bean(name = "customerEntityManager")@DependsOn("transactionManager")public LocalContainerEntityManagerFactoryBean customerEntityManager() throws Throwable {HashMap<String, Object> properties = new HashMap<String, Object>();properties.put("hibernate.transaction.jta.platform", AtomikosJtaPlatform.class.getName());properties.put("javax.persistence.transactionType", "JTA");LocalContainerEntityManagerFactoryBean entityManager = new LocalContainerEntityManagerFactoryBean();entityManager.setJtaDataSource(customerDataSource());entityManager.setJpaVendorAdapter(jpaVendorAdapter);entityManager.setPackagesToScan("com.at.mul.domain.customer");entityManager.setPersistenceUnitName("customerPersistenceUnit");entityManager.setJpaPropertyMap(properties);return entityManager;}}
package com.at.mul;import java.util.HashMap;import javax.sql.DataSource;import org.h2.jdbcx.JdbcDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;import com.at.mul.repository.order.OrderDatasourceProperties;
import com.atomikos.jdbc.AtomikosDataSourceBean;@Configuration
@DependsOn("transactionManager")
@EnableJpaRepositories(basePackages = "com.at.mul.repository.order", entityManagerFactoryRef = "orderEntityManager", transactionManagerRef = "transactionManager")
@EnableConfigurationProperties(OrderDatasourceProperties.class)
public class OrderConfig {@Autowiredprivate JpaVendorAdapter jpaVendorAdapter;@Autowiredprivate OrderDatasourceProperties orderDatasourceProperties;@Bean(name = "orderDataSource", initMethod = "init", destroyMethod = "close")public DataSource orderDataSource() {JdbcDataSource h2XaDataSource = new JdbcDataSource();h2XaDataSource.setURL(orderDatasourceProperties.getUrl());AtomikosDataSourceBean xaDataSource = new AtomikosDataSourceBean();xaDataSource.setXaDataSource(h2XaDataSource);xaDataSource.setUniqueResourceName("xads2");return xaDataSource;}@Bean(name = "orderEntityManager")public LocalContainerEntityManagerFactoryBean orderEntityManager() throws Throwable {HashMap<String, Object> properties = new HashMap<String, Object>();properties.put("hibernate.transaction.jta.platform", AtomikosJtaPlatform.class.getName());properties.put("javax.persistence.transactionType", "JTA");LocalContainerEntityManagerFactoryBean entityManager = new LocalContainerEntityManagerFactoryBean();entityManager.setJtaDataSource(orderDataSource());entityManager.setJpaVendorAdapter(jpaVendorAdapter);entityManager.setPackagesToScan("com.at.mul.domain.order");entityManager.setPersistenceUnitName("orderPersistenceUnit");entityManager.setJpaPropertyMap(properties);return entityManager;}}

Another important thing here is the definition of the LocalContainerEntityManagerFactoryBean.

  • the @Bean annotation has a given name, that is the one specified in the @EnableJpaRepositories annotation.
  • you need to set some properties to the JpaPropertyMap, in particular you need to say that the transaction type is JTA and that the jta platform is AtomikosJtaPlatform.class.getName()
Not setting the second property was the reason why I could not get it work. As Dave Syer wrote "It seems Hibernate4 doesn't work with Atomikos out of the box", so you need to implement the class to be set as hibernate.transaction.jta.platform property by yourself. In my opinion this is not very well documented, but fortunately Oliver Gierke found another StackOverflow discussion about this topic. If you are using another JTA provider, you may find this useful.

Write the AbstractJtaPlatform implementation

As said, this is the most important step, as we need to write the implementation of that class by ourselves since Hibernate does not provide it. Here is the resulting code:

package com.at.mul;import javax.transaction.TransactionManager;
import javax.transaction.UserTransaction;import org.hibernate.engine.transaction.jta.platform.internal.AbstractJtaPlatform;public class AtomikosJtaPlatform extends AbstractJtaPlatform {private static final long serialVersionUID = 1L;static TransactionManager transactionManager;static UserTransaction transaction;@Overrideprotected TransactionManager locateTransactionManager() {return transactionManager;}@Overrideprotected UserTransaction locateUserTransaction() {return transaction;}}

Write the main configuration class

Also in this case it's a pretty standard class, with @EnableTransactionManagement annotation and Atomikos bean definitions. The only very important thing to notice is that we need to set AtomikosJtaPlatform.transactionManager and AtomikosJtaPlatform.transaction attributes.

package com.at.mul;import javax.transaction.TransactionManager;
import javax.transaction.UserTransaction;import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.vendor.Database;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.jta.JtaTransactionManager;import com.atomikos.icatch.jta.UserTransactionImp;
import com.atomikos.icatch.jta.UserTransactionManager;@Configuration
@ComponentScan
@EnableTransactionManagement
public class MainConfig {@Beanpublic PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {return new PropertySourcesPlaceholderConfigurer();}@Beanpublic JpaVendorAdapter jpaVendorAdapter() {HibernateJpaVendorAdapter hibernateJpaVendorAdapter = new HibernateJpaVendorAdapter();hibernateJpaVendorAdapter.setShowSql(true);hibernateJpaVendorAdapter.setGenerateDdl(true);hibernateJpaVendorAdapter.setDatabase(Database.H2);return hibernateJpaVendorAdapter;}@Bean(name = "userTransaction")public UserTransaction userTransaction() throws Throwable {UserTransactionImp userTransactionImp = new UserTransactionImp();userTransactionImp.setTransactionTimeout(10000);return userTransactionImp;}@Bean(name = "atomikosTransactionManager", initMethod = "init", destroyMethod = "close")public TransactionManager atomikosTransactionManager() throws Throwable {UserTransactionManager userTransactionManager = new UserTransactionManager();userTransactionManager.setForceShutdown(false);AtomikosJtaPlatform.transactionManager = userTransactionManager;return userTransactionManager;}@Bean(name = "transactionManager")@DependsOn({ "userTransaction", "atomikosTransactionManager" })public PlatformTransactionManager transactionManager() throws Throwable {UserTransaction userTransaction = userTransaction();AtomikosJtaPlatform.transaction = userTransaction;TransactionManager atomikosTransactionManager = atomikosTransactionManager();return new JtaTransactionManager(userTransaction, atomikosTransactionManager);}}

Resources

Here is the resulting structure of the project:

You can see the full source code here: https://github.com/fabiomaffioletti/mul-at, The master branch uses in memory database. Checkout branch named mysql-db to use real databases (see application.properties to tweak your database connection data).

转载于:https://my.oschina.net/pangzhuzhu/blog/318126

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/290656.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Avalonia跨平台入门第十四篇之ListBox折叠列表

在前面分享的几篇中咱已经玩耍了Popup、ListBox多选、Grid动态分、RadioButton模板、控件的拖放效果、控件的置顶和置底、控件的锁定、自定义Window样式、动画效果、Expander控件;今天趁着空闲时间接着去摸索基于ListBox的折叠列表的效果,最终实现的效果如下图:先来看看布局吧:…

iptables配置详解

-A参数是将规则写到现有链规则的最后面-I 参数默认是将一条规则添加到现有规则链的最前面&#xff0c;当然也可以指定插入到第几行 行数可以用数字来指定 比如说将一条规则添加到某一条链的第三行 那么原来在第三行的规则就会降到下一行第四行。例如&#xff1a; iptables -I …

C语言之strstr函数类似Java字符串的contain函数

1、strstr函数介绍 找出haystack字符串在needle字符串中第一次出现的位置&#xff08;不包括needle的串结束符&#xff09;。返回该位置的指针&#xff0c;如找不到&#xff0c;返回空指针。 2、举例 3、运行结果

软件测试技术——软件测试概述

文章目录一、软件测试的定义标准定义Bug和缺陷二、软件测试与软件质量保证三、软件测试七大基本原则四、软件测试分类按测试手段按测试执行方式按测试阶段或层次按测试对象五、软件测试过程模型V模型W模型H模型X模型一、软件测试的定义 正向观点逆向观点验证软件是否能正常工作…

Linux-No.04 Linux 设置定时任务发送邮件功能

2019独角兽企业重金招聘Python工程师标准>>> 1、定时任务 crontab -l crontab -e /sbin/service crond start //启动服务/sbin/service crond stop //关闭服务 /sbin/service crond restart //重启服务 /sbin/service crond reload //重新载入配置 /sbin/…

WPF 基础控件之 DataGrid 样式

此群已满340500857 &#xff0c;请加新群458041663由于微信群人数太多入群请添加小编微信号yanjinhuawechat 或 W_Feng_aiQ 邀请入群需备注WPF开发者 PS&#xff1a;有更好的方式欢迎推荐。支持NugetInstall-Package WPFDevelopers.Minimal -Version 3.2.001—代码如下一、创建…

C#页面抓取信息

//根据Url地址得到网页的html源码private string GetWebContent(string Url){string strResult"";try{HttpWebRequest request (HttpWebRequest)WebRequest.Create(Url);//声明一个HttpWebRequest请求request.Timeout 30000;//设置连接超时时间request.Headers.Set…

在 Linux 下使用 RAID(二):使用 mdadm 工具创建软件 RAID 0 (条带化)

RAID 即廉价磁盘冗余阵列&#xff0c;其高可用性和可靠性适用于大规模环境中&#xff0c;相比正常使用&#xff0c;数据更需要被保护。RAID 是一些磁盘的集合&#xff0c;是包含一个阵列的逻辑卷。驱动器可以组合起来成为一个阵列或称为&#xff08;组的&#xff09;集合。 创建…

struts2批量删除

2019独角兽企业重金招聘Python工程师标准>>> 2012-10-27 12:48 997人阅读 评论(0) 收藏 举报 逻辑代码 [java] view plain copy package com.stu2; import java.util.ArrayList; import java.util.List; import java.sql.*; import com.s…

电子商务应用课程知识整理 第六章-数据挖掘

文章目录一、概述二、关联分析概念&#xff1a;关联分析步骤&#xff1a;用于寻找频繁项集的算法算法一&#xff1a;蛮力法算法二&#xff1a;Apriopri算法&#xff08;先验算法&#xff09;三、分类与预测k近邻算法&#xff08;kNN&#xff09;四、聚类分析k均值算法&#xff…

.NET Core 返回结果统一封装

本文使用.NET Core Webapi演示&#xff01;一:新建.NetCore webapi项目为了方便开发,简化代码,也为了与前端方便对接,需要对接口服务返回结果进行统一处理。二:定义返回结果结构我们需要定义一个统一返回结果泛型类ApiResultpublic class ApiResult<T>{/// <summary&g…

linux之nm命令

1、nm命令介绍 nm命令很好记&#xff0c;当时看到大神在我的电脑面前在open ssl编译的.a文件里面过滤SHA函数&#xff0c;nm你就这样记&#xff0c;nm 尼玛, 哈哈。我们可以通过nm命令查看后缀out文件和后缀a文件里面的函数和部分属性。比如我们过滤SHA函数&#xff0c;命令如下…

.NET的两种部署模式,了解一下

前言以往部署程序一直是习惯性先安装运行时环境&#xff0c;然后再将发布打包好的程序运行起来&#xff1b;但当多个程序依赖不同版本框架平台时&#xff0c;如果部署在同一台机器上&#xff0c;那就需要在同一台机器上安装多个版本的运行时&#xff0c;总感觉有点不太方便&…

CSS3弹性伸缩布局(一)——box布局

CSS3弹性伸缩布局简介 2009年&#xff0c;W3C提出了一种崭新的方案----Flex布局&#xff08;即弹性伸缩布局&#xff09;&#xff0c;它可以简便、完整、响应式地实现各种页面布局&#xff0c;包括一直让人很头疼的垂直水平居中也变得很简单地就迎刃而解了。但是这个布局方式还…

Avalonia跨平台入门第十五篇之ListBox聊天窗口

在前面分享的几篇中咱已经玩耍了Popup、ListBox多选、Grid动态分、RadioButton模板、控件的拖放效果、控件的置顶和置底、控件的锁定、自定义Window样式、动画效果、Expander控件、ListBox折叠列表;今天趁着大周末的时间接着去摸索基于ListBox的聊天窗口的效果,最终实现的效果如…

《Unity着色器和屏幕特效开发秘笈(原书第2版)》一2.9 打包和混合纹理

本节书摘来自华章出版社《Unity着色器和屏幕特效开发秘笈&#xff08;原书第2版&#xff09;》一书中的第2章&#xff0c;第2.9节&#xff0c;作者 [英]艾伦朱科尼&#xff08;Alan Zucconi&#xff09; [美]肯尼斯拉默斯&#xff08;Kenneth Lammers&#xff09;&#xff0c;更…

云计算及应用课程知识整理

文章目录一、云计算云计算概念云计算的服务类型云计算技术体系结构的层次及其功能为什么云计算成本低&#xff1f;二、GFS分布式的文件系统设计需要考虑哪些问题&#xff1f;GFS架构GFS容错机制三、分布式数据处理MapReducemapReduce概念MapReduce适合什么类型数据&#xff1f;…

Android loading进度条使用简单总结

在这里&#xff0c;总结一下loading进度条的使用简单总结一下。 一、说起进度条&#xff0c;必须说说条形进度条&#xff0c;经常都会使用到嘛&#xff0c;特别是下载文件进度等等&#xff0c;还有像腾讯QQ安装进度条一样&#xff0c;有个进度总给人良好的用户体验。 先来找图看…

sublime php语法检查

安装sublimelinter 安装sublimelinter-php 设置sublimelinter 进入SublimeLinter文件夹改动 SublimeLinter.sublime-settings文件 设置php文件夹

lv13 环境搭建SD卡启动

一、制作SD卡启动盘 1.1 方法1&#xff1a;在Linux下制作 一、准备好烧录脚本 cd ~/fs4412 ​ unzip sdfuse_q.zip ​ cd sdfuse_q ​ chmod x *.sh 二、将SD卡插入USB读卡器&#xff0c;并连接到虚拟机 或者 一般识别的sd卡会在dev目录下显示sdb 三、烧录 cp ../u-boot-f…