Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I am not sure which problem (caused by lazyness) you're hinting to, but for me the biggest pain is to avoid losing session context in my own application caches. Typical case:</p> <ul> <li>object <code>foo</code> is loaded and put into a map;</li> <li>another thread takes this object from the map and calls <code>foo.getBar()</code> (something that was never called before and is lazy evaluated);</li> <li>boom!</li> </ul> <p>So, to address this we have a number of rules:</p> <ul> <li>wrap sessions as transparently as possible (e.g. <code>OpenSessionInViewFilter</code> for webapps);</li> <li>have common API for threads/thread pools where db session bind/unbind is done somewhere high in the hierarchy (wrapped in <code>try/finally</code>) so subclasses don't have to think about it;</li> <li>when passing objects between threads, pass IDs instead of objects themselves. Receiving thread can load object if it needs to;</li> <li>when caching objects, never cache objects but their ids. Have an abstract method in your DAO or manager class to load the object from 2nd level Hibernate cache when you know the ID. The cost of retrieving objects from 2nd level Hibernate cache is still far cheaper than going to DB.</li> </ul> <p>This, as you can see, is indeed nowhere close to <em>non-invasive and transparent</em>. But the cost is still bearable, to compare with the price I'd have to pay for eager loading. The problem with latter is that sometimes it leads to the butterfly effect when loading single referenced object, let alone a collection of entities. Memory consumption, CPU usage and latency to mention the least are also far worse, so I guess I can live with it.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload