I'm fairly new to Hibernate and have just written a class DataFetcher that manages all the data fetching from the DB using Hibernate. All this fetching happens from web service methods.
I get a feeling I have done something wrong because the production server crashes on OoM Error every 5 days or so.
The service I host has 4 endpoint methods, say processDataOne(), processDataTwo() and so on.
In DataFetcher I have the following:
I have methods createSessionFactory() and destroySessionFactory():
createSessionFactory() is called at the very beginning of a service call (processDataOne()). And the destroySessionFactory() is called at the very end.
I then have methods to fetch data, fetchABC(), fetchDEF(), ... fetchXYZ()
1. I read about how hibernate implements cacheing in sessions, so now I would be modifying the above to use the same open session in multiple fetch<Data>() methods and then closing this session just before closing the session factory. Is this alright?
2. Should the sessionFactory be singleton? I also need to ensure when 1000 users are using the web service methods, this does not cause problems.
And if the sessionFactory is a singleton and inherently static, is there a way I can use the same sessionFactory between processDataOne() and processDataTwo() which are 2 different service methods.
Please suggest a good way to build this DataFetcher class and describe how to use it's methods.