• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

hibernate in servlet - threadsafe

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 257
Hibernate Oracle Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you both very much

Amy
 
reply
    Bookmark Topic Watch Topic
  • New Topic