• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

hibernate in servlet - threadsafe

 
Amy Saari
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All

We're really having a hard time trying to figure out if our code is really threadsafe. We have a web app that contains spring-injected services that use a dao layer to get to the database via Hibernate. We injected the services into INSTANCE variables in the servlet - so every request that comes in becomes a thread that uses those same service objects. The service object contains a dao object that is used to access the database via hibernate (oracle in this case).

So, for example, the contactService is an instance variable in the this servlet



this call is made from the servlet to the service



in the ContactService class we use a dao class:



and make this call:



in the ContactDao class is the method:



From what I understand the org.hibernate.Query is not threadsafe. So is it possible our code is not threadsafe because each thread started by the servlet container is using the same service and dao objects?

Thanks for any help.
Amy
 
Salil Vverma
Ranch Hand
Posts: 257
Hibernate Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Amy,

Servlet's instance variables are not thread safe. so the following variable might have thread safety problem.



org.hibernate.Query is not thread safe but it would not be a problem in your code, reason being you are creating hqlQuery local object rather than an instace object. Each thread (each execution) using findSupervisorContactList function would have its own hqlQuery object in the stack.

 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The architecture you described IS thread safe.

The service class is not state, but a service class. In Spring it is instantiated just once and can receive calls from many threads simultaneously.

The architecture you described is exactly how you want it set up.

Mark
 
Amy Saari
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you both very much

Amy
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic