Now, when I know how to use the Hibernate Second Level Cache, I am trying to figure out why or when I have to use it in my web application. When I retrieve an object from a database, it resides in memory until there is at least one active reference to it around. If I want it to remain in memory between requests (when all local references disappear), I can add it to the HttpSession instance. In this case the life span of an object in memory will be same as the lifespan of the session instance. If I prefer to use the Hibernate Second Level Cache I will not put an object into a session, but instead will place it into the cache. But I still need its Id to be able to find it in the cache. So instead of using httpsession.setAttribute(param_name,object)/httpsession.getAttribute(param_name) I will using httpsession.setAttribute(param_name,object_id)/httpsession.getAttribute(param_name) plus commands to store and retrieve the object from the cache. So what is the benefit? I see only two potential benefits: 1. The Hibernate Second Level Cache is more compact storage because it stores only parameters, but not a class as a whole. Does this overweight an addition complexity? 2.The Hibernate Second Level Cache allows to use a disk as a storage. Using a disk instead of memory slows down access. It probably still will be faster then just reading from a database, but much slower then access to an object in memory. So, what could be a compelling case for use the Hibernate Second Level Cache instead of HttpSession except for the case of extremely big objects or extremely big collections of objects? Lev Tannen
|