为什么 Spring ChainedTransactionManager 被弃用?spring是否提供了任何替代库来支持多个事务管理器?
我的用例:- 我们正在构建一个连接到两个数据源(db1 和 db2)的 Spring 引导应用程序,它在两个数据库(db1 和 db2)上执行插入操作。我们的要求是这样的:插入 -
目前,我们正在使用 ChaninedTransactionManager,它按预期工作,但我可以看到 lib 已被弃用?所以,只是想确保使用它是否安全,或者 Spring 是否提供了任何我们可以用作替代品的替代库?
配置的实例将按给定的顺序启动事务,并以相反的顺序提交/回滚,这意味着最有可能破坏事务的 PlatformTransactionManager 应该是配置列表中的最后一个。
如果按以下顺序链接事务:事务 1、事务 2
transaction1 begin
transaction2 begin
transaction2 commit -> error rollbacks, rollbacks transction1 too
transaction1 commit -> error, only rollbacks transaction1
外壳插页 -
但是,如果你有插入 -
如果你对此没意见,你可以在你的项目中复制类 et 继续使用它:https://github.com/spring-projects/spring-data-commons/issues/2232#issuecomment-1018473289
为了让所有事务在失败时回滚,我将我的 conf 更改为使用 JtaTransactionManager(带有 spring boot 和 posgtres)。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jta-atomikos</artifactId>
</dependency>
应用程序属性
my.datasource.one.unique-resource-name=
my.datasource.one.xa-properties.url=jdbc:postgresql://
my.datasource.one.xa-data-source-class-name=org.postgresql.xa.PGXADataSource
my.datasource.one.xa-properties.user=
my.datasource.one.xa-properties.password=
my.datasource.one.max-pool-size=
my.datasource.one.min-pool-size=
my.datasource.two.unique-resource-name=
my.datasource.two.xa-properties.url=jdbc:postgresql://
my.datasource.two.xa-data-source-class-name=org.postgresql.xa.PGXADataSource
my.datasource.two.xa-properties.user=
my.datasource.two.xa-properties.password=
my.datasource.two.max-pool-size=
my.datasource.two.min-pool-size=
@Bean
@Primary
@ConfigurationProperties("my.datasource.one")
public DataSource dataSourceOne() {
return new AtomikosDataSourceBean();
}
@Bean("entityManagerOne")
@DependsOn("transactionManager")
public LocalContainerEntityManagerFactoryBean entityManagerOne(@Autowired JpaVendorAdapter jpaVendorAdapter) {
HashMap<String, Object> properties = new HashMap<>();
properties.put("hibernate.dialect", "org.hibernate.dialect.PostgreSQLDialect");
properties.put("hibernate.default_schema", "public");
properties.put("hibernate.ddl-auto", "none");
LocalContainerEntityManagerFactoryBean entityManager = new LocalContainerEntityManagerFactoryBean();
entityManager.setJtaDataSource(dataSourceOne());
entityManager.setJpaVendorAdapter(jpaVendorAdapter);
entityManager.setPackagesToScan("");
entityManager.setPersistenceUnitName("");
entityManager.setJpaPropertyMap(properties);
return entityManager;
}
@Bean
@Primary
@ConfigurationProperties("my.datasource.two")
public DataSource dataSourceTwo() {
return new AtomikosDataSourceBean();
}
@Bean("entityManagerTwo")
@DependsOn("transactionManager")
public LocalContainerEntityManagerFactoryBean entityManagerTwo(@Autowired JpaVendorAdapter jpaVendorAdapter) {
HashMap<String, Object> properties = new HashMap<>();
properties.put("hibernate.dialect", "org.hibernate.dialect.PostgreSQLDialect");
properties.put("hibernate.default_schema", "public");
properties.put("hibernate.ddl-auto", "none");
LocalContainerEntityManagerFactoryBean entityManager = new LocalContainerEntityManagerFactoryBean();
entityManager.setJtaDataSource(dataSourceTwo());
entityManager.setJpaVendorAdapter(jpaVendorAdapter);
entityManager.setPackagesToScan("");
entityManager.setPersistenceUnitName("");
entityManager.setJpaPropertyMap(properties);
return entityManager;
}
JTA.属性
com.atomikos.icatch.enable_logging=false
com.atomikos.icatch.default_jta_timeout=60000000
com.atomikos.icatch.max_timeout=100000000
com.atomikos.icatch.threaded_2pc=true
@Bean
public JpaVendorAdapter jpaVendorAdapter(@Autowired Environment env) {
HibernateJpaVendorAdapter hibernateJpaVendorAdapter = new HibernateJpaVendorAdapter();
hibernateJpaVendorAdapter.setShowSql(false);
return hibernateJpaVendorAdapter;
}
@Bean
public UserTransaction userTransaction() throws SystemException {
var userTransaction = new UserTransactionImp();
userTransaction.setTransactionTimeout(60000);
return userTransaction;
}
@Bean
public TransactionManager atomikosTransactionManager() throws SystemException {
var userTransactionManager = new UserTransactionManager();
userTransactionManager.setForceShutdown(true);
return userTransactionManager;
}
@Bean
public PlatformTransactionManager transactionManager(@Autowired UserTransaction userTransaction, @Autowired TransactionManager atomikosTransactionManager) throws Throwable {
return new JtaTransactionManager(userTransaction, atomikosTransactionManager);
}
为帖子启用准备好的事务
postgresql.conf
max_prepared_transactions = 100 # zero disables the feature
帮助我得到这个的来源:
https://www.baeldung.com/java-atomikos
http://www.thedevpiece.com/configuring-multiple-datasources-using-springboot-and-atomikos/
https://github.com/YihuaWanglv/spring-boot-jta-atomikos-sample
因为有一个 API 改进。从文档 https://docs.spring.io/spring-data/commons/docs/current/api/org/springframework/data/transaction/ChainedTransactionManager.html
不要使用 ChainedTransactionManager 将回调附加到事务提交(提交前/提交后),而是注册 TransactionSync,以便在发生异常时使用简化的语义显式遵循事务清理。
您仍然可以自由使用它,请记住,在某个时候,该类将在 Spring 的未来版本中删除,如果不重构该部分,将无法升级。