Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ThreadLocal when using hibernate session/JDO persistenceManager

I trying to understand the best prastice of using ThreadLocal for the above questions. From my understanding the reason of using this is to ensure only one session/pm created for entire application. My question is

  1. is there any impact of using threadlocal like this on clustering application? (example google app engine) ?

  2. if u use "transactional" begin,commit on my application, i do not need to use threadlocal right? since "transaction" already ensure my session open and close properly?

  3. if i need to use "transactional", tx, it should be in threadlocal as well?

  4. why not just use "static" instead of "threadlocal" ?

i interested to hear feedback from you all regarding advantages/disadvantages of using this techinque?

like image 230
cometta Avatar asked Dec 20 '25 00:12

cometta


1 Answers

ThreadLocal is not used to create one session for the whole application. It is used to create one session for every thread. Every user session will be one thread so the ThreadLocal ensures that every user accessing you web page/ database will get its own database connection. If you use a static singleton pattern every user on the server will use the same database connection and I don't know how that would work out.

like image 150
Janusz Avatar answered Dec 21 '25 18:12

Janusz