gpt4 book ai didi

spring - @Transactional 具有多个事务管理器和多个数据库

转载 作者:行者123 更新时间:2023-12-05 07:26:00 29 4
gpt4 key购买 nike

I have the below set up for a SpringBoot application:

  1. 数据库 1 - TransactionManagerDatabase1
  2. 数据库 2 - TransactionManagerDatabase2

    Transactional(value = "TransactionManagerDatabase1", readOnly = true)
    public void getResults() {
    // select a row from table in Database1
    // select a row from table in Database2

    DataSource dataSourceDB2 = SpringContextProvider.getApplicationContext().getBean("TransactionManagerDatabase2", DataSource.class);
    dataSourceDB2.getPool().getActive(); //This is returning an active connection with Database2

    }

@Transactional被应用于Database1时,Database2如何仍然保持事件连接??

最佳答案

根据这个:Transaction management for multiple database Using Spring & Hibernate

您可以像这样指定多个 TM:

<bean>
<bean id="transactionManager1"
class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory1" />
<qualifier value="account"/>
</bean>

<bean id="transactionManager2"
class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory2" />
<qualifier value="businessData"/>
</bean>

然后您可以执行此操作以指定您需要的 TM:

public class TransactionalService {

@Transactional("account")
public void setSomethingInAccount() { ... }

@Transactional("businessData")
public void doSomethingInBusinessData() { ... }
}
public class TransactionalService {

@Transactional("businessData")
public void doSomethingInBusinessData(
@Transactional
("account") -> {...})
{...}
}

关于spring - @Transactional 具有多个事务管理器和多个数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54586257/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com