我有一个现有的应用程序,它将Hibernate会话工厂用于一个数据库。我们正在添加另一个数据库来进行分析。事务永远不会交叉,所以我不需要JTA,但我确实想使用JPA EntityManager来创建新的数据库。
我已经设置了EntityManager和新的事务管理器,这是我已经鉴定过的,但是Spring抱怨我需要鉴定现有的@Transactional注释。我正试图找到一种方法来告诉Spring使用TxManager作为默认值。有什么办法吗?否则,我必须将限定符添加到所有现有的@transactional注释中,如果可能的话,我希望避免这些注释。
@Bean(name = "jpaTx")
public PlatformTransactionManager transactionManagerJPA() throws NamingException {
JpaTransactionManager txManager = new JpaTransactionManager(entityManagerFactory());
return txManager;
}
@Bean
public PlatformTransactionManager txManager() throws Exception {
HibernateTransactionManager txManager = new HibernateTransactionManager(sessionFactory());
txManager.setNestedTransactionAllowed(true);
return txManager;
}
我得到的错误
No qualifying bean of type [org.springframework.transaction.PlatformTransactionManager] is defined: expected single matching bean but found 2:
谢谢
最佳答案
我能用注释来解决这个问题。
@Bean(name = "jpaTx")
public PlatformTransactionManager transactionManagerJPA() throws NamingException {
JpaTransactionManager txManager = new JpaTransactionManager(entityManagerFactory());
return txManager;
}
@Bean
@Primary
public PlatformTransactionManager txManager() throws Exception {
HibernateTransactionManager txManager = new HibernateTransactionManager(sessionFactory());
txManager.setNestedTransactionAllowed(true);
return txManager;
}