using spring transactionManager with Hibernate after transaction committed only one repository of two is actually being committed

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

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

huangapple
  • 本文由 发表于 2020年8月24日 03:41:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/63551280.html
匿名

发表评论

匿名网友

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

确定