【问题标题】:jooq DataSourceConnectionProvider with TransactionAwareDataSourceProxy is not taking part in spring transactions带有 TransactionAwareDataSourceProxy 的 jooq DataSourceConnectionProvider 不参与春季交易
【发布时间】:2022-11-21 07:38:10
【问题描述】:

我有以下弹簧数据源设置:

 datasource:
    name: postgres-datasource
    url: ${POSTGRES_URL:jdbc:postgresql://localhost:5432/mydb}?reWriteBatchedInserts=true&prepareThreshold=0
    username: ${POSTGRES_USER:mydb}
    password: ${POSTGRES_PASS:12345}
    driver-class: org.postgresql.Driver
    hikari:
      minimumIdle: 2
      maximum-pool-size: 30
      max-lifetime: 500000
      idleTimeout: 120000
      auto-commit: false
      data-source-properties:
        cachePrepStmts: true
        useServerPrepStmts: true
        prepStmtCacheSize: 500
  jpa:
    database-platform: org.hibernate.dialect.PostgreSQLDialect
    properties:
      hibernate:
        dialect: org.hibernate.dialect.PostgreSQLDialect
#        generate_statistics: true
        order_inserts: true
        order_updates: true
        jdbc:
          lob:
            non_contextual_creation: true
          batch_size: 50

请注意,自动提交是false
因为,我需要同时使用 jooq 和 JPA,并且在我的数据库中也有多个模式,所以我配置了以下 DataSourceConnectionProvider

public class SchemaSettingDataSourceConnectionProvider extends DataSourceConnectionProvider {

    public SchemaSettingDataSourceConnectionProvider(TransactionAwareDataSourceProxy dataSource) {
        super(dataSource);
    }

    public Connection acquire() {
        try {
            String tenant = TenantContext.getTenantId();
            log.debug("Setting schema to {}", tenant);
            Connection connection = dataSource().getConnection();
            Statement statement = connection.createStatement();
            statement.executeUpdate("SET SCHEMA '" + tenant + "'");
            statement.close();
            return connection;
        } catch (SQLException var2) {
            throw new DataAccessException("Error getting connection from data source " + dataSource(), var2);
        }
    }

我在 spring boot 配置上有 @EnableTransactionManagement。有了这个设置,连接在事务结束后不会提交。

@Transactional(propagation = Propagation.REQUIRES_NEW)
    public FlowRecord insert(FlowName name, String createdBy) {
        return dslContext.insertInto(FLOW, FLOW.NAME, FLOW.STATUS)
                .values(name.name(), FlowStatus.CREATED.name())
                .returning(FLOW.ID)
                .fetch()
                .get(0);
    }

这不承诺。因此,我尝试将以下代码添加到我的SchemaSettingDataSourceConnectionProvider 类中

@Override
    public void release(Connection connection) {
        connection.commit();
        super.release(connection);
    }

然而,现在的问题是,即使事务应该回滚,例如由于运行时异常,它仍然会一直提交。

我缺少一些配置吗

【问题讨论】:

    标签: spring hibernate jooq spring-transactions hikaricp


    【解决方案1】:

    需要注意的一件事是确保您使用的是正确的@Transactional 注释。在我的项目中有两个:一个来自 Jakarta 包,一个来自 Spring 包——确保您使用的是 Spring 注释。

    我不知道你的Spring配置是否正确。 我们使用 Java 配置,因此很难比较。

    一个明显的区别是我们明确定义了 TransactionManager,这是您可能需要做的事情吗?

    【讨论】:

      猜你喜欢
      • 2019-02-14
      • 1970-01-01
      • 1970-01-01
      • 2014-05-28
      • 2020-12-18
      • 1970-01-01
      • 1970-01-01
      • 2012-08-10
      • 1970-01-01
      相关资源
      最近更新 更多