简体   繁体   English

休眠二级ehcache小姐

[英]hibernate second level ehcache miss

I'm trying to get a second-level hibernate cache working, using the ehcache implementation. 我正在尝试使用ehcache实现来获得二级hibernate缓存。

I'm sure it's some obvious noob mistake I'm making, but I can't see what it is! 我确定这是我正在做的一些明显的noob错误,但我看不出它是什么!

To test out my cache, I'm doing the following: 为了测试我的缓存,我正在做以下事情:

Creating an object and saving it. 创建一个对象并保存它。 I 'get' it once within the transaction, where I can see I get the object back without a db hit, which is just the hibernate first level cache doing its thing. 我在事务中得到了一次,在那里我可以看到我在没有数据库命中的情况下返回对象,这只是休眠的第一级缓存。

Then I commit the transaction and start a new session. 然后我提交事务并开始一个新会话。

This time, when I 'get' the object, I can see a cache miss from ehcache in the debugs. 这次,当我'得到'对象时,我可以在调试中看到来自ehcache的缓存未命中。 I would have expected the object to be in the cache now since I saved it in the previous transaction? 因为我将它保存在上一个事务中,所以我原本期望该对象在缓存中?

Here's my code: 这是我的代码:

  Session session = getSession();   
  session.beginTransaction();

  Test1 test1a = new Test1();
  test1a.setId(5);
  test1a.setName("Test 1");
  test1a.setValue(10);
  // Touch it
  session.save(test1a);

  // Now get it
  Test1 test1b = (Test1)session.get(Test1.class, 5);

  // Within a transaction, the session caches it - no db hit
  System.out.println("GOT object with value "+test1b.getValue());

  session.getTransaction().commit();

  System.out.println("Between sessions");

  session = getSession();
  session.beginTransaction();

  test1b = (Test1)session.get(Test1.class, 5);

  System.out.println("GOT object with value "+test1b.getValue());

  session.getTransaction().commit(); 

And here's the relevant part of my hibernate.cfg.xml 这是我的hibernate.cfg.xml的相关部分

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

And my ehcache.xml 还有我的ehcache.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="http://ehcache.sf.net/ehcache.xsd"> 
    <cache name="com.play.hibernate1.Test1" maxElementsInMemory="1000" eternal="false"     timeToLiveSeconds="600" overflowToDisk="false"/>
    <defaultCache maxElementsInMemory="10000" eternal="false"
overflowToDisk="true" timeToIdleSeconds="10" timeToLiveSeconds="20" diskPersistent="true" />
    </ehcache>

And my annotations 还有我的注释

@Entity
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class Test1 {

And the debug logs 和调试日志

11:21:03,474 DEBUG CacheManager:239 - Configuring ehcache from classpath.
11:21:03,479 DEBUG ConfigurationFactory:122 - Configuring ehcache from ehcache.xml found in the classpath: file:/Users/bw/Documents/workspace/hibernate1/target/classes/ehcache.xml
11:21:03,479 DEBUG ConfigurationFactory:87 - Configuring ehcache from URL: file:/Users/brucewood/Documents/workspace/hibernate1/target/classes/ehcache.xml
11:21:03,480 DEBUG ConfigurationFactory:139 - Configuring ehcache from InputStream
11:21:03,485 DEBUG BeanHandler:213 - Ignoring ehcache attribute xmlns:xsi
11:21:03,485 DEBUG BeanHandler:213 - Ignoring ehcache attribute xsi:noNamespaceSchemaLocation
11:21:03,517 DEBUG ConfigurationHelper:208 - No CacheManagerEventListenerFactory class specified. Skipping...
11:21:03,518 DEBUG ConfigurationHelper:183 - No CachePeerListenerFactoryConfiguration specified. Not configuring a CacheManagerPeerListener.
11:21:03,518 DEBUG ConfigurationHelper:159 - No CachePeerProviderFactoryConfiguration specified. Not configuring a CacheManagerPeerProvider.
11:21:03,525 DEBUG ConfigurationHelper:135 - No BootstrapCacheLoaderFactory class specified. Skipping...
11:21:03,526 DEBUG ConfigurationHelper:135 - No BootstrapCacheLoaderFactory class specified. Skipping...
11:21:03,532 DEBUG MemoryStore:73 - Initialized net.sf.ehcache.store.LruMemoryStore for com.play.hibernate1.Test1
11:21:03,533 DEBUG LruMemoryStore:71 - com.play.hibernate1.Test1 Cache: Using SpoolingLinkedHashMap implementation
11:21:03,533 DEBUG Cache:429 - Initialised cache: com.play.hibernate1.Test1
1528 [Finalizer] INFO org.hibernate.connection.DriverManagerConnectionProvider - cleaning up connection pool: jdbc:mysql://localhost:3306/play
1668 [main] INFO org.hibernate.impl.SessionFactoryObjectFactory - Not binding factory to JNDI, no JNDI name configured
GOT object with value 10
Hibernate: 
    /* insert com.play.hibernate1.Test1
        */ insert 
        into
            Test1
            (name, value, id) 
        values
            (?, ?, ?)
1274984463818|1|1|batch|/* insert com.play.hibernate1.Test1 */ insert into Test1 (name, value, id) values (?, ?, ?)|/* insert com.play.hibernate1.Test1 */ insert into Test1 (name, value, id) values ('Test 1', 10, 5)
1274984463820|1|1|statement|/* insert com.play.hibernate1.Test1 */ insert into Test1 (name, value, id) values (?, ?, ?)|/* insert com.play.hibernate1.Test1 */ insert into Test1 (name, value, id) values ('Test 1', 10, 5)
1274984463821|1|1|commit||
Between sessions
11:21:03,823 DEBUG EhCache:68 - key: com.play.hibernate1.Test1#5
11:21:03,823 DEBUG MemoryStore:138 - com.play.hibernate1.Test1Cache: com.play.hibernate1.Test1MemoryStore miss for com.play.hibernate1.Test1#5
11:21:03,823 DEBUG Cache:661 - com.play.hibernate1.Test1 cache - Miss
11:21:03,824 DEBUG EhCache:77 - Element for com.play.hibernate1.Test1#5 is null
Hibernate: 
    /* load com.play.hibernate1.Test1 */ select
        test1x0_.id as id0_0_,
        test1x0_.name as name0_0_,
        test1x0_.value as value0_0_ 
    from
        Test1 test1x0_ 
    where
        test1x0_.id=?
1274984463829|4|1|statement|/* load com.play.hibernate1.Test1 */ select test1x0_.id as id0_0_, test1x0_.name as name0_0_, test1x0_.value as value0_0_ from Test1 test1x0_ where test1x0_.id=?|/* load com.play.hibernate1.Test1 */ select test1x0_.id as id0_0_, test1x0_.name as name0_0_, test1x0_.value as value0_0_ from Test1 test1x0_ where test1x0_.id=5
1274984463831|-1||resultset|/* load com.play.hibernate1.Test1 */ select test1x0_.id as id0_0_, test1x0_.name as name0_0_, test1x0_.value as value0_0_ from Test1 test1x0_ where test1x0_.id=5|name0_0_ = Test 1, value0_0_ = 10
GOT object with value 10
1274984463834|0|1|commit||

Thanks for your help! 谢谢你的帮助!

The problem was that I was using NONSTRICT_READ_WRITE. 问题是我使用的是NONSTRICT_READ_WRITE。 It seemed to fit the bill from this explanation in the docs: 它似乎符合文档中这个解释的法案:

If the application only occasionally needs to update data (ie if it is extremely unlikely that two transactions would try to update the same item simultaneously), and strict transaction isolation is not required, a nonstrict-read-write cache might be appropriate. 如果应用程序仅偶尔需要更新数据(即,如果两个事务不太可能同时尝试更新同一项),并且不需要严格的事务隔离,则非严格读写缓存可能是合适的。 If the cache is used in a JTA environment, you must specify hibernate.transaction.manager_lookup_class. 如果在JTA环境中使用缓存,则必须指定hibernate.transaction.manager_lookup_class。 In other environments, you should ensure that the transaction is completed when Session.close() or Session.disconnect() is called. 在其他环境中,您应确保在调用Session.close()或Session.disconnect()时完成事务。

but actually it looks like it doesn't add saved objects to the cache.. Docs are a little thin here, IMO. 但实际上它看起来并没有将保存的对象添加到缓存中。文档在这里有点薄,IMO。 There is a better treatment of the caching options in Java Persistence with Hibernate, although still a little thin on concrete explanation of what actually happens to your data. 使用Hibernate对Java Persistence中的缓存选项有一个更好的处理方法,尽管对于数据实际发生的具体解释仍然有点薄弱。 Anyway.. 无论如何..

I changed my caching option to READ_WRITE, and now it works as expected. 我将缓存选项更改为READ_WRITE,现在它按预期工作。

Note also that the first time an object gets added to the cache, you get a 'cache miss' in the ehcache debugs. 另请注意,第一次将对象添加到缓存时,您会在ehcache调试中获得“缓存未命中”。 Presumably it looks in its cache, finds the object is not there, records a miss, then adds the object. 据推测,它在其缓存中查找,发现对象不存在,记录未命中,然后添加对象。 After that, you get 'hits'. 在那之后,你会得到'命中'。

Also be wary of the bug in org.hibernate.action.internal.BulkOperationCleanupAction.#affectedEntity see discussion in both 还要警惕org.hibernate.action.internal.BulkOperationCleanupAction中的错误。#affectededEntity参见两者中的讨论

https://forum.hibernate.org/viewtopic.php?f=1&t=998970 https://forum.hibernate.org/viewtopic.php?f=1&t=998970

and

https://hibernate.atlassian.net/browse/HHH-4035 https://hibernate.atlassian.net/browse/HHH-4035

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM