File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Threads and Synchronization and the fly likes Possible threading problem in web application Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Possible threading problem in web application" Watch "Possible threading problem in web application" New topic

Possible threading problem in web application

Mike Himstead
Ranch Hand

Joined: Apr 12, 2006
Posts: 178

so far I was lucky enough not to encounter threading problems, now I got hit with a broad sword. I try to fix a bug in a web application that makes usage of a third-party library for opening database connections. These connections come from a container-supplied connection pool, the connections are supposed to be closed at the end of their life cycle to be returned to the connection pool again. Unfortenately, the pool sooner or later is full. My best guess so far is that we have a threading-related deadlock.

More details: a user request starts a for-loop. Inside the loop a new DBHandler object is created and the getPerson-Method is called, this method accesses the third-party library, namely a Context object used for DB access. A parts of DBHandler method look like this:

Let's say the connection pool is set to a maximum of 10 connections. Debugging shows that 10 connections are open the the application tries to get the 11th connections ending in a dead-lock (until the connections time out).

My reasoning is that somehow the clearContext()-Method isn't called thus the database resources aren't freed.
Alan Mehio
Ranch Hand

Joined: Apr 04, 2005
Posts: 73
You mention deadlock? based on what ? as a can see from the code especially from the getPerson method which is being attached by multi threads, there is not lock ( sychnronized) block at all.

I would imagine a dead lock scenario based on the give code is like that

The second thing with connection pooling for the third party, if the max has been exceeded, then the new thread should wait until there is another connection returned to the pool or is there a policy to increase the pool size.

My reasoning is that somehow the clearContext()-Method isn't called thus the database resources aren't freed.

Have you thought about calling the clearContext after directly calling person = createBeans(DBPerson);
Then what about the common resource "context"
the getPerson is not thread safe so two threads can use the same context for executing different username; is that OK for some reason which I am not aware of?

The method getContext() may return two context instances if is called by two threads at the same time
The method clearnContext() can end up with NullPointerException if it is called by two threads at the same time; So the best thing to make the class attribute or member class as volatile
private volatile Context context = null; // gives visiblity for other threads for any state change for this object.

Just some thoughts which may help

Alan Mehio
London, UK
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42965
I suspect that this:

should be something like this:

Otherwise new Context objects are being created all the time. Note that this method is not thread-safe - there's a possibilty that 2 Context objects will be created by concurrent threads, yet only one of them will be assigned to "context", and thus ever be eligible for a "close" call. If a DB connection is obtained inside of the Context constructor, then this is a serious problem.
Mike Himstead
Ranch Hand

Joined: Apr 12, 2006
Posts: 178
Thanks for your input. I hope I can give further information in a structured way.

First, Ulf, you're right, with your correction, that was a typo. I'm aware that copy and paste of code if better for forum conversation, but that would be too much code really.

Alan, my suspicion of a dead-lock comes from the connection pool. There's no policy to increase the pool. Currently, the code snippet gets 10 connections and waits for connection 11 which it can't get until the server time outs. That's what I meant with dead-lock: it waits for freeing a resource it occupies by itself.

Why is context a common resource? Each DBHandler instance has its own Context instance. The latter would be okay since multiple user can have their own context each (the third party library uses a system user to access the DB). And if each user/thread has its own Context instance, a NPE in clearContext shouldn't be possible.
I agree. Here's the link:
subject: Possible threading problem in web application
It's not a secret anymore!