我是Spring的新手,我想知道是否可能在同一个应用程序中使用大量的事务管理器?
我有两个数据访问层 – 一个用于两个数据库。我想知道,你如何使用一个事务管理器为一个层和不同的事务管理器为其他层。我不需要在两个数据库之间执行事务。但我需要单独对每个数据库执行事务。我创建了一个图像来帮助概述我的问题:
这里是我的应用程序上下文配置:
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
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">
class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
class="org.springframework.orm.jpa.JpaTransactionManager">
下面是使用此配置的示例:
@Repository
public class JpaAccountRepository implements AccountRepository {
@PersistenceContext(unitName = "cheetahAccounts")
private EntityManager accountManager;
@Override
@Transactional
public Account findById(long id) {
Account account = accountManager.find(Account.class, id);
return account;
}
}
所以对于帐户存储库,我想使用实体管理器工厂,持久性单位设置为帐户。但是,对于我的BusinessData存储库,我想使用具有不同持久性单位的实体管理器工厂。由于我只能定义一个事务管理器bean,我如何使用不同的事务管理器为不同的存储库?
感谢任何帮助。