JPA shared cache / second level cache in WildFly
Asked Answered
E

1

6

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

Epigraphic answered 21/1, 2015 at 11:17 Comment(2)
Have you solved your problem?Shall
Did you enable statistics & checked WildFly management console > Runtime > JPA tab?Loritalorn
B
2

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.

Big answered 21/1, 2015 at 17:46 Comment(2)
Thank you, but unfortunatly it didn't works for me, I edit my post to explainEpigraphic
Setting cacheable in the query is important, maybe you can show the client code to see if you're doing it right? Alternatively, enable TRACE logging for org.hibernate and maybe even org.infinispan to see what's going exactly. Stepping through the code with a remote debugger would also help.Infertile

© 2022 - 2024 — McMap. All rights reserved.