简体   繁体   中英

Listening for Hibernate Session creation

I have a bug in my code that is causing a hibernate exception where an object is being associated with two open sessions when calling session.lock() .

I am using Spring 3.0.4 with HibernateTemplate and Hibernate 3.6.0.

I would like to be able to trap when a Session is created, create/store a stacktrace for the Session and then proxy the Session so when close() is called the the stacktrace is cleaned up.

I have found this question but this is specifically for web-apps. I've poked through the hibernate event/Session factory stats code and spring code but can't see an appropriate hook point.

Does anyone have any suggestions to do this?

This question has been posted on this forum, with a different environment (spring/grails/hibernate), but the answer could help you.

Basically, proxy the session factory and intercept calls to getCurrentSession.

See details here .

If the purpose is to debug / troubleshoot the problem you are facing - enabling debug logging for spring hibernate is a simpler / better option. You will be able to trace where the second session is opened.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM