# Syncing data between two processes with JPA and hibernate without refresh

**URL:** <https://discourse.hibernate.org/t/syncing-data-between-two-processes-with-jpa-and-hibernate-without-refresh/12388>\
**Category:** Hibernate ORM\
**Created:** [August 11, 2026, 6:42pm UTC](https://discourse.hibernate.org/t/syncing-data-between-two-processes-with-jpa-and-hibernate-without-refresh/12388 "2026-08-11T18:42:49Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![McModknower](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/mcmodknower/32/4369_2.png) [@McModknower](https://discourse.hibernate.org/u/McModknower)\
**Post date:** [August 11, 2026, 6:42pm UTC](https://discourse.hibernate.org/t/syncing-data-between-two-processes-with-jpa-and-hibernate-without-refresh/12388/1 "2026-08-11T18:42:49Z")

</div>

I have two processes using the same entities in the same table. When I read the row from process A, then write from process B, then read again from process A, i get the value before the write of B.

I know that I can use `entityManager.refresh(entity)` on the entity after each `find()` operation to get the correct one, but since hibernate receives the updated value from the database even WITHOUT doing `refresh()`, I would like to ask: **What other options do I have?**

> **Here are the log lines with log level TRACE, starting with the line that shows hibernate receives the update value (en-US) from the database, and ending with the log line from my code showing that the entity has the old value (de-DE)**
>
> ```auto
> [18:32:52] [ForkJoinPool.commonPool-worker-2/TRACE] [org.hibernate.orm.jdbc.extract]: extracted value ([4] : [VARCHAR]) - [en-US]
> [18:32:52] [ForkJoinPool.commonPool-worker-2/TRACE] [org.hibernate.orm.results.loading]: StandardRowReader#readRow
> [18:32:52] [ForkJoinPool.commonPool-worker-2/TRACE] [org.hibernate.orm.results.loading.entity]: (o.h.s.r.g.e.i.EntityResultInitializer) Beginning Initializer#resolveKey process for entity : mypackage.SettingValueEntity(e)
> [18:32:52] [ForkJoinPool.commonPool-worker-2/DEBUG] [org.hibernate.orm.results]: Extracted JDBC value [0] - [3]
> [18:32:52] [ForkJoinPool.commonPool-worker-2/DEBUG] [org.hibernate.orm.results.loading.entity]: (EntityResultInitializer) Hydrated EntityKey (mypackage.SettingValueEntity(e)): 3
> [18:32:52] [ForkJoinPool.commonPool-worker-2/TRACE] [org.hibernate.orm.results.loading.entity]: (o.h.s.r.g.e.i.EntityResultInitializer) Beginning Initializer#resolveInstance process for entity (mypackage.SettingValueEntity(e)) : 3
> [18:32:52] [ForkJoinPool.commonPool-worker-2/DEBUG] [org.hibernate.orm.results]: Extracted JDBC value [2] - [LANGUAGE]
> [18:32:52] [ForkJoinPool.commonPool-worker-2/TRACE] [org.hibernate.orm.results.loading.entity]: (o.h.s.r.g.e.i.EntitySelectFetchInitializer) Beginning Initializer#resolveInstance process for entity (mypackage.SettingValueEntity(e).setting) : LANGUAGE
> [18:32:52] [ForkJoinPool.commonPool-worker-2/DEBUG] [org.hibernate.orm.results.loading]: Calling top-level assembler (0 / 1) : org.hibernate.sql.results.graph.entity.internal.EntityAssembler@27100cef
> [18:32:52] [ForkJoinPool.commonPool-worker-2/TRACE] [org.hibernate.orm.results.loading]: StandardRowReader#afterRow
> [18:32:52] [ForkJoinPool.commonPool-worker-2/TRACE] [org.hibernate.engine.internal.StatefulPersistenceContext]: Initializing non-lazy collections
> [18:32:52] [Velocity - Task Executor #0/INFO] [com.velocitypowered.proxy.console.VelocityConsole]: SettingBasedLocaleService.loadLocale(df252223-4d16-4abc-a8cd-f7228a25fa88) => de-DE
> 
> ```

---

<div class="post-metadata">

**Author:** ![beikov](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/beikov/32/258_2.png) [@beikov](https://discourse.hibernate.org/u/beikov)\
**Post date:** [August 12, 2026, 1:46pm UTC](https://discourse.hibernate.org/t/syncing-data-between-two-processes-with-jpa-and-hibernate-without-refresh/12388/2 "2026-08-12T13:46:58Z")

</div>

It strongly depends on what you want to achieve. You can acquire a `PESSIMISTIC_WRITE` lock when invoking `find()` to ensure you have the latest state and no other process can change the state while your transaction runs. You can also use optimistic locking if it’s ok for you to rollback changes in case of conflicts.

---

<div class="post-metadata">

**Author:** ![McModknower](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/mcmodknower/32/4369_2.png) [@McModknower](https://discourse.hibernate.org/u/McModknower)\
**Post date:** [August 17, 2026, 6:06pm UTC](https://discourse.hibernate.org/t/syncing-data-between-two-processes-with-jpa-and-hibernate-without-refresh/12388/3 "2026-08-17T18:06:35Z")

</div>

Using an optimistic lock works, but since I only change the value from a single process, while the other process only reads the value and is only doing one read per transaction, it feels like there should be some “lighter” tool for the job.

---

<div class="post-metadata">

**Author:** ![beikov](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/beikov/32/258_2.png) [@beikov](https://discourse.hibernate.org/u/beikov)\
**Post date:** [August 18, 2026, 9:36am UTC](https://discourse.hibernate.org/t/syncing-data-between-two-processes-with-jpa-and-hibernate-without-refresh/12388/4 "2026-08-18T09:36:43Z")

</div>

If you tell me what these processes do exactly, maybe I can suggest something, but with the little information that you gave, this is the best I can say.

---

<div class="post-metadata">

**Author:** ![McModknower](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/mcmodknower/32/4369_2.png) [@McModknower](https://discourse.hibernate.org/u/McModknower)\
**Post date:** [August 18, 2026, 4:50pm UTC](https://discourse.hibernate.org/t/syncing-data-between-two-processes-with-jpa-and-hibernate-without-refresh/12388/5 "2026-08-18T16:50:49Z")

</div>

I want to store language settings for users. Process A has a UI for setting the language, and both process A and B read this language setting to get the translations for the user set language.

The basics work, however currently some part of Hibernate caches the stuff, so when changing the language using the UI in Process A, process B does not get the updated value on the next query, even though logging shows hibernate received the updated value from the database.

If you need more info, please ask a more specific question.

---

<div class="post-metadata">

**Author:** ![beikov](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/beikov/32/258_2.png) [@beikov](https://discourse.hibernate.org/u/beikov)\
**Post date:** [August 19, 2026, 3:13pm UTC](https://discourse.hibernate.org/t/syncing-data-between-two-processes-with-jpa-and-hibernate-without-refresh/12388/6 "2026-08-19T15:13:01Z")

</div>

Hibernate ORM doesn’t just randomly cache stuff, it has well defined semantics. In a persistence context, an entity that was read from the database retains its in-memory state, unless you explicitly ask the `EntityManager` to refresh it. When you have a persistence context open for too long, then you might get this behavior, but usually, you should only keep it open for the length of a transaction, which should always be as short as possible.

If you enable second level or query caching, then Hibernate ORM will cache data in the configured cache. When that happens, Hibernate ORM in one JVM has no knowledge about changes done to the database by another JVM. This is a common “problem” that you could solve with either clustering, a short TTL or invalidating the second level or query cache when you know that a change happened.

---

<div class="post-metadata">

**Author:** ![McModknower](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/mcmodknower/32/4369_2.png) [@McModknower](https://discourse.hibernate.org/u/McModknower)\
**Post date:** [August 26, 2026, 7:59am UTC](https://discourse.hibernate.org/t/syncing-data-between-two-processes-with-jpa-and-hibernate-without-refresh/12388/7 "2026-08-26T07:59:25Z")

</div>

> [@beikov](#):
>
> When you have a persistence context open for too long, then you might get this behavior, but usually, you should only keep it open for the length of a transaction, which should always be as short as possible.

That is probably the problem I have, I will investigate that at a later date. For now thanks for all the help.
