Warnings when objects are not lazy loading into sleep mode

I was wondering if there is a way to tell Hibernate to generate some kind of console warning when there are too many objects of a certain type in the session cache. I would like to do this for load testing, since we have problems with OutOfMemoryException when loading a BLOB from Oracle.

We are still using Hibernate 3.6.10 . Our best approach to this testing right now is to simply generate more data than the system could handle in the normal use case and try to load the parent object and see if it works. Doing so just feels bad.

Any suggestions are welcome.

One note that I forgot to mention is that this idea of โ€‹โ€‹โ€œregisteringโ€ is something that I would like to leave in working code to identify specific problems.

- EDIT -

Here is an example of what I'm trying to do:

Say I have an @Entity ClassX that has a lazy loaded list of @Entity ClassY objects. Some, like, I would like the log message to spit out when 100 or more ClassY instances are loaded into the session cache. Thus, during development, I can load the ClassX object and notice that if I (or another developer on the team) get access to this list when I shouldn't be.

+4
source share
1 answer

You can attach an Interceptor to listen for object loading events, maintain a quantity for each unique entity type, and register a warning whenever it passes a certain threshold. The documentation shows how to identify an interceptor associated with a session by passing it during creation:

 Session session = sf.openSession( new AuditInterceptor() ); 

Most likely you are not creating your session manually so that this does not help, but perhaps the way you are declaring your session has some way to pass an Interceptor through.

It is easier to declare a SessionFactory interceptor, but it does not seem to give you any reference to the session that the object is being created inside, otherwise you can knock down some kind of counter in WeakHashMap (with Session as the key so that you do not leak memory). If you use the default local session strategy, you can always set sessionFactory.getCurrentSession() .

+1
source

All Articles