kwi*_*atz 6 hibernate jpa second-level-cache shared-cache wildfly
我正在使用WildFly 8.1,所以JPA 2.1和Hibernate 4.3.5
我想在WildFly中使用JPA共享缓存/二级缓存
我遵循WildFly文档:https://docs.jboss.org/author/display/WFLY8/JPA+Reference+Guide#JPAReferenceGuide-UsingtheInfinispansecondlevelcache
这是我的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>
Run Code Online (Sandbox Code Playgroud)
我将属性hibernate.cache.use_second_level_cache设置为true并将shared-cache-mode设置为ENABLE_SELECTIVE
我希望缓存的实体(@Entity)使用@Cacheable(true)进行注释,如下所示:
@Entity
@Cacheable(true)
public class Tooltip implements Serializable {
@Id
private String path ;
private String description ;
private Boolean rendered ;
//...
}
Run Code Online (Sandbox Code Playgroud)
但每次我访问一个网页时,hibernate都会生成大量的select来获取我指示为@Cacheable(true)的所有实体,即使我已经访问过该页面(在同一个会话中).
所以似乎共享缓存/二级缓存不起作用
我错过了什么?
谢谢hwellmann
我试图在persitence.xml中将hibernate.cache.use_query_cache设置为true
<?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>
Run Code Online (Sandbox Code Playgroud)
和我正在使用的查询中的提示
@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 {
//...
}
Run Code Online (Sandbox Code Playgroud)
但问题仍然存在
我也尝试使用新版本的WildFly:8.2所以Hibernate 4.3.7但问题仍然存在
二级缓存只影响直接实体查找,对应于EntityManager.find().
如果您试图避免影响缓存实体的各种SELECT查询,您还需要启用查询缓存:
<property name="hibernate.cache.use_query_cache" value="true" />
Run Code Online (Sandbox Code Playgroud)
并且您需要org.hibernate.cacheable=true为要缓存的每个查询设置查询提示。
| 归档时间: |
|
| 查看次数: |
3319 次 |
| 最近记录: |