首先看下遇到的错误

org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.transaction.PlatformTransactionManager' available
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:347)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:334)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:345)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:334)
	at org.springframework.transaction.interceptor.TransactionAspectSupport.determineTransactionManager(TransactionAspectSupport.java:394)
	at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:284)
	at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:98)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
	at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:689)
	at com.dlyx.analysis.forecast.biz.SmTranPrdicetOderBiz$$EnhancerBySpringCGLIB$$14c10172.saveData(<generated>)

这个问题的原因是因为项目以前只有一个pg数据源,一个数据源绑定一个事务管理器,直接使用@Transactional()注释时Spring会帮助我们自动识别出来,但是现在项目中出现了多个数据源,再使用@Transactional()注解时如果不直接指定,Spring就不知道具体使用哪一个事务管理器来进行事务管理了,因此需要具体指定一下事务管理器,代码如下:

    @Bean
    public PlatformTransactionManager pgTransactionManager(@Qualifier("pgDataSource") DataSource prodDataSource) {
        return new DataSourceTransactionManager(prodDataSource);
    }
    @Bean
    public PlatformTransactionManager hiveTransactionManager(@Qualifier("hiveDataSource") DataSource prodDataSource) {
        return new DataSourceTransactionManager(prodDataSource);
    }

这个时候就可以指定用哪个事务处理了,value值填Bean名就可以(Bean名默认是 “方法名” + “首字母小写”)

    @Transactional(transactionManager = "pgTransactionManager")
    public String saveData(JSONObject jsonObject) {
    }
Logo

长江两岸老火锅,共聚山城开发者!We Want You!

更多推荐