我为SpringBoot应用程序设置了以下内容:
数据库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 如何仍保持活动连接??
答案 0 :(得分:0)
据此: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") -> {...}) {...} }