2010-05-27 21 views
5

Tôi đang cố gắng để có được một bộ nhớ cache ngủ đông cấp thứ hai làm việc, bằng cách thực hiện ehcache.hibernate cấp độ thứ hai ehcache bỏ lỡ

Tôi chắc chắn đó là một số lỗi không rõ ràng mà tôi đang thực hiện, nhưng tôi không thể thấy nó là gì!

Để kiểm tra bộ nhớ cache của tôi, tôi làm như sau:

Tạo đối tượng và lưu. Tôi nhận được nó một lần trong giao dịch, nơi tôi có thể nhìn thấy tôi nhận được các đối tượng trở lại mà không có một hit db, mà chỉ là bộ nhớ cache cấp đầu tiên ngủ đông làm điều của nó.

Sau đó, tôi cam kết giao dịch và bắt đầu một phiên mới.

Lần này, khi tôi nhận được 'đối tượng, tôi có thể thấy một bộ nhớ cache bỏ lỡ từ ehcache trong các lỗi. Tôi đã có thể mong đợi các đối tượng được trong bộ nhớ cache bây giờ kể từ khi tôi đã lưu nó trong giao dịch trước đó?

Dưới đây là mã của tôi:

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(); 

Và đây là phần có liên quan của hibernate.cfg.xml tôi

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

Và ehcache.xml tôi

<?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> 

Và chú thích của tôi

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

Và bản ghi gỡ lỗi

 
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|| 

Nhờ sự giúp đỡ của bạn!

+0

Cho đến nay tôi đã truy tìm nó để lưu ban đầu - Tôi có thể thấy nó không có trong bộ nhớ cache sau đó. Tuy nhiên, nó nằm trong bộ nhớ cache sau khi tôi lấy nó từ cơ sở dữ liệu, và sau đó tôi có thể lấy lại nó từ bộ nhớ cache. Vì vậy, có vẻ như tôi không lưu nó vào bộ đệm. – Jeremy

Trả lời

7

Vấn đề là tôi đang sử dụng NONSTRICT_READ_WRITE. Nó dường như phù hợp với những hóa đơn từ lời giải thích này trong các tài liệu:

Nếu ứng dụng chỉ thỉnh thoảng nhu cầu cập nhật dữ liệu (ví dụ: nếu nó là cực kỳ chắc rằng hai giao dịch sẽ cố gắng cập nhật các cùng mục cùng một lúc) và không cần cách ly giao dịch nghiêm ngặt, bộ nhớ cache không đọc được ghi có thể là thích hợp. Nếu bộ nhớ cache được sử dụng trong môi trường JTA , bạn phải chỉ định hibernate.transaction.manager_lookup_class. Trong các môi trường khác, bạn nên đảm bảo rằng giao dịch là hoàn thành khi Session.close() hoặc Session.disconnect() được gọi.

nhưng thực sự có vẻ như nó không thêm đối tượng đã lưu vào bộ nhớ cache .. Tài liệu hơi mỏng ở đây, IMO. Có một điều trị tốt hơn về các tùy chọn bộ nhớ đệm trong Java Persistence với Hibernate, mặc dù vẫn còn một chút giải thích cụ thể về những gì thực sự xảy ra với dữ liệu của bạn. Dù sao ..

Tôi đã thay đổi tùy chọn bộ nhớ đệm thành READ_WRITE và bây giờ nó hoạt động như mong đợi.

Cũng lưu ý rằng lần đầu tiên một đối tượng được thêm vào bộ nhớ cache, bạn sẽ nhận được 'cache miss' trong các lỗi ehcache.Có lẽ nó trông trong bộ nhớ cache của nó, tìm thấy đối tượng không có, ghi lại một miss, sau đó thêm đối tượng. Sau đó, bạn nhận được 'số truy cập'.

+0

+2 cho câu hỏi và câu trả lời –

Các vấn đề liên quan