Ok here's what I've experienced yesterday:
My test (run by selenium) logs in to my application, and loads data from the DB, thus creating one hibernate Session. It then logs out. This is done once every 3 minutes.
The persistance beans are created by Spring as follows:
<bean scope="session"
id="persistanceService"
class="com.a.b.c.PersistanceServiceImpl"
p:entityManagerFactory-ref="entityManagerFactory" />
The entity manager factory is configured as follows:
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
p:dataSource-ref="dataSource" p:persistenceUnitName="jpa" >
<property name="jpaVendorAdapter">
<bean
class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"
p:database="ORACLE"
p:databasePlatform="org.hibernate.dialect.Oracle9iDialect"
p:showSql="false" />
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">validate</prop>
</props>
</property>
</bean>
I ran the test and created a thread dump every 30 minutes, that is every 10 login/logout.
If the listener is not set, I can see that session instances add up each time I login and out. The session-timeout is set to 30 minutes and I can see in the logs that the session is invalidated, so I expected the number of sessions to decrease after that time, but it still increases. The number of sessions was 30 after 1h30 minutes and kept going up afterwards.
If the listener is set, then session instances count stays low. Actually, it says at one because once the test logs out, the http session invalidation actually invalidates the hibernate session and everything is GC'ed. It has been like this for the last 15 hours.
Given all that, I think that missing this listener might well lead to memory leaks...
Bozho: what do you think of this? By the way, I'm not an experienced SO user: should I have posted this under my question? I thought that I was answering my own question here, so I added an answer, but not sure :-)