为什么我的二级缓存实现会出错?

时间:2013-11-19 03:42:48

标签: java hibernate

我正在尝试在我的应用程序上实现二级缓存,但它会返回以下错误。我对setCacheMode持怀疑态度。

hibernate.cfg.xml中

 ...
  <property name="cache.provider_class">
            org.hibernate.cache.EhCacheProvider
  </property>

  <property name="hibernate.cache.use_query_cache">true</property>
 ...

Product.Java

public class Product implements Serializable {
...
    @Id
    @GeneratedValue
    @Column(name = "id")
    @Cache(usage= CacheConcurrencyStrategy.READ_ONLY)
    public long getID() {
        return ID;
    }
...
}

Model.Java

....
final Session session = HibernateUtil.getSession();
        try {
            final Transaction tx = session.beginTransaction();
            try {
                Product product = (Product) session.get(Product.class, id);
                session.setCacheMode(CacheMode.NORMAL);
                tx.commit();
                return product;
            } catch (Exception e) {
                tx.rollback();
                e.printStackTrace();
            }
        } finally {
            HibernateUtil.closeSession();
        }
.....

错误

INFO: ** Exception in SessionFactory **
SEVERE: org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.spi.CacheImplementor]
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:186)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:150)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:131)
    at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:264)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1760)
    at com.my.util.HibernateUtil.configureSessionFactory(HibernateUtil.java:26)
    at com.my.util.HibernateUtil.<clinit>(HibernateUtil.java:43)
    at com.my.controller.Default.execute(Default.java:29)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:446)
    at com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:285)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    at org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:256)
    at 
    .....

的Util

 private static SessionFactory configureSessionFactory() {
        try {
            Configuration configuration = new Configuration();
            configuration.configure();
            serviceRegistry = new 
                     ServiceRegistryBuilder()
                    .applySettings(configuration.getProperties())
                    .buildServiceRegistry();

            sessionFactory = configuration.buildSessionFactory(serviceRegistry);

            return sessionFactory;
        } catch (HibernateException e) {
            System.out.append("** Exception in SessionFactory **");
            e.printStackTrace();
        }
       return sessionFactory;
  }

3 个答案:

答案 0 :(得分:6)

如果使用高于3.3的hibernate版本,则需要使用此属性

<property name="hibernate.cache.region.factory_class">
        org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory
   </property>

并为maven添加依赖

<dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-ehcache</artifactId>
        <version>${hibernate-core.version}</version>
    </dependency>

然后你用 产品产品=(产品)session.get(Product.class,id); 这将是二级缓存(如果会话关闭),而不是查询。

答案 1 :(得分:0)

你的hbm配置缺少二级缓存使用的重要部分。请添加hibernate.cache.use_second_level_cachehibernate.cache.region.factory_class选项。请参考您的hibernate版本的hbm手册,了解工厂的正确类值。

答案 2 :(得分:-1)

如果您使用高于3.3的休眠版本,则需要使用此属性:

<property name="hibernate.cache.region.factory_class">
     org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory
</property>

并为maven添加依赖项:

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-ehcache</artifactId>
    <version>${hibernate-core.version}</version>
</dependency>

然后你使用:

Product product = (Product) session.get(Product.class, id);

这将是二级缓存(如果会话已关闭),而不是查询。

您还需要覆盖buildsessionFactory。 检查您在以前版本的hibernate中使用的方法。 然后检查更高版本正在使用的内容 - 该方法只需要覆盖。

现在我重写了buildsessionfactory方法,因为我正在使用hibernate4.3.11-final。