英文:
using spring transactionManager with Hibernate after transaction committed only one repository of two is actually being committed
问题
我有一个带有@Transactional
注解的方法,它会更改两个不相关存储库中两个实体的状态。
大致如下:
@Transactional
public void foo() {
A a = repoA.findById(1);
a.setState(s1);
B b = repoB.findById(1);
b.setState(s2);
// (虽然多余,但我也执行 repoA.save(a); 和 repoB.save(b);)
}
我还有一个事务性方法bar
,它调用foo
方法并发布一个事件,该事件会被TransactionalEventListener
捕获,如下所示:
@Transactional
public void bar() {
foo();
applicationEventPublisher.publishEvent(new AppEvent(123));
}
以及
@Component
public class MyApplicationEventListener {
@TransactionalEventListener
public void handleAfterCommit(AppEvent appEvent){
//做些事情;
}
}
现在的问题是,当调用handleAfterCommit
方法时,80%的情况下只有(A a)
被提交,而(B b)
失去了它的更改。
我需要帮助来理解这里发生了什么,我尝试过调试和探索TransactionAspectSupport.currentTransactionStatus()
,但没有找到任何见解。
谢谢,
Eilon
英文:
I have a @Transactional
method that changes the state of two entities of different, not related, repositories.
something like this:
@Transactional
public void foo() {
A a = repoA.findById(1);
a.setState(s1);
B b = repoB.findById(1);
b.setState(s2);
// (and I also do repoA.save(a); and repoB.save(b); although it is redundant)
}
I also have a transactional method bar
that calls foo
and publishes an event that is being caught by a TransactionalEventListener
like this:
@Transactional
public void bar() {
foo();
applicationEventPublisher.publishEvent(new AppEvent(123));
}
and
@Component
public class MyApplicationEventListener {
@TransactionalEventListener
public void handleAfterCommit(AppEvent appEvent){
//do something;
}
}
Now the issue is that in 80% of the time when handleAfterCommit
method is invoked, only (A a )
is being committed but (B b)
is losing its changes.
I need help to understand what is going on here, I tried to debug and explore the
TransactionAspectSupport.currentTransactionStatus()
but didn't find any insights.
Thanks,
Eilon
答案1
得分: 0
我找到了问题,我们正在使用自定义的AttributeConverter,而且我们没有为相关的Java对象实现Equals方法,这导致每次选择性能检查失败,并进行完整的更新(覆盖其间已更改的值)
谢谢。
英文:
I found the issue, we are using a custom AttributeConverter and we didnt implement Equals for the relevant javav object, this caused every dirty check on select to fail and do a full update (overriding values that meanwhile have been changed)
Thanks
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论