Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

JavaWorld Daily Brew

Help with ThreadLocal



Hi Expert programmers please help me out with a ThreadLocal Query.

private static final ThreadLocal cache = new ThreadLocal() {
protected ConnectioninitialValue() {
return ConnectionPool.getConnection()
}
};

Calling Get on the cache returns a cached connection per thread. If the call is for the first time from same thread it returns same connection. Sweet.

Issue 1
When the client gets the connection it cannot close it (If the client close the connection all the subsequent get will return the closed Connection).

Issue 2
If the connection gets timed out the subsequent get will return closed connection.

Issue 3
If you get teh connection don't close it. How to you gracefully close the connection in the above implementation upon garbage collection when Thread goes out of scope.

Any takers.