首页 > 其他分享 >Spring boot事务报错

Spring boot事务报错

时间:2024-08-19 10:52:34浏览次数:7  
标签:Qualifier Spring boot Bean firstDataSource DataSourceTransactionManager 报错 Trans

1. 报错信息

系统错误,错误信息:org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'defaultTx' is expected to be of type 'org.springframework.transaction.PlatformTransactionManager' but was actually of type 'org.springframework.transaction.support.TransactionTemplate'2.

2. 原因

使用了自定义的事务模板,样例代码如下

 

    @Bean(name = TransactionConfig.DEFAULT_TX)
    @Primary
    public TransactionTemplate transaction(@Qualifier("firstDataSource") DataSource firstDataSource) {
        DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager(firstDataSource);
        return new TransactionTemplate(dataSourceTransactionManager);
    }

    @Bean(name = TransactionConfig.SECOND_TX)
    public TransactionTemplate secondTransaction(@Qualifier("secondDataSource") DataSource secondDataScoure) {
        DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager(secondDataScoure);
        return new TransactionTemplate(dataSourceTransactionManager);
    }

 

 

3. 解决方法

    @Bean(name = DEFAULT_TX)
    @Primary
    public DataSourceTransactionManager firstDataSourceTransaction(@Qualifier("firstDataSource") DataSource firstDataSource) {
        return new DataSourceTransactionManager(firstDataSource);
    }

    @Bean(name = SECOND_TX)
    public DataSourceTransactionManager secondDataSourceTransaction(@Qualifier("secondDataSource") DataSource secondDataSource) {
        return new DataSourceTransactionManager(secondDataSource);
    }

4. 结语

使用自定义的事务管理器时,关注一下返回值是否为事务模板,如果是事务模板的话,使用方式就得改变了。    

标签:Qualifier,Spring,boot,Bean,firstDataSource,DataSourceTransactionManager,报错,Trans
From: https://www.cnblogs.com/dawenyang/p/18366882

相关文章