Spring:我需要为2个数据源定义单独的TransactionManager吗?

时间:2018-06-19 12:16:03

标签: java spring spring-mvc migration

我正在研究一个Spring-MVC应用程序,其中我为不同类型的任务定义了2个数据源。当前,在从XML到Java的迁移过程中,我偶然发现了为HibernateTransactionManager添加新的@Transactional对象的要求。对于2个不同的数据源,我有2个单独的SessionFactory实例。但是,当我尝试使用辅助数据源创建另一个HibernateTransactionManager实例时,出现了一个非唯一异常。

我要发布的配置是否需要辅助HibernateTransactionManager实例,还是1个就足够了?如果需要,如何创建一个?谢谢。

错误日志:

org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [org.springframework.transaction.PlatformTransactionManager] is defined: expected single matching bean but found 2: primary_tx,extended_tx
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:368)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:331)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.determineTransactionManager(TransactionAspectSupport.java:367)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:271)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
    at org.springfram

WebConfig.java:

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = {"com.ourapp.spring"})
@EnableTransactionManagement
@EnableCaching
public class WebConfig extends WebMvcConfigurerAdapter {


 @Bean(name="primary_tx")
    public HibernateTransactionManager getPrimaryTransactionManager() throws IOException {
        HibernateTransactionManager txName= new HibernateTransactionManager();
        txName.setSessionFactory(sessionFactory().getObject());
        return txName;
    }

    @Bean(name="extended_tx")
    public HibernateTransactionManager txName() throws IOException {
        HibernateTransactionManager txName= new HibernateTransactionManager();
        txName.setSessionFactory(getExtendedSessionFactory().getObject());
        return txName;
    }

    @Bean
    @Qualifier("sessionFactory_origin")
    public LocalSessionFactoryBean sessionFactory() {
        LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
        sessionFactory.setDataSource(new DataSourceConfig().primaryDataSource());
        sessionFactory.setPackagesToScan("com.ourapp.spring");
        return sessionFactory;
    }

     @Bean
@Qualifier("sessionFactory_extended")
public LocalSessionFactoryBean getExtendedSessionFactory() {
    LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
    sessionFactory.setDataSource(new DataSourceConfig_Extended().secondaryDataSource());
    sessionFactory.setPackagesToScan("com.ourapp.spring");
    return sessionFactory;
}
}

典型服务层方法:

@Service
@Transactional("primary_tx")
public class ChatRoomMembersServiceImpl implements ChatRoomMembersService{


   private final ChatRoomMembersDAO chatRoomMembersDAO;

    @Autowired
    public ChatRoomMembersServiceImpl(ChatRoomMembersDAO chatRoomMembersDAO){
        this.chatRoomMembersDAO = chatRoomMembersDAO;
    }
}

典型的DAO层方法:

@Repository
@Transactional("primary_tx")
public class ChatRoomMembersDAOImpl implements ChatRoomMembersDAO{

    @Autowired
    @Qualifier(value = "sessionFactory_origin")
    private  SessionFactory sessionFactory;

    @Autowired
    @Qualifier(value = "sessionFactory_extended")
    private  SessionFactory sessionFactory_extended;

}

现在,每当需要时,我指的是扩展的SessionFactory实例。现在,我已经注释了DAO层方法,该方法需要使用secondary_tx扩展sessionFactory,但是它不起作用。谢谢。 :-)

1 个答案:

答案 0 :(得分:1)

问题的答案

  

我需要为2个数据源定义单独的TransactionManager吗?

从Java8开始,您可以在一个方法上多次具有相同的注释(前提是该注释被标记为@Repeatable)。完成此操作的另一种方法是提供自定义注释,该注释将处理其中一个数据源。

您遇到的异常是因为有两个用于事务管理的合格Bean。为此,您需要预选赛。

代替:

@Bean(name="primary_tx")
    public HibernateTransactionManager getPrimaryTransactionManager() throws IOException {
        HibernateTransactionManager txName= new HibernateTransactionManager();
        txName.setSessionFactory(sessionFactory().getObject());
        return txName;
    }

使用

@Bean @Qualifier("primary_tx")

    public HibernateTransactionManager getPrimaryTransactionManager() throws IOException {
        HibernateTransactionManager txName= new HibernateTransactionManager();
        txName.setSessionFactory(sessionFactory().getObject());
        return txName;
    }

现在,假设您要在我的方法m1中将事务作为原子进行。以下是您需要的

@Transactional("primary_tx")
public void m1(){
}