0

I'm currently using Spring and Hibernate. At the moment, if I make a create object call (for example) from the client a request comes in on the service stub on the serverside. The service call has to create a new hibernate session factory, get the session, and then make the transaction. The problem is that this occurs every single time, so the session factory needs to be recreated to be used. This seems to be extremely wasteful and performance impacting since creating that factory takes a toll.

What I would like to do is reuse that one session factory, for example, across different service calls made by the client or multiple clients. The problem is I don't know how to do that since the entry point to the serverside functionality is the service call. I know that I would have to save state on the serverside somehow so that different calls could access the same session factory. I know of the scalability issues with keeping state and such, but there has to be a way to reuse previously created objects.

My question is how would I do this with Spring (am I supposed to use Session beans or HttpSession)? Is it possible for the container to set these things up on startup or does it have to wait for a service request to come in?

I'm for the most part a Spring newb, is it just that I don't understand the web service role?

Thanks in advance.

nightsRey
  • 124
  • 8

1 Answers1

0
  1. Yours is typical MVC scenario which is achieved by GWT+MVP. Based on your description seems you are creating the session-factory on every call which is obviously not a standard practice.
  2. Session-factory is created only once and every request executes in a different session created by the session-factory.
  3. With Spring, typical approach would be to configure the session-factory with spring wiring and hibernate. This config will be loaded only once when application starts up.
  4. On every service request, get the reference of session-factory from the bean-container (instead of creating it every time) and create session from it for DB operation.
  5. Check out this project which uses GWT+MVP+Spring+MyBatis. I understand that you use Hibernate instead of MyBatis but this would server as reference for this type of project.
Santosh
  • 17,667
  • 4
  • 54
  • 79
  • Hi Santosh, thanks I think I almost found what I was looking for thanks to your direction. There's just a couple of things I need to wrap my head around. Basically I have to define my spring context as a servlet on its own in order for the config to be loaded on startup right? Is this done in the web.xml with the listener and servlet tags? I guess I still don't see who's actually loading the config in order to declare the beans and such. – nightsRey Sep 04 '12 at 17:13
  • Yes. Its done in `web.xml`. Check [this link](http://stackoverflow.com/questions/6451377/loading-context-in-spring-using-web-xml) – Santosh Sep 05 '12 at 06:31
  • Hi Santosh, thank you, I'm still not sure how to retrieve the servlet to get the servlet context argument for the web application context in the link you gave but I guess I'll have to do some more research. – nightsRey Sep 05 '12 at 23:44
  • You can always retrieve `ServletContext` from request object as `HttpServletRequest.getServletContext();` – Santosh Sep 06 '12 at 08:02
  • Hi Santosh, I think I've finally got it. I have the applicationContext.xml being loaded in the web.xml by the contextLoaderListener. The applicationContext.xml specifies the hibernate SessionFactory bean, and the session factory is then injected into the DAOs that need it. I can also retrieve the servlet context from request factory when needed. Thanks ago for the help – nightsRey Sep 06 '12 at 16:37