放弃 / 忽略当前交易,如果有新的交易可用则使用新的交易。

huangapple go评论60阅读模式
英文:

Drop / ignore current transaction and use upcoming transaction if available

问题

以下是翻译好的内容:

我有3个方法,定义如下。methodXmethodY 在不同的类中定义。methodYmethodZAsync 在同一个类中定义。

@Transactional(propagation = Propagation.REQUIRED)
public void methodX(){
    .....
    methodY();
    methodZAsync(); //
}

@Transactional(propagation = Propagation.REQUIRED)
public void methodY(){
    .....
    someDatabaseOperations();
    .....
}

@Async
public void methodZAsync(){
    .....
    pollingBasedOnDataOperationsOfMethodY();
    .....
}

问题在于,methodZAsync 需要在开始工作之前提交 methodY() 的数据库操作。但是由于 methodZAsync 在不同的线程中运行,所以无法实现这一点。

一个选项是将 methodY 的事务设置为 @Transactional(propagation = Propagation.REQUIRES_NEW)。但由于 methodY 在多个地方使用且具有不同的用例,我不能这样做。

我查看了这个问题,但 TransactionSynchronization 是一个接口,我不确定如何处理其余未实现的方法。

因此,我认为,如果我可以在不修改 methodY() 的情况下,让 methodX() 告诉 methodY() 使用一个新的事务(简单的方法:关闭当前正在运行的事务),那么问题就可以解决。

methodX() 中是否可以实现这一点,而不必修改 methodY()

英文:

I have 3 methods defined the following way. methodX and methodY are defined in different classes. methodY and methodZAsync are defined in same class.

@Transactional(propagation = Propagation.REQUIRED)
public void methodX(){
    .....
    methodY();
    methodZAsync(); //
}


@Transactional(propagation = Propagation.REQUIRED)
public void methodY(){
    .....
    someDatabaseOperations();
    .....
}


@Async
public void methodZAsync(){
    .....
    pollingBasedOnDataOperationsOfMethodY();
    .....
}

The problem here is, methodZAsync requires methodY()'s DB operations to be committed before it can start it's work. And this fails because methodZAsync runs in a different thread.

One option is to make methodY's transaction to use @Transactional(propagation = Propagation.REQUIRES_NEW). But since methodY is used in multiple places with a different use cases, I'm not allowed to do that.

I've checked this question but TransactionSynchronization is an interface and Im not sure what to do with rest of the un-implemented methods.

So I thought, instead of making changes in methodY(), If I can somehow make methodX() to tell methodY() to use a new transaction(naive way: close current running transaction), It'll fix the things for me.

Is this doable from methodX() without having to modify methodY()?

答案1

得分: 1

你可以围绕methodY()创建一个新的包装方法,该方法会创建一个新的事务。然后,你可以从methodX()调用这个新方法,而不会影响任何其他用例。

@Transactional(propagation = Propagation.REQUIRED)
public void methodX(){
    .....
    methodYInNewTxn();
    methodZAsync(); //
}

@Transactional(propagation = Propagation.REQUIRES_NEW)
public void methodYInNewTxn() {
    methodY();
}

@Transactional(propagation = Propagation.REQUIRED)
public void methodY(){
    .....
    someDatabaseOperations();
    .....
}
英文:

You can create a new wrapper method around methodY() that creates a new transaction. Then you can call this new method from methodX() without impacting any other use cases.

@Transactional(propagation = Propagation.REQUIRED)
public void methodX(){
    .....
    methodYInNewTxn();
    methodZAsync(); //
}

@Transactional(propagation = Propagation.REQUIRES_NEW)
public void methodYInNewTxn() {
    methodY();
}

@Transactional(propagation = Propagation.REQUIRED)
public void methodY(){
    .....
    someDatabaseOperations();
    .....
}

答案2

得分: 0

参考Spring Boot,还有以下内容:

> @Transactional(propagation = Propagation.REQUIRES_NEW)

方法可供查阅。希望您能获得正确的答案。

英文:

Refer spring boot, there is also

> @Transactional(propagation = Propagation.REQUIRES_NEW)

method are there go through. Hope you will get your proper answer

huangapple
  • 本文由 发表于 2020年10月13日 12:46:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/64328695.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定