Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JPA shared cache / second level cache in WildFly

I'm using WildFly 8.1 so JPA 2.1 and Hibernate 4.3.5

I want to use JPA shared cache / second level cache in WildFly

I follow the WildFly documentation: https://docs.jboss.org/author/display/WFLY8/JPA+Reference+Guide#JPAReferenceGuide-UsingtheInfinispansecondlevelcache

here is my persitience.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="myAppPU" transaction-type="JTA">
    <jta-data-source>java:/jdbc/myAppDS</jta-data-source>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>
    <properties>
      <property name="hibernate.show_sql" value="true"/>
      <property name="hibernate.format_sql" value="true"/>
      <property name="org.hibernate.flushMode" value="MANUAL"/>
      <property name="hibernate.cache.use_second_level_cache" value="true"/>
    </properties>
  </persistence-unit>
</persistence>

I set the property hibernate.cache.use_second_level_cache to true and set the shared-cache-mode to ENABLE_SELECTIVE

And entities (@Entity) I want to cached are annotated with @Cacheable(true), like that:

@Entity
@Cacheable(true)
public class Tooltip implements Serializable {
  @Id
  private String path ;
  private String description ;
  private Boolean rendered ;
  //...
}

But each time I visit a web page hibernate generate a lot of select to get all entities I have indicate as @Cacheable(true) even if I already visit the page (in the same session).

So it seems that the shared cache / second level cache is not working

Did I miss something?


Thank you hwellmann

I tried to put hibernate.cache.use_query_cache to true in the persitence.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="myAppPU" transaction-type="JTA">
    <jta-data-source>java:/jdbc/myAppDS</jta-data-source>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>
    <properties>
      <property name="hibernate.show_sql" value="true"/>
      <property name="hibernate.format_sql" value="true"/>
      <property name="hibernate.cache.use_second_level_cache" value="true"/>
      <property name="hibernate.cache.use_query_cache" value="true" />
      <property name="org.hibernate.flushMode" value="MANUAL"/>
    </properties>
  </persistence-unit>
</persistence>

and and the hint in the query I'm using

@Entity
@NamedQueries({
    @NamedQuery(name = "FieldInfos.findAll", query = "SELECT i FROM FieldInfos i", hints = {@QueryHint(name="org.hibernate.cacheable",value="true")}),
    @NamedQuery(name = "FieldInfos.findByPath", query = "SELECT i FROM FieldInfos i WHERE i.path = :path", hints = {@QueryHint(name="org.hibernate.cacheable",value="true")})
})
@Cacheable(true)
public class FieldInfos implements Serializable {
    //...
}

but the problem remain

I also try with the new version of WildFly: 8.2 so Hibernate 4.3.7 but the problem remain too

like image 350
kwisatz Avatar asked Jan 21 '15 11:01

kwisatz


People also ask

How does JPA implement second level cache?

To enable second-level cache we have to use <shared-cache-mode> element in the persistence. xml, with one of the values as defined in SharedCacheMode enum. In following example we will use shared-cache-mode=ALL which causes all entities and entity-related state and data to be cached.

Which 2nd level cache is better in hibernate?

Hibernate second level cache uses a common cache for all the session object of a session factory. It is useful if you have multiple session objects from a session factory. SessionFactory holds the second level cache data. It is global for all the session objects and not enabled by default.

What is second level caching?

A second-level cache is a local store of entity data managed by the persistence provider to improve application performance. A second-level cache helps improve performance by avoiding expensive database calls, keeping the entity data local to the application.

Does hibernate support second level caching?

A Hibernate second-level cache is one of the data caching components available in the Hibernate object-relational mapping (ORM) library. Hibernate is a popular ORM library for the Java language, and it lets you store your Java object data in a relational database management system (RDBMS).


1 Answers

The second level cache only affects direct entity lookups, corresponding to EntityManager.find().

If you're trying to avoid all sorts of SELECT queries affecting your cached entities, you also need to enable the query cache:

    <property name="hibernate.cache.use_query_cache" value="true" />

and you need to set a query hint org.hibernate.cacheable=true for each query to be cached.

like image 182
Harald Wellmann Avatar answered Sep 19 '22 16:09

Harald Wellmann