I'm not sure what kind of problem (caused by laziness) you are hinting at, but for me the biggest pain is to avoid losing the session context in my own application caches. Typical case:
- the
foo object is loaded and placed on the map; - another thread takes this object from the map and calls
foo.getBar() (something that has never been called before and is lazily evaluated); - boom!
So, to solve this problem, we have a number of rules:
- end sessions as transparent as possible (for example,
OpenSessionInViewFilter for webapps); - have a common API for threads / thread pools, where db session bind / unbind runs somewhere high in the hierarchy (wrapped in
try/finally ), so subclasses don't need to think about it; - when transferring objects between threads, skip identifiers instead of the objects themselves. The receiving stream can load an object if it needs to;
- when caching objects, it never caches objects, but their identifiers. Have an abstract method in your DAO or manager class to load an object from the second level Hibernate cache when you know the identifier. The cost of retrieving objects from the second-level Hibernate cache is still much cheaper than moving to DB.
This, as you can see, is really nowhere near non-invasive and transparent. But the cost is still bearable to compare with the price I would have to pay for impatient download. The problem with the latter is that sometimes this leads to a butterfly effect when loading a single link object, not to mention a collection of objects. Memory consumption, CPU usage, and latency, to mention the least, are also much worse, so I guess I can live with it.
mindas Feb 17 2018-11-17T00: 00Z
source share