简体   繁体   English

如何将Spring事务管理与JDBI 3集成?

[英]How to integrate Spring transaction management with JDBI 3?

I am trying to integrate spring transaction management with JDBI 3. When i implement below solution, with auto-commit property is set to false, i get below exception* due to closing handles before committing or rolling back transactions. 我正在尝试将Spring事务管理与JDBI 3集成。当我实现以下解决方案时,如果auto-commit属性设置为false,由于在提交或回滚事务之前关闭了句柄,因此我得到了exception *。

On the other hand, if auto-commit property set to true there is not any transaction error but this time rollback does not work (after runtime exception) and so X object is being persisted to database. 另一方面,如果将auto-commit属性设置为true,则不会发生任何事务错误,但是这次回滚将不起作用(在运行时异常之后),因此X对象将被持久化到数据库中。

I use spring 2.0.3, JDBI 3.0.0-beta2 . 我使用spring 2.0.3,JDBI 3.0.0-beta2。

Is there any recommended/applicable solution for that? 有什么建议/适用的解决方案吗?

Below is config class: 下面是配置类:

@Bean
public HikariDataSource hikariDataSource() {
    HikariConfig dataSourceConfig = new HikariConfig();
    dataSourceConfig.setDriverClassName(driverClassName);
    dataSourceConfig.setJdbcUrl(jdbcUrl);
    dataSourceConfig.setUsername(username);
    dataSourceConfig.setPassword(password);
    dataSourceConfig.setAutoCommit(true);
    return new HikariDataSource(dataSourceConfig);
}

@Bean
public TransactionAwareDataSourceProxy transactionAwareDataSourceProxy(HikariDataSource dataSource) {
    return new TransactionAwareDataSourceProxy(dataSource);
}

@Bean
public PlatformTransactionManager platformTransactionManager(HikariDataSource dataSource) {
    return new DataSourceTransactionManager(dataSource);
}

@Bean
public Jdbi jdbi(TransactionAwareDataSourceProxy transactionAwareDataSourceProxy) {
    Jdbi jdbi = Jdbi.create(transactionAwareDataSourceProxy);
    jdbi.installPlugin(new SqlObjectPlugin());
    return jdbi;
}

Here is service layer 这是服务层

@Override
@Transactional
public Long createX(X x) {
    Long aLong = xDao.insertX(x);
    if(true) throw new RuntimeException();
    return aLong;
}

*Improper transaction handling detected: A Handle with an open transaction was closed. *检测到不正确的事务处理:关闭了具有未完成事务的句柄。 Transactions must be explicitly committed or rolled back before closing the Handle. 在关闭句柄之前,必须显式提交或回滚事务。 Jdbi has rolled back this transaction automatically. Jdbi已自动回滚此事务。

Problem solved. 问题解决了。 I was using older version of JDBI, updated it ( check this thread ) and everything resolved. 我正在使用旧版本的JDBI,对其进行了更新( 请检查此线程 ),所有问题都已解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM