dirty reads of associations in hibernate second level cache

edbras

When enabling ehCache (2.7.0) as Hibernate (4.3.7) Second level cache, Hibernate returns the old collection association.

Model: A Member has a Wallet with Wallet transactions.

Scenario: a Wallet transaction is added to a Member in a transaction (with ehcache enabled). However, after the scenario (after the commit), the Wallet transaction isn't present in the Member, whereas it's present in the db.

Scenarios testing Code:

startTransaction(); // used to create a transaction through Spring.
member = findMemberById(); // Hibernate "get()" to retrieve member from Db.
final WalletTransaction walTx = member.getEnsureWallet().addWalletTransaction(10); // add wallet tx of 10 euro.
member.saveOrUpdate(); // will update the member and the wallet transactions through cascading
commitTransaction();

// assert wallet transaction is present
startTransaction();
final Taxer mem = findMemberById(member.getId()); // refresh member in session through it's PK, logging indicates it comes from cache.
// final Taxer mem = findMemberByLoginName(member.getLoginName()); // when retrieving the member through it's loginName, the test works.
assertTrue(mem.containsWalletTransaction(walTx)); // FAILS
commitTransaction();

The hibernate model member snippet:

<class name="com.core.domain.MemberDefault" table="mem" discriminator-value="Mem" >
   <component name="wallet" class="com.core.domain.Wallet">
        <set name="transactions" table="wallet_tx" cascade="save-update, delete" >
            <!--cache usage="read-write" /-->
            <key column="idMember" not-null="true" />

            <composite-element class="com.core.domain.WalletTransactionDefault">
              <property name="amount" type="big_decimal" column="amount" />
               .... (more props)
            </composite-element>
        </set>
  </component>
</class

The MemberDefault and WalletDefault class snippets:

public class MemberDefault implements Member {
  private Wallet wallet;
....
}

public class WalletDefault implements Wallet {
 private Set<WalletTransaction> transactions;

public void setTransactions(Set<WalletTransaction> transactions) {
  this.transactions = transaction;
 }

 public Set<WalletTransaction> getTransactions() {
  return this.transactions;
 }
}

Notes:

  1. In case I not add the wallet transaction in a transaction (remove the first start/commit), the test is performed with success (The above is isolated testing code to reproduce bug).
  2. In case I turn off the second-level, the test works.
  3. In case I not retrieve the member from the Db through it's PK, but through it's loginName, such that a query is used by Hibernate, and as such the query cache, the test works.

I debugged, enabled hibernate/ehcache debug logging, modified the cache settings in hibernate, tried older Hibernate 4 and ehCache version, but don't seem to solve it, a bit frustrating.

Please some advice on how to solve this?

edbras

I solved it by always assigning an instance to the wallet field (hibernate component) in MemberDefault. That is, instead of:

private Wallet wallet;

we had to use:

private Wallet wallet = new WalletDefault();

in the MemberDefault class.

Is this a bug, or does it has any logic ? I think it is a bug as Hibernate knows that it's a component of type WalletDefault from the hibernate config. (I discovered it by removing the Wallet component for testing)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Hibernate second level cache example

From Dev

Hibernate : Invalidating Second Level cache

From Dev

infinispan as second level cache hibernate

From Dev

Hazelcast Hibernate second-level cache configuration

From Dev

How to enable second level cache in Hibernate

From Dev

Ehcache second level cache not working with JPA and Hibernate?

From Dev

How hibernate second level cache works internally?

From Dev

Entity update using hibernate second level cache

From Dev

Configuring Infinispan as remote second level cache for Hibernate

From Dev

hibernate second level cache with Redis -will it improve performance?

From Dev

How hibernate second level cache works internally?

From Dev

Is second level cache is Session specific in Hibernate

From Dev

When to use second level cache in hibernate

From Dev

How to enable second level cache in Hibernate 5.2.2?

From Dev

Hibernate 4 second level cache hibernate is not working with association (EhCache)

From Dev

How hibernate ensures second level cache is updated with latest data in database

From Dev

Spring Boot + JPA2 + Hibernate - enable second level cache

From Dev

Collections not read from hibernate/ehcache second-level-cache

From Dev

Hibernate: Is it possible to save a transient field in second level cache?

From Dev

Will HQL query use Hibernate second-level cache

From Dev

Mapping two CacheConcurrencyStrategy for the same Hibernate second-level cache region

From Dev

Hibernate: Is it possible to save a transient field in second level cache?

From Dev

Hibernate Second-Level Cache, Shared between different JVM

From Dev

Will HQL query use Hibernate second-level cache

From Dev

Hibernate + ehcache second level cache miss in simple example

From Dev

Cannot find class [org.hibernate.cache.EhCacheProvider] using Second-level cache of Hibernate

From Dev

Centralised Second Level Cache

From Dev

Centralised Second Level Cache

From Dev

Second level Cache Perfomance

Related Related

  1. 1

    Hibernate second level cache example

  2. 2

    Hibernate : Invalidating Second Level cache

  3. 3

    infinispan as second level cache hibernate

  4. 4

    Hazelcast Hibernate second-level cache configuration

  5. 5

    How to enable second level cache in Hibernate

  6. 6

    Ehcache second level cache not working with JPA and Hibernate?

  7. 7

    How hibernate second level cache works internally?

  8. 8

    Entity update using hibernate second level cache

  9. 9

    Configuring Infinispan as remote second level cache for Hibernate

  10. 10

    hibernate second level cache with Redis -will it improve performance?

  11. 11

    How hibernate second level cache works internally?

  12. 12

    Is second level cache is Session specific in Hibernate

  13. 13

    When to use second level cache in hibernate

  14. 14

    How to enable second level cache in Hibernate 5.2.2?

  15. 15

    Hibernate 4 second level cache hibernate is not working with association (EhCache)

  16. 16

    How hibernate ensures second level cache is updated with latest data in database

  17. 17

    Spring Boot + JPA2 + Hibernate - enable second level cache

  18. 18

    Collections not read from hibernate/ehcache second-level-cache

  19. 19

    Hibernate: Is it possible to save a transient field in second level cache?

  20. 20

    Will HQL query use Hibernate second-level cache

  21. 21

    Mapping two CacheConcurrencyStrategy for the same Hibernate second-level cache region

  22. 22

    Hibernate: Is it possible to save a transient field in second level cache?

  23. 23

    Hibernate Second-Level Cache, Shared between different JVM

  24. 24

    Will HQL query use Hibernate second-level cache

  25. 25

    Hibernate + ehcache second level cache miss in simple example

  26. 26

    Cannot find class [org.hibernate.cache.EhCacheProvider] using Second-level cache of Hibernate

  27. 27

    Centralised Second Level Cache

  28. 28

    Centralised Second Level Cache

  29. 29

    Second level Cache Perfomance

HotTag

Archive