[Building Sakai] cache automatically updates itself?

Aaron Zeckoski azeckoski at unicon.net
Mon May 31 00:23:08 PDT 2010


> Re double caching, the Hibernate objects don't seem to be being cached at all (nor implicitly written), they are always being retrieved from the database and I always need to commit the save, hence the introduction of the MemoryService cache. Unless there is some other explicit way to check the Hibernate cache, but I thought it was all transparent, ie Hibernate figures it out whether to get from cache or lookup.


The hibernate cache is configured as part of the
org.sakaiproject.springframework.orm.hibernate.GlobalSessionFactory
and by default uses this cache:
org.sakaiproject.springframework.orm.hibernate.L2Cache. That uses the
same underlying ehcache as the memory service. As far as I have seen
it is pretty much automatic. Might be worth looking at your
SessionFactory (via debugger) to see what it is using for the L2
cache. The L1 cache in hibernate cannot be disabled and is always part
of every hibernate SessionFactory so you can be sure that one is
having an effect (though only within a given request and session).

As for the automatic save. It happens as part of the transaction
closing. The hibernate SessionManager will look for any changes
detected by the proxies around the object and then explicitly write
them to the DB if they have not already been written. This one has
bitten me before and I have had to explicitly work around it by
copying to the hibernate object rather than making changes to it
directly.
You do have to be using the GlobalSessionFactory for the first
behavior but the second one is just how hibernate works so you should
see that behavior (unless the transaction closes with a failure, then
it issues a rollback and the changes are discarded).

-AZ



On Sun, May 30, 2010 at 11:48 PM, Steve Swinsburg
<steve.swinsburg at gmail.com> wrote:
> Thanks Aaron, yes this is what I was doing, putting an Object into the cache and then updating the underlying object. I did some logging and confirmed that both before and after were still the same object, so all is well there.
>
> Re double caching, the Hibernate objects don't seem to be being cached at all (nor implicitly written), they are always being retrieved from the database and I always need to commit the save, hence the introduction of the MemoryService cache. Unless there is some other explicit way to check the Hibernate cache, but I thought it was all transparent, ie Hibernate figures it out whether to get from cache or lookup.
>
> cheers,
> Steve
>
>
> On 30/05/2010, at 5:37 PM, Aaron Zeckoski wrote:
>
>> It's a little odd to double cache things but I doubt that will hurt
>> the system too badly. It will just use up more memory.
>>
>> Without seeing the code, I can only guess but it sounds like maybe you
>> are placing an object into the cache and then updating the fields on
>> that same object. Since the memory service just uses ehcache
>> underneath and that stores its cached objects in a type of
>> ConcurrentHashMap it means any updates to the object would be
>> reflected in the cache as well (regardless of any explicit cache
>> update command you might have issued).
>> The same thing is true of hibernate objects which are updated except
>> that those updates are written into the database (despite not issuing
>> a save or update command). They are both artifacts of the object
>> oriented structure of Java (though in the case of hibernate it is
>> because it puts a double proxy on all hibernate objects).
>>
>> Hope that is helpful.
>> :-)
>> -AZ
>>
>>
>>
>> On Sun, May 30, 2010 at 12:02 AM, Steve Swinsburg
>> <steve.swinsburg at gmail.com> wrote:
>>> They are hibernate objects for persistence, but I am putting them into a
>>> MemoryService cache.
>>> cheers.
>>>
>>> On Sat, May 29, 2010 at 6:02 PM, Aaron Zeckoski <azeckoski at unicon.net>
>>> wrote:
>>>>
>>>> Are these hibernate objects and are you talking about the hibernate
>>>> cache or is this simply an object you are placing into the in-memory
>>>> cache in the memory service?
>>>>
>>>> -AZ
>>>>
>>>>
>>>> On Sat, May 29, 2010 at 12:13 AM, Steve Swinsburg
>>>> <steve.swinsburg at gmail.com> wrote:
>>>>> Hi all,
>>>>>
>>>>> I have a cache in my app that caches objects loaded from the database so
>>>>> that subsequent requests use the cached version and don't hit the database
>>>>> again.
>>>>>
>>>>> On one of my pages I allow a user to update their own version of the
>>>>> object and it is saved to the database. What I am seeing however, is that
>>>>> this change is also reflected in the cached version, ie they are kept in
>>>>> sync. Is this normal behaviour? I haven't specifically coded for updates to
>>>>> be readded to the cache, I was just testing and noticed this was happening
>>>>> anyway.  I am not sure if it's expected behaviour so will explicitly update
>>>>> the cache with the updated verison when it's saved.
>>>>>
>>>>> Some pseudocode of what I have at the moment:
>>>>>
>>>>> getRecord(userId) {
>>>>>
>>>>>   check cache
>>>>>   if exists in cache, returned cached version
>>>>>
>>>>>   if not get from db and add to cache for future lookups
>>>>> }
>>>>>
>>>>> updateRecord(object) {
>>>>>   save object to db
>>>>> }
>>>>>
>>>>> So no explicit cache update in the updateRecord method, but it's
>>>>> happening anyway.
>>>>>
>>>>> Any input on the Cache/MemoryService appreciated.
>>>>>
>>>>> cheers,
>>>>> Steve
>>>>> _______________________________________________
>>>>> sakai-dev mailing list
>>>>> sakai-dev at collab.sakaiproject.org
>>>>> http://collab.sakaiproject.org/mailman/listinfo/sakai-dev
>>>>>
>>>>> TO UNSUBSCRIBE: send email to
>>>>> sakai-dev-unsubscribe at collab.sakaiproject.org with a subject of
>>>>> "unsubscribe"
>>>>>
>>>>
>>>>
>>>>
>>>> --
>>>> Aaron Zeckoski - Software Engineer - http://tinyurl.com/azprofile
>>>
>>>
>>
>>
>>
>> --
>> Aaron Zeckoski - Software Engineer - http://tinyurl.com/azprofile
>
>



-- 
Aaron Zeckoski - Software Engineer - http://tinyurl.com/azprofile


More information about the sakai-dev mailing list