Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Springboot TransactionManager 报错 #26

Open
hawind opened this issue Jan 13, 2022 · 5 comments
Open

Springboot TransactionManager 报错 #26

hawind opened this issue Jan 13, 2022 · 5 comments

Comments

@hawind
Copy link

hawind commented Jan 13, 2022

感觉是事物管理出问题了,我这边因为还使用了hibernate,会导致下面的配置报错。

@Bean
@ConditionalOnMissingBean
public GaarasonTransactionManager gaarasonTransactionManager() {
    logger.info("-------------------- gaarasonTransactionManager init ------------------");
    return new GaarasonTransactionManager(gaarasonDataSource());
}

报错截图
image

GaarasonDatabaseConfiguration自定义的代码,因为database-spring-boot-starter里没有配置@primary优先级,也会导致出问题

@Slf4j
@Configuration
@AutoConfigureBefore(DruidDataSourceAutoConfigure.class)
@EnableConfigurationProperties({GaarasonDatabaseProperties.class})
@Import({GeneralModel.class})
public class GaarasonDatabaseConfiguration {

    private static final Logger logger = LoggerFactory.getLogger(GaarasonDatabaseConfiguration.class);

    /**
     * 指定 model 扫描范围
     */
    GaarasonDatabaseConfiguration(ApplicationContext applicationContext, GaarasonDatabaseProperties gaarasonDatabaseProperties) {
        // 注册 model实例获取方式
        ModelInstanceProvider.register(modelClass -> {
            try {
                return ObjectUtils.typeCast(applicationContext.getBean(modelClass));
            } catch (BeansException e) {
                return ObjectUtils.typeCast(applicationContext.getBean(StringUtils.lowerFirstChar(modelClass.getSimpleName())));
            }
        });
        logger.info("Model instance provider has been registered success.");

        // 注册 雪花id实现
        final int workerId = gaarasonDatabaseProperties.getSnowFlake().getWorkerId();
        final int dataId = gaarasonDatabaseProperties.getSnowFlake().getDataId();
        ContainerProvider.register(IdGenerator.SnowFlakesID.class, clazz -> new SnowFlakeIdGenerator(workerId, dataId));

        logger.info("SnowFlakesID[ workId: {}, dataId: {}] instance has been registered success.", workerId, dataId);
    }

    @Primary
    @Bean
    @ConfigurationProperties(prefix = "spring.datasource2")
    @ConditionalOnMissingBean
    public DataSource dataSourceDruidConfig() {
        logger.info("-------------------- dataSource druid config init ---------------------");
        return DruidDataSourceBuilder.create().build();
    }

    @Bean
    @ConditionalOnMissingBean
    public GaarasonDataSource gaarasonDataSource() {
        logger.info("-------------------- gaarasonDataSource init --------------------------");
        return ContainerProvider.getBean(GaarasonDataSourceConfig.class).build(Collections.singletonList(dataSourceDruidConfig()));
    }

    @Bean
    @ConditionalOnMissingBean
    public GaarasonTransactionManager gaarasonTransactionManager() {
        logger.info("-------------------- gaarasonTransactionManager init ------------------");
        return new GaarasonTransactionManager(gaarasonDataSource());
    }
}

请教怎么在多个数据源里让事务可以正常执行。

@hawind hawind changed the title update 更新在循环中没有作用 Springboot TransactionManager 报错 Jan 16, 2022
@xutengx
Copy link
Member

xutengx commented Jan 21, 2022

根据报错信息
图片
似乎是标红的类么有找到。

此外,如果同时使用多种orm框架的话,那么在使用诸如@Transactional等spring的注解时,需要注意其中生效的DataSource接口的实现是否符合预期(例如,@Transactional -> GaarasonTransactionManager -> GaarasonDataSource(DataSource的实现)or @Transactional -> xxxxTransactionManager -> DataSource(其他orm框架中对于DataSource的实现))。

@hawind
Copy link
Author

hawind commented Jan 22, 2022

确实是这个提示,但是很奇怪,写了那个方法就会提示,不写就没有。

@xutengx
Copy link
Member

xutengx commented Jan 22, 2022

@Bean
@ConditionalOnMissingBean
public GaarasonTransactionManager transactionManager() {
    logger.info("-------------------- gaarasonTransactionManager init ------------------");
    return new GaarasonTransactionManager(gaarasonDataSource());
}

可以尝试更改下bean的名称~

@hawind
Copy link
Author

hawind commented Mar 6, 2022

好的,谢谢。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants