1. SessionFactory
从SessionFactory中获取session后需要手动关闭session,显示调用session.close方法;
或者交给spring维护,详细配置如下:
在 web.xml 文件里面加上下面的配置信息
<!-- 通过sessionFactory getsession方法获取的session关闭策略交给spring维护 -->
<filter >
<filter-name> osivFilter</filter-name >
<filter-class> org.springframework.orm.hibernate3.support.OpenSessionInViewFilter </filter-class>
</filter >
<filter-mapping >
<filter-name >osivFilter </filter-name>
<url-pattern >/*</url-pattern>
</filter-mapping >
2.HibernateTemplate
给HibernateTemplate注入SessionFactory,从而使用相关的方法;此时session的关闭交给template管理
3.HibernateDaoSupport
对HibernateTemplate进行的封装
4.
1)Hibernate中
getCurrentSession和openSession区别:
getCurrentSession会把Session和当前的线程关联起来,而openSession只是重新开启一个Session;
2)getCurrentSession创建的线程会在事务回滚或事物提交后自动关闭,而openSession必须手动关闭
所以如果需要手动处理事务就需要使用
openSession,否则会报:
Transaction not successfully started