Spring-동일한 애플리케이션에서 여러 트랜잭션 관리자를 사용할 수 있습니까?
저는 Spring을 처음 접했고 동일한 애플리케이션에서 수많은 트랜잭션 관리자를 사용할 수 있는지 궁금합니다.
두 개의 데이터 액세스 레이어가 있습니다. 하나는 두 데이터베이스 모두입니다. 한 계층에 대해 하나의 트랜잭션 관리자를 사용하고 다른 계층에 대해 다른 트랜잭션 관리자를 사용하는 방법에 대해 궁금합니다. 아직 두 데이터베이스에서 트랜잭션을 수행 할 필요는 없습니다. 하지만 각 데이터베이스에서 개별적으로 트랜잭션을 수행해야합니다. 문제를 설명하는 데 도움이되는 이미지를 만들었습니다.
내 응용 프로그램 컨텍스트 구성은 다음과 같습니다.
<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;
}
}
So for the account repository, I want to use an entity manager factory with the persistence unit set to accounts. However, with my BusinessData Repository, I want to use an entity manager factory with a different persistence unit. Since I can only define one transaction manager bean, how can I go about using different transaction managers for the different repositories?
Thanks for any help.
Where you use a @Transactional
annotation, you can specify the transaction manager to use by adding an attribute set to a bean name or qualifier. For example, if your application context defines multiple transaction managers with qualifiers:
<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>
You can use the qualifier to specify the transaction manager to use:
public class TransactionalService {
@Transactional("account")
public void setSomethingInAccount() { ... }
@Transactional("businessData")
public void doSomethingInBusinessData() { ... }
}
This Spring Jira entry discusses the issue a bit:
https://jira.spring.io/browse/SPR-3955
2 단계 커밋을 사용하지 않는 경우 연결 당 하나의 트랜잭션 관리자가 될 수 있다고 생각합니다. 두 개의 트랜잭션 관리자를 만들고 적절한 연결을 삽입하기 만하면됩니다.
하지만 질문을해야합니다. 왜 두 명의 트랜잭션 관리자가 필요하다고 생각합니까? 둘 이상의 데이터베이스 연결이있을 수 있습니다. 연결을 사용하는 DAO는 서로 다른 서비스에 의해 인스턴스화 될 수 있으며 인스턴스화되어야하며 각 서비스에는 고유 한 트랜잭션 설정으로 주석을 달 수 있습니다. 한 명의 관리자가 둘 다 수용 할 수 있습니다. 왜 두 개가 필요하다고 생각합니까?
'Development Tip' 카테고리의 다른 글
Kubernetes (kubectl)에 사용자를 추가하는 방법은 무엇입니까? (0) | 2020.12.01 |
---|---|
pycharm에서 Conda 환경 사용 (0) | 2020.12.01 |
html5 : 머리글 또는 바닥 글 태그를 두 번 사용합니까? (0) | 2020.12.01 |
더 큰 JavaScript 애플리케이션 개발을위한 모범 사례 (0) | 2020.12.01 |
흐림 이벤트가 클릭 이벤트 작동을 중지합니까? (0) | 2020.12.01 |