2

I have two questions:

  • When does Spring close the SessionFactory class that is created by the Spring Container (the SessionFactory is defined in the container) ?
  • 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?
Leo
  • 187
  • 6
  • 19

1 Answers1

1

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 . 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

Community
  • 1
  • 1
Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674
  • Thanks Tomasz. For(1), isn't the container shut down when it goes out of scope (i.e handler to the container going out of scope)? For(2), I was getting memory leak and found that the cause was Hibernate's primary cache (no secondary cache was used). Although the Spring container went out of scope and sessions got closed (checked using Statistics class), the caches didnt get released. They did get released, however, when I explicitly called sessionFactory.close() just before the container went out of scope. – Leo May 28 '12 at 15:17
  • @Leo: by *container* do you mean Springs' `ApplicationContext` on `BeanFactory`? The only safe way to close it is to call `.close()` directly. It is highly improbable you are having a leak in first level cache. You are either not closing the session (*not* `SessionFactory`) or you are storing too many objects there (see: stateless sessions in Hibernate). – Tomasz Nurkiewicz May 28 '12 at 15:29
  • Its `BeanFactory`. Regarding closing the session, I am using `HibernateTemplate`. So ideally, that should handle all session management for me (which I allowed it to do). Regarding too many objects in cache, the memory leak persisted despite using `getHibernateTemplate.clear()`. And heap dump analysis shows the cache being full with Hibernate generated queries, Hibernate properties and so on(but not objects). – Leo May 28 '12 at 15:42
  • @Leo: interesting. Do you remember which class(es) from Hibernate was holding all these references? Ideally maybe you can make a screenshot of reference tree? – Tomasz Nurkiewicz May 28 '12 at 15:49
  • 1
    `org.hibernate.impl.SessionFactoryObjectFactory` is the class holding a `HashMap`, which in turn holds lots of `char[]`, and these char[] occupy most of the memory. The reference tree looks like: `ContextClassLoader->VectorObj->SessionFactoryObjectFactory->FastHashMap->HashMap` – Leo May 28 '12 at 15:58
  • In above, further, each HashMap has number of HashMapEntry wherein each entry primarily has SessionFactoryImpl obj. This implies that although the XMLBeanFactory goes out of scope, the SessionFactoryImpl objects it creates remain in memory and moreover, continue to occupy resources they use. This proves that `destroy()-> sessionFactory.close()` is not getting called by the container for some reason. It may be due to Spring container itself not being shut down until JVM is closed or it may indicate a leak in Spring (because user defined beans get garbage collected after some time)! – Leo May 29 '12 at 12:43