私は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>
プロパティ 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 ;
//...
}
しかし、Web ページにアクセスするたびに (同じセッションで) 既にページにアクセスしている場合でも、休止状態で @Cacheable(true) として示したすべてのエンティティを取得するために多くの選択が生成されます。
というわけで共有キャッシュ・二次キャッシュが機能していないようです
私は何か見落としてますか?
ありがとうございます
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>
そして、私が使用しているクエリのヒント
@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 {
//...
}
しかし、問題は残ります
私もWildFlyの新しいバージョンを試してみました:8.2なのでHibernate 4.3.7ですが、問題は残ります