如何使用JOOQ和Spring-boot 2.0进行手动事务管理?

时间:2018-08-10 09:27:34

标签: spring spring-boot java-8 jooq

使用Spring Boot 2.0.4和JOOQ 3.11.3。

我有一个服务器端点,需要对事务管理进行细粒度的控制;它需要在外部调用之前和之后发出多个SQL语句,并且在与外部站点通信时一定不能保持数据库事务处于打开状态。

在下面的代码testTransactionV4中,这是我最喜欢的尝试。

我已经看过JOOQ手册,但是交易管理部分还很轻巧,似乎暗示这是这样做的方法。

感觉我在努力工作,比我在这里要努力,通常这表明我做错了。是否有更好的“正确”方法来使用Spring / JOOQ进行手动事务管理?

此外,对TransactionBean实施的任何改进将不胜感激(并赞成)。

但是这个问题的重点实际上是:“这是正确的方法吗?”


TestEndpoint:

@Role.SystemApi
@SystemApiEndpoint
public class TestEndpoint {
  private static Log log = to(TestEndpoint.class);

  @Autowired private DSLContext db;
  @Autowired private TransactionBean txBean;
  @Autowired private Tx tx;

  private void doNonTransactionalThing() {
    log.info("long running thing that should not be inside a transaction");
  }

  /** Works; don't like the commitWithResult name but it'll do if there's
   no better way.  Implementation is ugly too.
   */
  @JsonPostMethod("testTransactionV4")
  public void testMultiTransactionWithTxBean() {
    log.info("start testMultiTransactionWithTxBean");

    AccountRecord account = txBean.commitWithResult( db ->
      db.fetchOne(ACCOUNT, ACCOUNT.ID.eq(1)) );

    doNonTransactionalThing();

    account.setName("test_tx+"+new Date());

    txBean.commit(db -> account.store() );
  }

  /** Works; but it's ugly, especially having to work around lambda final
   requirements on references.   */
  @JsonPostMethod("testTransactionV3")
  public void testMultiTransactionWithJooqApi() {
    log.info("start testMultiTransactionWithJooqApi");

    AtomicReference<AccountRecord> account = new AtomicReference<>();
    db.transaction( config->
      account.set(DSL.using(config).fetchOne(ACCOUNT, ACCOUNT.ID.eq(1))) );

    doNonTransactionalThing();

    account.get().setName("test_tx+"+new Date());

    db.transaction(config->{
      account.get().store();
    });
  }

  /** Does not work, there's only one commit that spans over the long operation */
  @JsonPostMethod("testTransactionV1")
  @Transactional
  public void testIncorrectSingleTransactionWithMethodAnnotation() {
    log.info("start testIncorrectSingleTransactionWithMethodAnnotation");

    AccountRecord account = db.fetchOne(ACCOUNT, ACCOUNT.ID.eq(1));

    doNonTransactionalThing();

    account.setName("test_tx+"+new Date());
    account.store();
  }

  /** Works, but I don't like defining my tx boundaries this way, readability
   is poor (relies on correct bean naming and even then is non-obvious) and is
   fragile in the face of refactoring.  When explicit TX boundaries are needed
   I want them getting in my face straight away.
   */
  @JsonPostMethod("testTransactionV2")
  public void testMultiTransactionWithNestedComponent() {
    log.info("start testTransactionWithComponentDelegation");

    AccountRecord account = tx.readAccount();

    doNonTransactionalThing();

    account.setName("test_tx+"+new Date());
    tx.writeAccount(account);
  }

  @Component
  static class Tx {
    @Autowired private DSLContext db;

    @Transactional
    public AccountRecord readAccount() {
      return db.fetchOne(ACCOUNT, ACCOUNT.ID.eq(1));
    }

    @Transactional
    public void writeAccount(AccountRecord account) {
      account.store();
    }
  }

}

TransactionBean:

@Component
public class TransactionBean {
  @Autowired private DSLContext db;

  /**
   Don't like the name, but can't figure out how to make it be just "commit".
   */
  public <T> T commitWithResult(Function<DSLContext, T> worker) {
    // Yuck, at the very least need an array or something as the holder.
    AtomicReference<T> result = new AtomicReference<>();
    db.transaction( config -> result.set(
      worker.apply(DSL.using(config))
    ));
    return result.get();
  }

  public void commit(Consumer<DSLContext> worker) {
    db.transaction( config ->
      worker.accept(DSL.using(config))
    );
  }

  public void commit(Runnable worker) {
    db.transaction( config ->
      worker.run()
    );
  }
}

1 个答案:

答案 0 :(得分:1)

使用TransactionTemplate包装事务部分。 Spring Boot提供了一个开箱即用的功能,因此可以使用。您可以使用execute 方法将呼叫包装在事务中。

@Autowired
private TransactionTemplate transaction;

@JsonPostMethod("testTransactionV1")
public void testIncorrectSingleTransactionWithTransactionTemplate() {
  log.info("start testIncorrectSingleTransactionWithMethodAnnotation");

  AccountRecord account = transaction.execute( status -> db.fetchOne(ACCOUNT, ACCOUNT.ID.eq(1)));

  doNonTransactionalThing();

  transaction.execute(status -> {
    account.setName("test_tx+"+new Date());
    account.store();
    return null;
  }
}

类似的事情应该可以解决。不确定lambda是否会正常工作(请忘记TransactionCallback

的语法