permaculture playing cards*
The moose likes Servlets and the fly likes Singletons in servlets - threadsafe? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Singletons in servlets - threadsafe?" Watch "Singletons in servlets - threadsafe?" New topic
Author

Singletons in servlets - threadsafe?

Hank Jordan
Greenhorn

Joined: Oct 23, 2006
Posts: 11
Hi all,

I am using a singleton object to read configuration values from an XML file on the web server. All the class does is read values from the file, store them in instance variables, and return them via getter methods. This data is shared across the web application, and will probably never change while the application is running. Do I need to worry about thread safety?

I thought it would make sense to make the config object a singleton, so I gave it a private constructor and a getInstance() method. As per the singleton design pattern, I made the getInstance() method synchronized to prevent multiple instances from being created. It seems to me that it wouldn't be a problem if there were multiple instances, since they would be storing the same values, but I can't help wondering if there might be some hidden pitfall, since multi-threading with servlets is a complex issue.

So, my question is, do I need to synchronize the methods of my config object, or can I allow multiple instances? Any help is greatly appreciated.

Here is the code. Note that the initialize() method must be called at least once before getInstance().

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60766
    
  65

If the singleton is initialized by a one-time event such as a context listener, and then only read by the request threads, there's no chance for read-write contention and so this is safe.

That said, I never use singletons to share application-wide data; that's what application scope (the servlet context) is for.
[ January 22, 2008: Message edited by: Bear Bibeault ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Hank Jordan
Greenhorn

Joined: Oct 23, 2006
Posts: 11
OK, that sounds like a better solution. Thanks!
Kim Kantola
Ranch Hand

Joined: May 17, 2001
Posts: 274
Hello, reviving this old thread because I have a similar issue going on and it is helping me to see where I think I am going wrong.

In my singleton class, I am also only having requests get data, but instead of initialized data as described in this thread, my get method actually hits the database to get a result set of data each time. It is only doing a select, so I thought this was OK, but my app is locking up , so I guess it is not OK.

Is it only ok to use static get methods with servlets if the thing that is being returned is already set previously as in serverAdress with this example? If I had to go and create a database connection and select serverAddress from a database table in this example, would that be a problem?
Thanks,
Kim
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4165
    
  21

You should be able to access the database with the method as you describe as long as you don't share any resources between threads. Resources include the Database Connection object. There could be several reasons that the application is locking up, so a profiler may help. Things to look out for:

1) Are you creating a new connection to the DB with each method, or is there a single connection used by the singleton? The first should be fine (conceptually) but the second may cause conflict.

-> You really care about references here. Best may be to deploy a Connection Pool with a fixed amount of connections, but from the method's point of view it is a unique object

2) Is your get method synchronized? This would cause a backup of threads waiting their turn. Search for other solutions like only using method-local or Thread-local data so there would be no need to synchronize. If you do synchronize gather all the code that needs synchronization together and only synchronize the smallest possible block - not the full method.

3) Do you close all your resources in a finally block? You don't want to have left-over DB clutter killing your app.


Steve
Kim Kantola
Ranch Hand

Joined: May 17, 2001
Posts: 274
Hmm, if I understand your answer right, it seems like my code should be OK.
I call the method inside a struts action as follows:


The static getDataList method is not synchronized, just static, it is as follows:


When I get the connection, that is also a static method of a singleton class called "Database" as follows

Am I right that this should be ok? The problem I am seeing is that I see two requests calling the "getDataList" method at almost the exact same time, and then the app freezes up.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4165
    
  21

I see nothing wrong with this code.

Does this work when a single request calls the method?

Your logs don't show anything?

Have you checked your database logs to see if there is something going on there?

Have you tried running a profiler to see what specific step the methods are holding on?
Kim Kantola
Ranch Hand

Joined: May 17, 2001
Posts: 274
Thanks Steve. Yes, this code works fine if there is just one request to it.

There are no smoking guns in my logs, no exceptions or anything. What I do see is two logmessages I pring out right before calling this method, indicating that two requests are calling it close together. (We have multiple people testing app at same time which caused this) Then, I do not see the log message I have in the code right after the return from this method. So the logs indicate that somehow the code gets stuck there.

I will have to turn my database logging on to see what is there.

Have you tried running a profiler to see what specific step the methods are holding on?
- I am not sure how to do this.
Kim Kantola
Ranch Hand

Joined: May 17, 2001
Posts: 274
Ooh, by profiler do you mean something like Fiddler?
Kalyan Ram
Greenhorn

Joined: Dec 29, 2003
Posts: 14
Originally posted by Kim Kantola:
Ooh, by profiler do you mean something like Fiddler?

No, I think Steve Luke meant something like -> http://profiler.netbeans.org/
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4165
    
  21

Yes. I meant a Java Profiler like JProfiler, the one integrated with NetBeans, or a handful of others out there.

I believe that Fiddler is a client-side HTML debugger which is different and wouldn't help you in this case.
[ August 23, 2008: Message edited by: Steve Luke ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Singletons in servlets - threadsafe?
 
Similar Threads
Why constuctor can't be marked as final?
Singleton & multithread env.
Want to know why this is not a threading issue ?
Singleton Class ?
B&S 2.3.2 - Data class as a singleton