英文:
Drop / ignore current transaction and use upcoming transaction if available
问题
以下是翻译好的内容:
我有3个方法,定义如下。methodX
和 methodY
在不同的类中定义。methodY
和 methodZAsync
在同一个类中定义。
@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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论