We have a web app built with spring and hibernate that we are currently testing and profiling we found out that the hibernate session(managed by spring) isn't being closed after the request is over, thus causing very very serious performance issues, i was wondering if any of you could help me out figuring a way to somehow verify that the hibernate session is being handled correctly by spring or how could i implement some form of control over the hibernate session through spring?
Ah my mistake...I read the code too quickly. You are using a Template, so yes Spring should be doing all the Session handling.
Joined: Jun 19, 2001
@Paul: there is the openSessionInView interceptor that should open/close the session. at least i think so. this is of course tricky to test in a non-web environment.
@Roger: - I would sugest you use a TransactionProxyFactoryBean instead of starting ending the TX manualy. less error prone. - then i see some catch statements without logging! and you never rollback TX in case of exceptions. - also i would check out the API of the HibernateTemplate it offers methods like delete()/save)=/saveOrUpdate() and so on... - your findById method looks weird to me! why do you use SQL queries ? this is exactly what hibernate should do for you! why do you create an empty ArrayList() and then "throw it away" ? if you got the class and the ID, then just do something like this:
or use one of the HibernateTemplate methods.... or use HQL but not SQL. - better switch autocommit on your connection to false... just to be sure (though i think hibernate will complain if connection is in autocommit mode. not sure)
Joined: May 08, 2006
Thank you both Paul and pascal.
Now, I have to say this is a bit confusing to me since i am a newbie with ORM hibernate and spring, so please pardon my ignorance in case i say something stupid.
if spring is handling transactions for me how would i implement a TransactionProxyFactoryBean? The code i posted isn't mine, but you are right, we shouldn't use sql but the thing is the whole app has sql querys everywhere everyone here is a newbie with both hibernate and spring and i guess there wasn't much time to learn the stuff...
a TransactionProxy is an Object wrapping a "target Object", implementing the same interface as the target. when you call method x() on the proxy, it will open a session/start a TX or reuse an existing TX (as required, you can configure this), forward the call to the target's method x(). it close the session/commit the TX before returning the return value (if method is not void). it will also handle commit/rollback of TX.