• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Singletons in servlets - threadsafe?

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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().

 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Hank Jordan
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, that sounds like a better solution. Thanks!
 
Ranch Hand
Posts: 276
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Kim Kantola
Ranch Hand
Posts: 276
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 276
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 276
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ooh, by profiler do you mean something like Fiddler?
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic