Win a copy of Java Mock Exams (software) this week in the Programmer Certification (OCPJP) forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Hibernate issue with `SessionFactory`

 
Yohan Weerasinghe
Ranch Hand
Posts: 507
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all please note I have posted the below question in Stackoverflow and I have one answer there, but I am not sure about it because it seems like the member who answered also not much sure about his answer. Below is my question.

I am having a JSP, Servlet (Pure JSP, Servlet) application where it uses `Hibernate`. Below is a `Hibernate` implementation class for a one table.

DesignationImpl.java




Below is the service class which calls to the above class.

DesignationService .java




And the servlets call them like below.



As you can see, there is a bad thing happening there, that is, the servlets are creating new `SessionFactory` instances every time it executes.I am having `Driver#Connect` issues and I guess this might be the reason for it.

I read stackoverflow posts and some seems to be suggesting the use of only one `SessionFactory` for the entire application. If it is suitable, then how can I do it? Maybe make a singleton class like below and use it in my implementation classes?



But then what about threads? Servlets are multi threaded isn't it?
 
What are you doing? You are supposed to be reading this tiny ad!
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!