-->
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: Thread safety problem?
PostPosted: Tue Mar 28, 2006 4:21 pm 
Beginner
Beginner

Joined: Thu Feb 05, 2004 4:59 am
Posts: 23
Servlet code:
Code:
doGet(...) {
      List results = DataLoader.getResultSet();
}



Extra class
Code:
public class DataLoader {
     ...

    public static final List getResultSet() throws HibernateException {
        ConnectionHandler handler = (ConnectionHandler) connections.get(connectionName);
        Session session = handler.getOrCreateSession();
        Criteria criteria = session.createCriteria(...);
        ....
        return criteria.list();
    }
}


1) Is the above code thread-safe?
2) If yes, then is it ok if I put all queries used by my application in DataLoader's static methods and will be executing them from all servlets?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 29, 2006 12:27 am 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
1) That depends on what connections and handler are. I'm guessing that connections is a threadlocal, which would mean that the code is threadsafe (because no non-threadlocal state is maintained).

2) If by query you mean the string that you pass into createQuery, then yes. If you mean the object that's returned from createQuery, then no, as they would all be generated in the classloader thread but available to all threads. As an alternative to statically storing the query strings, you could (or IMO, should) put your queries into your mapping file and use session.getNamedQuery to get them. That will definitely be threadsafe.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 29, 2006 12:51 am 
Beginner
Beginner

Joined: Thu Feb 05, 2004 4:59 am
Posts: 23
tenwit, thanks.

ConnectionHandlers are ThreadLocal.
I meant that I'm going to store all DB related operations (criteria queries, SQL native queries, e.t.c) in one big class DataLoader to separate business logic from DB stuff.
All servlets will be running DataLoader's static methods like getCustomer(), getCustomerData(), getAdvertisement() concurrently.

Is this a good idea?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 29, 2006 1:25 am 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
It's a fairly primitive and brittle way of doing things, but yes, that will work. The normal pattern would be closer to having a separate factory object, frequently called a DAO object, that provides the various load, save methods on a per class basis (you'd have a CustomerDataLoader, AdvertisementDataLoader, etc.). Those DataLoaders could easily use one small DataLoader class with the threadlocals you need. This is a more maintainable way of doing the same thing.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 29, 2006 2:41 am 
Beginner
Beginner

Joined: Thu Feb 05, 2004 4:59 am
Posts: 23
Thank you, I got it.


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.