-->
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.  [ 3 posts ] 
Author Message
 Post subject: whats best practice for hibernate with threadpool? concerns
PostPosted: Tue Feb 28, 2012 2:39 pm 
Newbie

Joined: Tue Feb 28, 2012 2:35 pm
Posts: 3
My application is heavily using thread pool, even a method of a class may be executed in another thread using aspectj advice.

This brings me a problem of how to design my database entity using Hibernate JPA implementation, especially with one-to-many, many-to-many and so forth. The problem is my application uses session-per-thread pattern that EntityManager is stored in ThreadLocal, so the entity obtained in one thread is frequently passed to another thread. If I have any one-to-many like mapping in entity, the lazy fetching won't work since the entity is out of persistence.

The workaround I see is calling merge() to attach entity to thread's entity manager before any access to lazy fetching entity, but this requires a wrapper class for entity which call merge() in every getter of associated entity. This has a drawback that if I change some fields of the entity but not want to merge to persistence right now, calling a getter will screw up.

I did some research but not got any better solution. I was wondering this should be a common practice for threadpool oriented application. there must be a best practice pattern.

another way is never using such one-to-many facility but that introduces much boring coding burden.

Does anyone have a better idea?


Top
 Profile  
 
 Post subject: Re: whats best practice for hibernate with threadpool? concerns
PostPosted: Wed Feb 29, 2012 4:57 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
Hi,
I've never heard before of a threadpool oriented application; I do use threadpools myself in several occasions, but this sounds like an excessive usage, I hope you have good reasons for this and have done good research before deciding for this pattern.

How are you going to handle transactions? Also make sure when one thread receives the entity instance, the other threads have no more references to it: including the loading Session which should be closed. Transactions on the JDBC datasource are also commonly bound to the running thread, so you'll have to deal with that too.

The Hibernate Session is designed to wrap a full "unit of work", if you can't you should make sure you pass only the object identifiers to the alternative thread so that it will re-load the entity creating a new instance. If you configure second level caching, it won't need to reload from the database.

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject: Re: whats best practice for hibernate with threadpool? concerns
PostPosted: Thu Mar 01, 2012 7:19 pm 
Newbie

Joined: Tue Feb 28, 2012 2:35 pm
Posts: 3
Thanks sanne.grinovero.

The intention to use threadpool like this is my application consists of multiple services based on message.
So every service need to dispatch message to worker thread which also involves passing entity.
I also thinking passing id and reloading it, but it introduces lots of DB connection.

Looks like the only way is to look into secondary cache right now.


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