假设在你的应用中Hibernate是通过spring 来管理它的session.如果在你的应用中没有使用OpenSessionInViewFilter或者OpenSessionInViewInterceptor。session会在transaction结束后关闭。
如果你采用了spring的声明式事务模式,它会对你的被代理对象的每一个方法进行事务包装(AOP的方式)。如下:

<bean id="txProxyTemplate" abstract="true"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager" ref="transactionManager"/>
<property name="transactionAttributes">
<props>
<prop key="save*">PROPAGATION_REQUIRED</prop>
<prop key="remove*">PROPAGATION_REQUIRED</prop>
<prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>

<bean id="manager" parent="txProxyTemplate">
<property name="target">
<bean class="org.appfuse.service.impl.BaseManager">
<property name="dao" ref="dao"/>
</bean>
</property>
</bean>
目标类org.appfuse.service.impl.BaseManager 的 save *方法的事务类型PROPAGATION_REQUIRED ,remove* 方法的事务类型PROPAGATION_REQUIRED
其他的方法的事务类型是PROPAGATION_REQUIRED,readOnly。
所以给你的感觉是调用这个名为“manager”的bean的方法之后session就关掉了。
如果应用中使用了OpenSessionInViewFilter或者OpenSessionInViewInterceptor,所有打开的session会被保存在一个线程变量里。在线程退出前通过
OpenSessionInViewFilter或者OpenSessionInViewInterceptor断开这些session。 为什么这么做?这主要是为了实现Hibernate的延迟加载功能。基于一个请求
一个hibernate session的原则。

好了,上而是抄的。下面才是自己的:

 OpenSessionInViewFilter或者OpenSessionInViewInterceptro是spring为了解决hibernate解决Lazy的问题。

    也就是在延迟加载的时候,只要关闭了session就会出现,读不数据的解决方案, OpenSessionInViewFilter或者OpenSessionInViewInterceptro会在整个程序执行完毕才会关闭hibernate的Session,在这种情况 下,spring提供了这种解决方案。

但是这种解决方案是会出现问题,在应用了这个方案的时候,你会发现在点击页面的时候,多点几次,就会卡住了,永远也执行不过去了。这个时候最好的解决方案是关闭服务器,重新来过。但是服务器又是不可能关闭的。如果关闭了,这个应用也太差了。

  这种情况是因为session并没有关闭,导致线程阻塞,一直在控制台会出现open hibernate session.一直就卡在这边,在这种情况下只能重启服务器。当然 只要在dao里面加入getHibernateTemplate().initialize(getSession());把session初始化掉。

就OK了。

一些自己的见解 。第一次写,写得不好,希望原谅。