简体   繁体   English

jOOQ和Spring事务管理

[英]jOOQ and Spring transaction management

I use jOOQ 3.8 and Spring Boot 1.4.1. 我使用jOOQ 3.8和Spring Boot 1.4.1。 I see that jOOQ uses a mechanism to guarantee the handling of transactions . 我看到jOOQ使用一种机制来保证事务处理

If I define a method annotated as transactional and inside a execute two insert, are they executed in the same transaction, eg 如果我将一个注释为事务的方法定义为一个执行二插入,那么它们是否在同一个事务中执行,例如

@Transactional(propagation = Propagation.MANDATORY)
public doInsert(){
    DSL.using(configuration).insertInto(...);
    DSL.using(configuration).insertInto(...);
}

will all the executed insert rollback in case of exception? 如果发生异常,所有执行的插入回滚吗? Will they be executed in one transaction? 它们会在一次交易中执行吗?

Or, should I do as follows: 或者,我应该这样做:

public doInsert(){
  create.transaction(configuration -> {

    DSL.using(configuration).insertInto(...);
    DSL.using(configuration).insertInto(...);    
  });
}

And what happens if I use the annotation and the jOOQ transaction as follows: 如果我使用注释和jOOQ事务如下所示会发生什么:

@Transactional(propagation = Propagation.MANDATORY)
public doInsert(){
  create.transaction(configuration -> {

    // Wrap configuration in a new DSLContext:
    DSL.using(configuration).insertInto(...);
    DSL.using(configuration).insertInto(...);    
  });
  throw new RuntimeException(":)");
}

Will the changes in the transaction be committed regardless of the exception? 无论例外情况如何,都会提交事务中的更改吗? (I would expect it) (我希望它)

I see that jOOQ uses a mechanism to guarantee the handling of transactions. 我看到jOOQ使用一种机制来保证事务的处理。

jOOQ doesn't actually do that. jOOQ实际上并没有这样做。 jOOQ provides an API for convenient transaction usage through lambdas. jOOQ提供了一个API,可以通过lambda方便地使用事务。 The API, however, is implemented by you (or indirectly, by Spring) through the jOOQ TransactionProvider SPI. 但是,API由您(或间接地,由Spring)通过jOOQ TransactionProvider SPI实现。

Using spring only (the easiest) 仅使用弹簧(最简单)

Given: 鉴于:

DSLContext ctx = ...

If you do this: 如果你这样做:

@Transactional(propagation = Propagation.MANDATORY)
public doInsert(){
    ctx.insertInto(...);
    ctx.insertInto(...);
}

You're not using jOOQ's transaction API at all, you're using only spring, which is perfectly fine for jOOQ. 你根本没有使用jOOQ的交易API,你只使用spring,这对于jOOQ来说非常好。

Using jOOQ (possibly with spring behind the scenes) 使用jOOQ(可能在幕后使用弹簧)

If you do this: 如果你这样做:

public doInsert(){
  ctx.transaction(configuration -> {
    DSL.using(configuration).insertInto(...);
    DSL.using(configuration).insertInto(...);    
  });
}

Then you use jOOQ's transaction API, which you may or may not configure to be implemented using spring transactions. 然后使用jOOQ的事务API,您可能配置或不配置使用spring事务实现。 By default, jOOQ will implement transactions via JDBC directly. 默认情况下,jOOQ将直接通过JDBC实现事务。

Using both APIs 使用两个API

However, this: 但是,这个:

@Transactional(propagation = Propagation.MANDATORY)
public doInsert(){
  ctx.transaction(configuration -> {

    // Wrap configuration in a new DSLContext:
    DSL.using(configuration).insertInto(...);
    DSL.using(configuration).insertInto(...);    
  });
  throw new RuntimeException(":)");
}

Currently (jOOQ 3.8) does not work without you implementing a rather sophisticated TransactionProvider that detects spring's declarative transaction scope in the current thread's context. 目前(jOOQ 3.8)如果没有实现在当前线程的上下文中检测spring的声明性事务范围的相当复杂的TransactionProvider ,则不起作用。

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

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