JPA shared cache / second level cache in WildFly

佐手、 提交于 2019-12-08 20:36:32

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/28065897/jpa-shared-cache-second-level-cache-in-wildfly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!