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.  [ 2 posts ] 
Author Message
 Post subject: Multithreading + Illegal access to loading collection
PostPosted: Fri Oct 03, 2008 4:08 am 
Newbie

Joined: Thu Oct 02, 2008 12:58 pm
Posts: 1
Hello,

So we've been using Hibernate (version 3.2.3) for our project and we're encountering a pretty annoying (but possibly "normal") issue.

To make it simple, we have many 1--* relationships, like this:
[ Parent ] 1 ---------- * [ Child ]

These relationships are lazy, so that the collection of children associated with a parent is not fetched until one calls parent.getChildren().

Now the issue is, say we have two concurrent threads running, T1 and T2. T1 calls parent.getChildren(), hence Hibernate starts initializing (loading) the collection. In the meantime, T2 calls parent.getChildren(). There, an exception occurs: "Illegal access to loading collection".

Both threads are using the same hibernate session. We use only one session for the whole app because we need a constant 1-1 mapping between our Java object tree and the database rows, and we want to minimize the number of accesses to the database (the actual "persist" action is supposed to be done when the user has finished working on his/her data).

Is there any way to make this work? Or are we not handling this the right way?

Thanks in advance for your help. :)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 03, 2008 1:40 pm 
Expert
Expert

Joined: Mon Nov 26, 2007 2:29 pm
Posts: 443
Sessions are not Thread-safe.
If it is probable that your session be accessed by more than one Thread, no Hibernate device (nor any "managed environment" configuration trick) will release you from the responsibility of making that code thread-safe yourself.

You are not "overlooking" anything, but you do have to add threading code there (you know, getting a lock on the collection, and then synchronizing any code that modifies it).

As an additional precaution, I would increase the level of transaction isolation/propagation (or simply add some manual transaction code) in the
method(s) that modify that collection. But again, that doesn't get you off the hook regarding thread safety.

_________________
Gonzalo Díaz


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