通过JPA对数据库进行更新为什么会回滚?

huangapple 未分类评论45阅读模式
英文:

Why does the updating to database through JPA roll back?

问题

我正在使用Spring Boot Starter JPA 2.1.8。

以下是我所做的。在一个使用**@Transactional(RollbackFor = Exception.class)注解的HTTP请求处理方法中,我通过jpaRepository查询了一些对象,并修改了该对象的某个属性。但我没有**调用repository.save()方法。请求完成后,我检查了数据库,发现我的修改成功了。但后来,当一个定时线程通过jpa查询相同的对象时,我之前的修改消失了!

我想知道为什么会发生这种情况。更新明确写入了数据库,但后来被回滚了。

我尝试通过调用**repository.save()**方法来保存更新,它正常工作。但我仍然想知道为什么在我没有保存实体的情况下更新被写入然后回滚了?

有人能帮我找出原因吗?

英文:

I'm using spring boot starter jpa 2.1.8.

Here's what I did. In a http request handler method annotated with @Transactional(RollbaclFor = Exception.class), I queried some object through jpaRepository and modified some property of that object. But I didn't call the repository.save() method. After the request is finished, I checked the database and my modification succeeded. But later, when a scheduled thread queried the same object through jpa, my previous modification disappeared!

I wonder why is this happening. The updating was difinitely written to the database, but was rolled back later.

I tried to save the updating by calling repository.save() method, and it worked fine. But I still want to know why is the updating written and rolled back when I didn't save the entity?

Can someone help me find out why?

huangapple
  • 本文由 发表于 2020年4月10日 11:02:39
  • 转载请务必保留本文链接:https://java.coder-hub.com/61133393.html
匿名

发表评论

匿名网友

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

确定