私はSpringを初めて使用しますが、同じアプリケーションで多数のトランザクションマネージャーを使用できるかどうか疑問に思っています。
2つのデータアクセス層があります。1つは両方のデータベース用です。1つのレイヤーに1つのトランザクションマネージャーを使用し、もう1つのレイヤーに別のトランザクションマネージャーを使用するにはどうすればよいのでしょうか。両方のデータベース間でトランザクションを実行する必要はありません-まだです。ただし、各データベースで個別にトランザクションを実行する必要があります。問題の概要を説明するための画像を作成しました。
これが私のアプリケーションコンテキスト設定です:
<beans xmlns="http://www.springframework.org/schema/beans"
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">
<context:component-scan base-package="cheetah.repositories" />
<tx:annotation-driven />
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
<property name="persistenceUnitName" value="accounts" />
</bean>
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
<bean id="transactionManager"
class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
</beans>
この構成を使用する例を次に示します。
@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は1つだけですが、リポジトリごとに異なるトランザクションマネージャーを使用するにはどうすればよいですか?
助けてくれてありがとう。