When does Spring close the SessionFactory class that is created by the Spring Container (the SessionFactory is defined in the container) ?
It is done automatically when you shut down the ApplicationContext
(undeploy the web application, gracefully close the JVM, etc.) When the container is shut down it calls destroy()
method from DisposableBean
interface on all beans. One of such methods is defined in LocalSessionFactoryBean
:
public void destroy() {
this.sessionFactory.close();
}
You don't have to bother about this at all.
Hibernate maintains primary cache by default. Is the scope of cache limited to a session or the sessionFactory? I mean, when is the memory for primary cache released?
The "primary" cache is called first level cache and is tied to a session. The second level cache is global to SessionFactory
, however it is not released when SessionFactory
is closed because most likely you are using some external cache provider like ehcache. If this is the case, EhCache must also be closed to release that memory. But this should also be handled by Spring container if EhCache support is used.
See also