我的应用程序经常向数据库查询很少更改的数据。所以我决定使用缓存的EJB实体来优化它,如下所示: http://docs.jboss.org/ejb3/docs/tutorial/1.0.7/html/Caching_EJB3_Entities.html
但是,当我看一下hibernate sql logs(hibernate.show_sql = true)时,我仍然可以看到相同数量的查询命中数据库而没有配置缓存。这是我的persistence.xml文件:
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
version="1.0">
<persistence-unit name="myds">
<jta-data-source>java:/myds</jta-data-source>
(...) classes definitions
<class>com.my.class.MyEntityOne</class>
(...)
<properties>
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
<property name="hibernate.format_sql" value="false" />
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.JBossTransactionManagerLookup"/>
<property name="hibernate.transaction.factory_class" value="org.hibernate.ejb.transaction.JoinableCMTTransactionFactory"/>
<!-- 2nd level cache -->
<property name="hibernate.cache.use_second_level_cache" value="true"/>
<property name="hibernate.cache.use_query_cache" value="true"/>
<property name="hibernate.cache.region.factory_class" value="org.hibernate.cache.jbc2.JndiMultiplexedJBossCacheRegionFactory"/>
<property name="hibernate.cache.region.jbc2.cachefactory" value="java:CacheManager"/>
<property name="hibernate.cache.region.jbc2.cfg.entity" value="mvcc-entity"/>
<property name="hibernate.cache.region.jbc2.cfg.query" value="local-query"/>
</properties>
</persistence-unit>
我的所有JPA实体都注明了:
@Cache (usage=CacheConcurrencyStrategy.TRANSACTIONAL)
当我关闭服务器时,可能会在日志中看到以下异常:
org.hibernate.cache.CacheException:java.lang.IllegalStateException:缓存未处于STARTED状态! 在org.hibernate.cache.jbc2.BasicRegionAdapter.destroy(BasicRegionAdapter.java:243) at org.hibernate.impl.SessionFactoryImpl.close(SessionFactoryImpl.java:813) 在org.hibernate.ejb.EntityManagerFactoryImpl.close(EntityManagerFactoryImpl.java:46) 在org.jboss.jpa.deployment.ManagedEntityManagerFactory.destroy(ManagedEntityManagerFactory.java:93) 在org.jboss.jpa.deployment.PersistenceUnitDeployment.stop(PersistenceUnitDeployment.java:343) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 在java.lang.reflect.Method.invoke(Method.java:597)
我应该做一些额外的设置吗? 任何帮助表示赞赏!
提前致谢, 彼得
答案 0 :(得分:1)
缓存查询(如skaffman建议的那样):
Query query = manager.createQuery("SELECT i FROM ....");
query.setHint("org.hibernate.cacheable", true);
对于实体,我注意到,用@Cache注释所有实体是不够的。如果您的实体引用了其他实体(使用@Cache注释),则还必须在引用字段的顶部包含此注释:
@Cache (usage=CacheConcurrencyStrategy.TRANSACTIONAL)
@Entity
public class EntityA {
@Cache (usage=CacheConcurrencyStrategy.TRANSACTIONAL)
private Set<EntityB> entitiesB;
}
@Cache (usage=CacheConcurrencyStrategy.TRANSACTIONAL)
@Entity
public class EntityB {
}
我不知道这是否是一般规则,但就我而言,在我添加这些注释后,一切都开始按预期工作。