Second Level Cache - JPA and Eclipselink

Asked

Viewed 277 times

2

I would like to totally disable the JPA/Eclipselink cache, but I’m not getting it.

I can disable the first level cache using:

query.setHint("javax.persistence.cache.storeMode", CacheStoreMode.REFRESH)

But the second level cache is not disabled.

What should I do to make sure the subtables (@Manytoone) are searched directly from the BD without using cache?

Persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
  <persistence-unit name="br.com.empresa_Sistema_war_1.0-SNAPSHOTPU" transaction-type="JTA">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <jta-data-source>jdbc/sistema</jta-data-source>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <shared-cache-mode>NONE</shared-cache-mode>
    <properties>
      <property name="eclipselink.cache.shared.default" value="false"/>
      <property name="eclipselink.query-results-cache" value="false"/>
      <property name="eclipselink.refresh" value="true"/>
      <property name="eclipselink.logging.level" value="FINE"/>
    </properties>
  </persistence-unit>
</persistence>
  • Tried to use @Cacheable(false)?

  • It would be nice to also take a look at documenting.

  • @Felipesoares, I added @Cacheable(false) in the main and secondary entity class, but it did not work. Keeps showing updated only the main entity information, the secondary updates in the BD, but in the queries appears unchanged.

1 answer

1


The problem was in the statement of EntityManager used to pick up the query items:

private static final EntityManager em = Persistence.createEntityManagerFactory("br.com.empresa_Sistema_war_1.0-SNAPSHOTPU").createEntityManager();  

I started using the EntityManager of Facade entity and the cache is no longer used, prevailing the file settings Persistence.xml, listed above.

Stabbing

@PersistenceContext(unitName = "br.com.empresa_Sistema_war_1.0-SNAPSHOTPU")
protected abstract EntityManager getEntityManager();

public EntityManager getEM() {
    return getEntityManager();
}

I didn’t even need to use the setHint() to configure the CacheStoreMode, that in the EM earlier worked only to disable the main entity cache, not working for second-level entities.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.