This week's book giveaway is in the Beginning Java forum.
We're giving away four copies of Learn Java with Math: Using Fun Projects and Games and have Ron Dai on-line!
See this thread for details.
Win a copy of Learn Java with Math: Using Fun Projects and Games this week in the Beginning Java forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Junilu Lacar
  • Martin Vashko
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Scott Selikoff
  • salvin francis
  • Piet Souris

hibernate in servlet - threadsafe

 
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
 
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.

 
ranger
Posts: 17344
11
Mac IntelliJ IDE 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
 
Those cherries would go best on cherry cheesecake. Don't put those cherries on this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!