-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: Multiple SessionFactory cache problem
PostPosted: Fri Jul 22, 2005 12:38 pm 
Regular
Regular

Joined: Fri Sep 17, 2004 10:51 am
Posts: 61
Location: Rimini, Italy
Hi, I need some light on this issue:
I'm using Hibernate 3.0.5 with two SessionFactory to connect to two different database. Every sessionFactory uses a different hibernate configuration file.
The databases has exactly the same structure but different data.
Since the tables I need are exactly the same, I'm using the same mapping documents.
The getter method for the object I need cycles the two source and returns the object given by the first answering source. Every source uses a simple session.get(); method to retrieve the object.
Everything works fine, but I noticed that when 2nd level cache is involved (ehcache), the first source always returns the object from the cache, even if the object is not in the corresponding db (it's in the other db!).

Some more info:
The sessionFactory are initialized in a static block and bound to a JNDI context, each with a different name, of course.
I'm using the sessionFactory.getCurrentSession() method to obtain a session.
I'm creating a single userTransaction for the whole request.

I've also tried to disable the cache for the object and everything works, without the problem described above.

Can someone shed some light on this? What I'm missing?
TIA

_________________
--
Marco


Top
 Profile  
 
 Post subject: Re: Multiple SessionFactory cache problem
PostPosted: Fri Jul 22, 2005 12:53 pm 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
marco-p wrote:
Hi, I need some light on this issue:
I'm using Hibernate 3.0.5 with two SessionFactory to connect to two different database. Every sessionFactory uses a different hibernate configuration file.
The databases has exactly the same structure but different data.
Since the tables I need are exactly the same, I'm using the same mapping documents.
The getter method for the object I need cycles the two source and returns the object given by the first answering source. Every source uses a simple session.get(); method to retrieve the object.
Everything works fine, but I noticed that when 2nd level cache is involved (ehcache), the first source always returns the object from the cache, even if the object is not in the corresponding db (it's in the other db!).

Some more info:
The sessionFactory are initialized in a static block and bound to a JNDI context, each with a different name, of course.
I'm using the sessionFactory.getCurrentSession() method to obtain a session.
I'm creating a single userTransaction for the whole request.

I've also tried to disable the cache for the object and everything works, without the problem described above.

Can someone shed some light on this? What I'm missing?
TIA


It sounds like you have both SessionFactory objects pointing to the same cache. I don't believe that the SessionFactory keeps track of whats in cache, but just checks the cache implementation when necessary and returns the object if it's found. If SF1 puts something into cache, it's logical that SF2 will find it when it looks in cache. There's no database dependency on the cache.


Top
 Profile  
 
 Post subject: Re: Multiple SessionFactory cache problem
PostPosted: Tue Jul 26, 2005 5:29 am 
Regular
Regular

Joined: Fri Sep 17, 2004 10:51 am
Posts: 61
Location: Rimini, Italy
Quote:
It sounds like you have both SessionFactory objects pointing to the same cache. I don't believe that the SessionFactory keeps track of whats in cache, but just checks the cache implementation when necessary and returns the object if it's found.


Yes, the beaviour is like you described.
I'm using two Session Factories to have separated 2nd level cache as max
said in this thread, at least this is what I understand from it.

Anyone know how I can use several SF with separated cache?

_________________
--
Marco


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 26, 2005 3:44 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Which cache are you using? What you are trying will only work on cache impls which are not singleton-based. For example EHCache is singleton-based and so multiple sesssion factories configured to use the EHCacheProvider will actually share underlying cache instances (provided all in the same "classloader scope").

If this is the problem (which it sounds like it is), there are really only two ways around this (aside from a different provider):
1) seperate the jars into truly seperate classloaders (a singleton is really only a singleton-per-classloader);
2) have each session factory assign its own distinct cache region prefix.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 27, 2005 12:31 pm 
Regular
Regular

Joined: Fri Sep 17, 2004 10:51 am
Posts: 61
Location: Rimini, Italy
Quote:
2) have each session factory assign its own distinct cache region prefix.


I think this option is simpler.
Setting a different value for the property hibernate.cache.region_prefix to each SessionFactory the cache behave correctly.

Thanks very much!

_________________
--
Marco


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.