Why is my Hibernate Query returning stale data?

前端 未结 2 2067
萌比男神i
萌比男神i 2021-02-10 21:55

Quick Version

Basically, I\'m updating a Hibernate Table and subsequent queries are loading a stale value.

Detailed Version

相关标签:
2条回答
  • 2021-02-10 22:55

    After CwmService.get().flushChanges(); // commits the transaction do an explicit commit. flush() only flushes the changes to db but does not commit it. I am not sure about flushChanges() though.

    0 讨论(0)
  • 2021-02-10 23:01

    I discovered the problem, but it introduces something else.

    Basically, when modifying a Book object's List<PageContent> field, Hibernate does three things:

    1. Expires the TimeStamp cache entry for both Book and PageContent
    2. Does many queries to reset the pageNum field on each PageContent object
    3. Removes the Book object from the Second Level Cache.

    This ensures that subsequent queries will search for new objects, etc. However:

    1. Hibernate fails to remove each renumbered PageContent object from the Second Level Cache

    As a result, any query for the list of pages will run properly, but then will fall back on stale Second Level Cache values for the actual data.

    I presume this is because Hibernate feels a pageNum change is not a change in data but a change in behind-the-scenes management. However, that is the data that I would like to read and display.

    The solution is to manually refresh every page after the insertion/deletion has occurred.

    0 讨论(0)
提交回复
热议问题