Spring管理Session何时打开何时关闭的呢?我在项目中直接super.getHibernateTemplate().find("");但是一直感觉到很奇怪,因为在用hibernate的时候都要打开session为什么用Spring的时候就不需要打开session呢?请高手指教,那么Session何时打开何时关闭的?
解决方案 »
- 程序员要写多少年代码才能写出房子 写出车子 写出gf
- 一个关于IO路径的疑问
- 向大家问一下为什么ajax xmlhttp不能有返回值那状态错误
- 第一个hibernate的例子就出错,why?
- 求 JAVA SWING Office2003LookAndFeel 皮肤源码
- 有状态会话BEAN连接数据库的问题--超级难道,有代码
- 面试常遇的问题!
- 关于JBuilder开发EntityBean的问题
- 给大家一个送分贴,关于配置sturts的问题
- springMVC 转 springboot 包含JSP 问题:linux环境启动第一次访问JSP很卡
- 敢问路在何方????
- 已经做好的项目,怎么导入MyEclipse
SessionFactory sf;
public HibernateTemplate getHibernateTemplate() {
//...
Configuration cfg = new Configuration().configure(...xml);
sf = cfg.buildSessionFactory();
}public List find(String hql) {
Session session = sf.openSession();
session....
session.close();
}
<bean id="myManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"><property name="sessionFactory" ref="sessionFactory"></property>
<tx:advice id="txAdvice" transaction-manager="myManager"> <tx:attributes>
<!--配置需要事务提交的方法-->
<tx:method name="find*" propagation="SUPPORTS"/><!--在查询方法执行完后事务不需进行提交-->
<tx:method name="add*" propagation="REQUIRED" rollback-for="Exception"/><!--在执行增加数据的时候需要提交,这时只有add方法执行完session才会关闭,事务就会提交-->
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut expression="execution(* .*.* (..))" id="pc"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="pc"/>
</aop:config>
<filter>
<filter-name>hibernateFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
<init-param>
<param-name>singleSession</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>hibernateFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
这样只有当当前请求(request)关闭,session才会相应关闭,LZ可以google这方面的资料