• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread local and Singleton class for database connection

 
Tuan La
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello
i have a singleton class to get connection from pool and a class using thread local to get connection from singleton class
when i need to use connection, i check this

if i'm not closing this connection after use, does it return to pool ?
and what if i call DBSession.closeSession()
Please guide me.
Here my class
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35279
384
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What library is DBSession from? Regardless, you do have to close a connection for it to return to the pool on a timely basis. If you don't, it may get returned eventually; but too late to avoid a connection leak.
 
Tuan La
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is not actually my code, i got from a webapp (Servlet - tomcat),
in this app when they want a connection they are just call DBSession.currentConnection() and after use they did not close it
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35279
384
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tuan La wrote:in this app when they want a connection they are just call DBSession.currentConnection() and after use they did not close it

Which is wrong. The only possibility is that they have a filter that takes care of closing it at the end of the session.
 
Tuan La
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote:
Tuan La wrote:in this app when they want a connection they are just call DBSession.currentConnection() and after use they did not close it

Which is wrong. The only possibility is that they have a filter that takes care of closing it at the end of the session.


thank you miss , can you explain that code please.
i dont understand why did they use singleton class and ThreadLocal to work with connection ?
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35279
384
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tuan,
First of all, I'm female. So let's not call me "sir."

If you read this, you'll see JPA has a pattern using ThreadLocal. The idea is to keep an open database session for the http request.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic