diff --git a/node/src/main/kotlin/net/corda/node/utilities/AppendOnlyPersistentMap.kt b/node/src/main/kotlin/net/corda/node/utilities/AppendOnlyPersistentMap.kt index 639a02ff85..945cc2135a 100644 --- a/node/src/main/kotlin/net/corda/node/utilities/AppendOnlyPersistentMap.kt +++ b/node/src/main/kotlin/net/corda/node/utilities/AppendOnlyPersistentMap.kt @@ -123,8 +123,12 @@ abstract class AppendOnlyPersistentMapBase( } protected fun loadValue(key: K): V? { - val result = currentDBSession().find(persistentEntityClass, toPersistentEntityKey(key)) - return result?.apply { currentDBSession().detach(result) }?.let(fromPersistentEntity)?.second + val session = currentDBSession() + // IMPORTANT: The flush is needed because detach() makes the queue of unflushed entries invalid w.r.t. Hibernate internal state if the found entity is unflushed. + // We want the detach() so that we rely on our cache memory management and don't retain strong references in the Hibernate session. + session.flush() + val result = session.find(persistentEntityClass, toPersistentEntityKey(key)) + return result?.apply { session.detach(result) }?.let(fromPersistentEntity)?.second } operator fun contains(key: K) = get(key) != null