persistence@glassfish.java.net

Re: Some questions about HardWeak/SoftWeak cache

From: James Sutherland <jamesssss_at_yahoo.com>
Date: Wed, 21 May 2008 06:39:03 -0700 (PDT)

TopLink will put new objects (persist) into the shared cache after the
transaction has been successfully committed to the database. The new object
will go into the transactional (non shared) cache when you call persist.

For HardCacheWeakIdentityMap (HardWeak), objects are put in both the
LinkedList (normal/hard references) and the HashMap (weak references). The
objects in the HashMap are free to garbage collect when not referenced. The
objects in the LinkedList will never garbage collect (until they are popped
from the list), even if memory is low. SoftWeak will garbage collect if
memory is low.

If your willing to drill down through internal code, there is also a
getSize(Class myClass, boolean recurse) method on IdentityMap that will give
you the current size.


jasonw41 wrote:
>
> I know the cache topic has been discussed a lot here, but after reading
> the related messages and Wonseok's blog(Thanks a lot.) I still have some
> questions about how HardWeak/SoftWeak caches work, so here is my story.
>
> I understand when I call em.persist(data) the data will be created and
> hold by persistence context until em closes. The question is: when does it
> go to shared cache? Does Toplink put the data into shared cache right
> after it is persisted?
>
> From TopLink Essentials JPA Extensions Reference I know HardWeak contains
> two caches: LinkedList and HashMap. I was wondering when the LinkedList is
> full, how does Toplink use the two caches? Does it just add the newly
> cached data into the LinkedList and push out the top one into the HashMap?
> Or is there any algorithm to decide whether data goes to LinkedList or
> HashMap?
>
> Another question is about the HardWeak cahce. TopLink Reference says the
> LinkedList holds hard references for cahced data. Does it mean thoese
> cache will never be gc-ed even the system is short of memory until Toplink
> cleans them after every 100nth access?
>
> The next question is how I can tell how many data have been cached in
> LinkedList or HashMap? I tried the following API:
> ((oracle.toplink.essentials.ejb.cmp3.EntityManager)em).getServerSession().getIdentityMapAccessor().printIdentityMaps()
> and it does give me the data list, but I am not sure if it is a proper
> way. By the way, above API works for java se. Could anybody tell me how to
> use it in an EJB when the em is obtained by annotation?
>
> I tried another way to count the HardWeak cahce and the sample is changed
> from Wonseok's blog.
> - 0 - Set cahce type HardWeak and size 50
> - 1 - In first transaction, persist 100 data and close the em
> - 2 - Update all 100 data in database by JDBC
> - 3 - Open the second transaction call em.find to retrieve all the 100
> data. Count all unchanged data which should be from the cache.
> But I am not sure if this is a valid test as well because of my first
> question: If Toplink puts new cahced data into LinkedList and the old one
> goes to HashMap which could be easily get gc-ed, then I will not get the
> correct number.
>
> Cheers,
> Jason
>


-----
---
http://wiki.eclipse.org/User:James.sutherland.oracle.com James Sutherland 
http://www.eclipse.org/eclipselink/
 EclipseLink ,  http://www.oracle.com/technology/products/ias/toplink/
TopLink 
Wiki:  http://wiki.eclipse.org/EclipseLink EclipseLink , 
http://wiki.oracle.com/page/TopLink TopLink 
Forums:  http://forums.oracle.com/forums/forum.jspa?forumID=48 TopLink , 
http://www.nabble.com/EclipseLink-f26430.html EclipseLink 
Book:  http://en.wikibooks.org/wiki/Java_Persistence Java Persistence 
-- 
View this message in context: http://www.nabble.com/Some-questions-about-HardWeak-SoftWeak-cache-tp17354187p17364233.html
Sent from the java.net - glassfish persistence mailing list archive at Nabble.com.