-->
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.  [ 9 posts ] 
Author Message
 Post subject: Problems with second level cache (EH),parent child relations
PostPosted: Wed Dec 24, 2008 3:09 am 
Newbie

Joined: Wed Dec 24, 2008 1:27 am
Posts: 4
Problems with second level cache (EH),
Newly created child object is not added to parent's (which is in second level cache)
collection of child objects.

I am facing problems with second level cache(EH),
I am creating child object using session.save(childObj). After closing the session,
I am opening a new session and retrievig parent object through it. But the collection in
the parent does not contain the child object created in the above step
even though child record is created in the data base.

But I could see delete working perfectly OK. If I delete the child object using
session.delete(childObj), the child object is deleted from it's parent object's collection.

Even though I could solve this problem by adding
the child object to the collection and save the parent, this is not feasible solution for me.
Parent has lot of child object types which are updated in many parts of the application.

Please help solve this issue. I am new to this forum.


Hibernate version: 3.3.1

Mapping documents:

Relevant portions of LevelType.hbm.xml



<class name="LevelType" table="levelTypes" dynamic-update="true">
<cache usage="nonstrict-read-write" region="enterpriseCacheRegion"/>

<id name="id" type="com.rrd.xspace.data.levels.hibernate.HiberLevelTypeID">
<column name="id" length="32"/>
<generator class="com.rrd.xspace.data.levels.hibernate.LevelTypeIDGenerator"></generator>
</id>
..............
................
...............
<set name="levelMetaDataTypes" inverse="true" lazy="false" cascade="all-delete-orphan">
<cache usage="nonstrict-read-write" region="enterpriseCacheRegion"/>
<key column="levelTypeId"/>
<one-to-many not-found="ignore" class="com.rrd.xspace.data.levels.metadata.LevelMetaDataType"/>
</set>

</class>

Relevant portions of LevelMeataDataType.hbm.xml


<class name="LevelMetaDataType" table="levelMetaDataTypes" dynamic-update="true">
<cache usage="nonstrict-read-write" region="enterpriseCacheRegion"/>
<id name="id" type="com.rrd.xspace.data.levels.hibernate.HiberLevelMetaDataTypeID">
<column name="id" length="32"/>
<generator class="com.rrd.xspace.data.levels.hibernate.LevelMetaDataTypeIDGenerator"></generator>
</id>
.................
............

<many-to-one name="levelType" column="levelTypeId" lazy="proxy" fetch="select"/>


</class>


ehcache.xml


<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="ehcache.xsd">
<diskStore path="java.io.tmpdir" />

<cacheManagerPeerProviderFactory
class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"
properties="peerDiscovery=manual,rmiUrls=//localhost:40001/enterpriseCacheRegion"/>

<cacheManagerPeerListenerFactory
class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"
properties="hostName=localhost, port=40002,socketTimeoutMillis=1200000" />

<cache name="enterpriseCacheRegion" maxElementsInMemory="1000"
eternal="true" timeToIdleSeconds="1200" timeToLiveSeconds="1200"
overflowToDisk="false" diskPersistent="false"
diskExpiryThreadIntervalSeconds="1200"
memoryStoreEvictionPolicy="LRU">

<cacheEventListenerFactory
class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"
properties="replicateAsynchronously=true,
asynchronousReplicationIntervalMillis=20,
replicatePuts=true,
replicateUpdates=true,
replicateUpdatesViaCopy=false,
replicateRemovals=true " />
</cache>

<defaultCache maxElementsInMemory="1000" eternal="true"
timeToIdleSeconds="1200" timeToLiveSeconds="1200" overflowToDisk="false"
diskPersistent="false" diskExpiryThreadIntervalSeconds="1200"
memoryStoreEvictionPolicy="LRU" />

</ehcache>



Code between sessionFactory.openSession() and session.close():

Session session = HibernateSessionFactory.getSession(); // opens new session
session.save(levelMetaDataTypeObj); // Child Object which has
//parent object reference

session.flush();
session.close();

// the below code is in another method call

Session session = HibernateSessionFactory.getSession(); // opens new session

Criteria criteria = session.createCriteria(LevelType.class);
criteria.add(Restrictions.eq("id", id));
LevelType type = (LevelType) criteria.uniqueResult();// LevelType
//is parent


Set levelMetaDataTypes = type.getLevelMetaDataTypes(); // this
//set does not contain child object created above

session.close();


Debug level Hibernate log excerpt:

_________________
Regards,
Ganesh


Top
 Profile  
 
 Post subject: RE: Problems with second level cache (EH),parent child relat
PostPosted: Tue Dec 30, 2008 2:35 am 
Newbie

Joined: Wed Dec 24, 2008 1:27 am
Posts: 4
Hi,

Did I not provide enough information to make my issue understood?
Some one would have already faced similar problem. Please help.

Regards
ganesh.

_________________
Regards,
Ganesh


Top
 Profile  
 
 Post subject: Re: Problems with second level cache (EH),parent child relat
PostPosted: Thu Feb 05, 2009 8:58 am 
Newbie

Joined: Thu Feb 05, 2009 8:47 am
Posts: 2
nagag124 wrote:
Problems with second level cache (EH),
Newly created child object is not added to parent's (which is in second level cache)
collection of child objects.
[...]
Relevant portions of LevelMeataDataType.hbm.xml
Code:
  <class name="LevelMetaDataType" table="levelMetaDataTypes" dynamic-update="true">
        .................
        <many-to-one name="levelType" column="levelTypeId" lazy="proxy" fetch="select"/>
  </class>


I had a problem very very similar to yours. My guess is that it's a bug in Hibernate. However, I did find a solution: adding
Code:
  cascade="save-update, persist, merge"
to the many-to-one did the trick for me.

In your case that would be:
Code:
  <class name="LevelMetaDataType" table="levelMetaDataTypes" dynamic-update="true">
        .................
        <many-to-one name="levelType" column="levelTypeId" lazy="proxy" cascade="save-update, persist, merge" fetch="select"/>
  </class>


Last edited by gjvoosten on Thu Feb 05, 2009 11:20 am, edited 1 time in total.

Top
 Profile  
 
 Post subject: RE: Problems with second level cache (EH),parent child relat
PostPosted: Thu Feb 05, 2009 9:24 am 
Newbie

Joined: Wed Dec 24, 2008 1:27 am
Posts: 4
Thanks a lot for your reply. I will try this and let you know.
Mean while I would like to tell something interesting about caching that I found.

The way hibernate session interacts with the second level cache can be controlled by setting the cache mode on session
(session.setCacheMode()). By looking at the documentation of CacheMode.java,
I understood that a new item will be put in the second level cache only
while retreiving it from database. Session does not put the item in second level cache when you create new record using session.save(). I have verified this by creating a new object using session.save()
in all 4 different cache modes and at the same time observed the number of items in the Second level cache. The number of items
would never increase until I retrieve the just created object using session.get().

_________________
Regards,
Ganesh


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 09, 2009 5:23 am 
Newbie

Joined: Wed Dec 24, 2008 1:27 am
Posts: 4
The suggested solution has worked.
Thanks a lot gjvoosten.

_________________
Regards,
Ganesh


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 12, 2009 6:40 am 
Newbie

Joined: Thu Feb 05, 2009 8:47 am
Posts: 2
nagag124 wrote:
The suggested solution has worked.
Thanks a lot gjvoosten.

You're welcome!

On a related note: I have done more testing with my own application, and found that there are several problems with second-level caching, unfortunately. A working application with hibernate.cache.use_second_level_cache=false exhibits the following when hibernate.cache.use_second_level_cache=true; apart from the one you mentioned I experienced:
    - LazyInitializationExceptions that only appear when second-level caching is enabled
    - a child is added to a parent's collection in the database, but the collection in the second-level cache is not updated
    - a child is added to a parent's collection in the database twice, but only once in the second-level cache
    - a new child object is added to a parent's collection, and upon persisting the child I get a Hibernate exception stating that the child instance is transient and I need to persist it first...

All these are with collection caching; I have now completely abandoned second-level caching of collections.

With second-level caching of entities I have found no problems. Well, not yet anyway. Second-level caching of (named) queries also has caused me no problems so far.

Version details: this is with hibernate-3.2.6.ga, hibernate-entitymanager-3.3.2.GA and ehcache-1.5.0; my model is in .hbm.xml files, with Java classes generated by hibernate3-maven-plugin:hbm2java.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 24, 2009 7:39 am 
Newbie

Joined: Tue Feb 24, 2009 7:23 am
Posts: 3
This seems to be a new problem. We have been using 3.1.2GA for a couple years with either JBoss Cache or Coherence as the second-level cache.

I decided to try out the 3.3.1 to see if it was worth upgrading since this was a good time in our product's lifecycle.

The only changes I made were to specify the Oracle10gDialect and specify the new JBoss Cache.

I am seeing similar problems to what you are seeing, and have not found any solution. I agree that it appears that the cache is not getting updated as we would expect. I save the parent assuming the children will get updated which worked well in the past.

From what I can tell the database seems fine, but the collection is not getting updated consistently in the cache. It is hard to determine what is going on, because I have not been able to come up with a pattern for when it works or doesn't.

I am wondering if a workaround is to evict the collection from the second-level cache when I change it.

I am a bit leary upgrading, because I would have expected improvements and not new problems for what seems like something pretty basic.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 24, 2009 7:41 am 
Newbie

Joined: Tue Feb 24, 2009 7:23 am
Posts: 3
This seems to be a new problem. We have been using 3.1.2GA for a couple years with either JBoss Cache or Coherence as the second-level cache.

I decided to try out the 3.3.1 to see if it was worth upgrading since this was a good time in our product's lifecycle.

The only changes I made were to specify the Oracle10gDialect and specify the new JBoss Cache.

I am seeing similar problems to what you are seeing, and have not found any solution. I agree that it appears that the cache is not getting updated as we would expect. I save the parent assuming the children will get updated which worked well in the past.

From what I can tell the database seems fine, but the collection is not getting updated consistently in the cache. It is hard to determine what is going on, because I have not been able to come up with a pattern for when it works or doesn't.

I am wondering if a workaround is to evict the collection from the second-level cache when I change it.

I am a bit leary upgrading, because I would have expected improvements and not new problems for what seems like something pretty basic.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 24, 2009 7:41 am 
Newbie

Joined: Tue Feb 24, 2009 7:23 am
Posts: 3
This seems to be a new problem. We have been using 3.1.2GA for a couple years with either JBoss Cache or Coherence as the second-level cache.

I decided to try out the 3.3.1 to see if it was worth upgrading since this was a good time in our product's lifecycle.

The only changes I made were to specify the Oracle10gDialect and specify the new JBoss Cache.

I am seeing similar problems to what you are seeing, and have not found any solution. I agree that it appears that the cache is not getting updated as we would expect. I save the parent assuming the children will get updated which worked well in the past.

From what I can tell the database seems fine, but the collection is not getting updated consistently in the cache. It is hard to determine what is going on, because I have not been able to come up with a pattern for when it works or doesn't.

I am wondering if a workaround is to evict the collection from the second-level cache when I change it.

I am a bit leary upgrading, because I would have expected improvements and not new problems for what seems like something pretty basic.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 9 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.