在EHCache中检索CacheManager对象

时间:2013-06-30 05:12:49

标签: java database hibernate orm ehcache

我正在使用Hibernate v3.2.5和EHCache(ehcache-1.2.3.jar)进行二级缓存。

这是 ehcache.xml 文件:

<ehcache>
  <defaultCache maxElementsInMemory="100" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="200" />
  <cache name="departments" maxElementsInMemory="100" eternal="false" timeToIdleSeconds="5" timeToLiveSeconds="200" />
</ehcache>

以下是我用于检索CacheManager的代码:

SessionFactory sf = new Configuration().configure("trial.cfg.xml").buildSessionFactory();
    Session session = sf.openSession();

    Dept department = (Dept)session.load(Dept.class, 1);
    //Performing some operations

    //Trying to get an instance of CacheManager
    CacheManager cm = new CacheManager("src/ehcache.xml");

我得到一个例外:

Exception in thread "main" java.lang.NullPointerException
at net.sf.ehcache.CacheManager.detectAndFixDiskStorePathConflict(CacheManager.java:269)
at net.sf.ehcache.CacheManager.configure(CacheManager.java:252)
at net.sf.ehcache.CacheManager.init(CacheManager.java:196)
at net.sf.ehcache.CacheManager.<init>(CacheManager.java:131)
at com.demo.Test.main(OneToMany.java:52)

同样,我尝试了这个替代方案:

Cache cache = CacheManager.getInstance().getCache("departments");

但我又得到了同样的例外。

修改

trial.cfg.xml

<property name="hibernate.connection.username">XXX</property>
<property name="hibernate.connection.password">XXX</property>
<property name="hibernate.connection.driver_class">oracle.jdbc.OracleDriver</property>
<property name="hibernate.connection.url">XXX</property>
<property name="cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
<property name="hibernate.jdbc.batch_size">30</property>
<property name="hibernate.dialect">org.hibernate.dialect.OracleDialect</property>
<property name="hibernate.cache.use_query_cache">false</property>
<property name="hibernate.cache.use_second_level_cache">true</property>
<property name="hibernate.connection.release_mode">after_transaction</property>
<property name="hibernate.connection.autocommit">true</property>
<property name="hibernate.connection.pool_size">0</property>
<property name="hibernate.current_session_context_class">thread</property>    
<property name="hibernate.show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>

请让我知道如何解决这个问题。

0 个答案:

没有答案