简单搭了SSH.用的是srping3.1,struts2.3.8,hibernate4.1.现在想给hibernate配二级缓存,在网上找了好多,都报错
以下是代码:
applicationContext.xml:<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<property name="jdbcUrl"
value="jdbc:mysql://localhost:3306/adloocon?autoReconnect=true&amp;useUnicode=true&amp;characterEncoding=utf-8" />
<property name="user" value="root" />
<property name="password" value="123456" />
<!-- 自动重连 -->
<property name="testConnectionOnCheckin" value="true"></property>
<!-- 连接池每隔60秒自动检测数据库连接情况 -->
<property name="idleConnectionTestPeriod" value="60"></property>
<!-- -->
<property name="maxStatements" value="0"></property>
<!-- 响应超时 -->
<property name="checkoutTimeout" value="60000"></property>
<!-- 一次性创建新连接的数目 -->
<property name="acquireIncrement" value="4"></property>
<!-- 初始化时创建的连接数 -->
<property name="initialPoolSize" value="2"></property>
<!-- 最大连接数 -->
<property name="maxPoolSize" value="10"></property>
<property name="numHelperThreads" value="3"></property>
</bean> <bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:hibernate.cfg.xml" />
</bean>

<!-- ***底层需要Session.begins...开始事务***  -->
<bean id="txtManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
     <property name="sessionFactory" ref="sessionFactory"/> 
</bean>hibernate.cfg.xml: <session-factory>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.cache.user_second_level_cache">true</property>
        <property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property>

     <mapping resource="hibernate/AdminUser.hbm.xml"/>
    </session-factory>把二级缓存的去掉,程序运行正常,要是加上<property name="hibernate.cache.user_second_level_cache">true</property>
        <property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property>
就会报:
Caused by: java.lang.NoSuchMethodError: net.sf.ehcache.config.CacheConfiguration.isTerracottaClustered()Z
at org.hibernate.cache.ehcache.internal.util.HibernateUtil.validateEhcache(HibernateUtil.java:101)
at org.hibernate.cache.ehcache.AbstractEhcacheRegionFactory.getCache(AbstractEhcacheRegionFactory.java:185)
at org.hibernate.cache.ehcache.AbstractEhcacheRegionFactory.buildEntityRegion(AbstractEhcacheRegionFactory.java:132)
at org.hibernate.cache.ehcache.EhCacheRegionFactory.buildEntityRegion(EhCacheRegionFactory.java:48)
at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:349)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1744)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1782)
at org.springframework.orm.hibernate4.LocalSessionFactoryBuilder.buildSessionFactory(LocalSessionFactoryBuilder.java:189)
at org.springframework.orm.hibernate4.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:350)
at org.springframework.orm.hibernate4.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:335)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1514)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1452)
... 59 more
hibernate二级缓存有这么难配么求教了~~~

解决方案 »

  1.   

    <property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property>改为
    <property name="hibernate.cache.provider_class">net.sf.ehcache.hibernate.EhCacheProvider</property>
      

  2.   

    改了之后,报:
    Caused by: org.hibernate.cache.NoCacheRegionFactoryAvailableException: Second-level cache is used in the application, but property hibernate.cache.region.factory_class is not given, please either disable second level cache or set correct region factory class name to property hibernate.cache.region.factory_class (and make sure the second level cache provider, hibernate-infinispan, for example, is available in the classpath).
    at org.hibernate.cache.internal.NoCachingRegionFactory.buildEntityRegion(NoCachingRegionFactory.java:69)
    at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:349)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1744)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1782)
    at org.springframework.orm.hibernate4.LocalSessionFactoryBuilder.buildSessionFactory(LocalSessionFactoryBuilder.java:189)
    at org.springframework.orm.hibernate4.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:350)
    at org.springframework.orm.hibernate4.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:335)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1514)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1452)
    ... 59 more
      

  3.   

    你没有ehcache-core-2.4.3.jar吧?还改为<property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property>
      

  4.   

    有这个包的改为<property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property>后,报:
    Caused by: java.lang.NoSuchMethodError: net.sf.ehcache.config.CacheConfiguration.isTerracottaClustered()Z
    at org.hibernate.cache.ehcache.internal.util.HibernateUtil.validateEhcache(HibernateUtil.java:101)
    at org.hibernate.cache.ehcache.AbstractEhcacheRegionFactory.getCache(AbstractEhcacheRegionFactory.java:185)
    at org.hibernate.cache.ehcache.AbstractEhcacheRegionFactory.buildTimestampsRegion(AbstractEhcacheRegionFactory.java:173)
    at org.hibernate.cache.ehcache.EhCacheRegionFactory.buildTimestampsRegion(EhCacheRegionFactory.java:48)
    at org.hibernate.cache.spi.UpdateTimestampsCache.<init>(UpdateTimestampsCache.java:63)
    at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:510)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1744)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1782)
    at org.springframework.orm.hibernate4.LocalSessionFactoryBuilder.buildSessionFactory(LocalSessionFactoryBuilder.java:189)
    at org.springframework.orm.hibernate4.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:350)
    at org.springframework.orm.hibernate4.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:335)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1514)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1452)
    ... 54 more
      

  5.   

    去掉了ehcache-1.6.2.jar还是报错:Caused by: java.lang.NoSuchMethodError: net.sf.ehcache.config.CacheConfiguration.isTerracottaClustered()Z
    at org.hibernate.cache.ehcache.internal.util.HibernateUtil.validateEhcache(HibernateUtil.java:101)
    at org.hibernate.cache.ehcache.AbstractEhcacheRegionFactory.getCache(AbstractEhcacheRegionFactory.java:185)
    at org.hibernate.cache.ehcache.AbstractEhcacheRegionFactory.buildTimestampsRegion(AbstractEhcacheRegionFactory.java:173)
    at org.hibernate.cache.ehcache.EhCacheRegionFactory.buildTimestampsRegion(EhCacheRegionFactory.java:48)
    at org.hibernate.cache.spi.UpdateTimestampsCache.<init>(UpdateTimestampsCache.java:63)
    at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:510)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1744)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1782)
    at org.springframework.orm.hibernate4.LocalSessionFactoryBuilder.buildSessionFactory(LocalSessionFactoryBuilder.java:189)
    at org.springframework.orm.hibernate4.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:350)
    at org.springframework.orm.hibernate4.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:335)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1514)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1452)
    ... 59 more
      

  6.   

    去掉了ehcache-1.6.2.jar还是报错:Caused by: java.lang.NoSuchMethodError: net.sf.ehcache.config.CacheConfiguration.isTerracottaClustered()Z
    at org.hibernate.cache.ehcache.internal.util.HibernateUtil.validateEhcache(HibernateUtil.java:101)
    at org.hibernate.cache.ehcache.AbstractEhcacheRegionFactory.getCache(AbstractEhcacheRegionFactory.java:185)
    at org.hibernate.cache.ehcache.AbstractEhcacheRegionFactory.buildTimestampsRegion(AbstractEhcacheRegionFactory.java:173)
    at org.hibernate.cache.ehcache.EhCacheRegionFactory.buildTimestampsRegion(EhCacheRegionFactory.java:48)
    at org.hibernate.cache.spi.UpdateTimestampsCache.<init>(UpdateTimestampsCache.java:63)
    at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:510)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1744)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1782)
    at org.springframework.orm.hibernate4.LocalSessionFactoryBuilder.buildSessionFactory(LocalSessionFactoryBuilder.java:189)
    at org.springframework.orm.hibernate4.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:350)
    at org.springframework.orm.hibernate4.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:335)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1514)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1452)
    ... 59 more
    我感觉既然SSH,那hibernate.cfg.xml文件还需要吗?
      

  7.   

    去掉了ehcache-1.6.2.jar还是报错:Caused by: java.lang.NoSuchMethodError: net.sf.ehcache.config.CacheConfiguration.isTerracottaClustered()Z
    at org.hibernate.cache.ehcache.internal.util.HibernateUtil.validateEhcache(HibernateUtil.java:101)
    at org.hibernate.cache.ehcache.AbstractEhcacheRegionFactory.getCache(AbstractEhcacheRegionFactory.java:185)
    at org.hibernate.cache.ehcache.AbstractEhcacheRegionFactory.buildTimestampsRegion(AbstractEhcacheRegionFactory.java:173)
    at org.hibernate.cache.ehcache.EhCacheRegionFactory.buildTimestampsRegion(EhCacheRegionFactory.java:48)
    at org.hibernate.cache.spi.UpdateTimestampsCache.<init>(UpdateTimestampsCache.java:63)
    at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:510)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1744)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1782)
    at org.springframework.orm.hibernate4.LocalSessionFactoryBuilder.buildSessionFactory(LocalSessionFactoryBuilder.java:189)
    at org.springframework.orm.hibernate4.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:350)
    at org.springframework.orm.hibernate4.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:335)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1514)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1452)
    ... 59 more
    我感觉既然SSH,那hibernate.cfg.xml文件还需要吗?
    那有没有能成功配置hinernate二级缓存的demo呢?配置在什么文件都可以
      

  8.   

    去掉了ehcache-1.6.2.jar还是报错:Caused by: java.lang.NoSuchMethodError: net.sf.ehcache.config.CacheConfiguration.isTerracottaClustered()Z
    at org.hibernate.cache.ehcache.internal.util.HibernateUtil.validateEhcache(HibernateUtil.java:101)
    at org.hibernate.cache.ehcache.AbstractEhcacheRegionFactory.getCache(AbstractEhcacheRegionFactory.java:185)
    at org.hibernate.cache.ehcache.AbstractEhcacheRegionFactory.buildTimestampsRegion(AbstractEhcacheRegionFactory.java:173)
    at org.hibernate.cache.ehcache.EhCacheRegionFactory.buildTimestampsRegion(EhCacheRegionFactory.java:48)
    at org.hibernate.cache.spi.UpdateTimestampsCache.<init>(UpdateTimestampsCache.java:63)
    at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:510)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1744)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1782)
    at org.springframework.orm.hibernate4.LocalSessionFactoryBuilder.buildSessionFactory(LocalSessionFactoryBuilder.java:189)
    at org.springframework.orm.hibernate4.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:350)
    at org.springframework.orm.hibernate4.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:335)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1514)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1452)
    ... 59 more
    我感觉既然SSH,那hibernate.cfg.xml文件还需要吗?
    那有没有能成功配置hinernate二级缓存的demo呢?配置在什么文件都可以我现在的项目在用,但东西太多,有时间我整理一个简单的demo!
      

  9.   

    <property name="cache.use_second_level_cache">true</property>
    <property name="cache.region.factory_class">
       org.hibernate.cache.ehcache.EhCacheRegionFactory
    </property>
    Hibernate4.X好像就是cache.region.factory_class,provider_class那个好像不行
    另外确实SSH就应该配到Spring的bean里面去了