锁定老帖子 主题:执行几次查询之后就不能查询了,页面不动了
精华帖 (1) :: 良好帖 (0) :: 新手帖 (1) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2006-09-19
温柔一刀 写道 能说详细点么?我看了下源码,execute方法好象实现不了 getHibernateTemplate.execute( new HibernateCallBack () { public Object doInHibernate(Session session) throws HibernateException { // do something } } ); |
|
返回顶楼 | |
发表时间:2006-09-19
dada 写道 温柔一刀 写道 能说详细点么?我看了下源码,execute方法好象实现不了 getHibernateTemplate.execute( new HibernateCallBack () { public Object doInHibernate(Session session) throws HibernateException { // do something } } ); 非常感谢!修改如下: public List getEmployee_sortByPage(final Page page) throws HibernateException { return (List)getHibernateTemplate().execute( new HibernateCallback() { public Object doInHibernate(Session session) throws HibernateException { String querySentence = "FROM Employee_sort_info"; Query query = session.createQuery(querySentence); query.setFirstResult(page.getBeginIndex()).setMaxResults(page.getEveryPage()); return query.list(); } } ); } |
|
返回顶楼 | |
发表时间:2006-09-19
downpour 写道 都在搞什么搞啊,HibernateTemplate里面的getSession()方法是受到Spring管理的,拿到的是当前线程安全的Session,无需手动管理。你可以先把Spring和Hibernate的日志打成DEBUG,看一下Transaction和Session的日志。再不明白可以跟踪一下Spring的源码看一下。其实文档里面都有写。查阅一下吧。
根据你提供的情况,应该是你的Transaction的配置有问题,先修改Transaction的配置再试试。 温柔一刀提供的初始代码里面调用的 getSession() 方法是继承自 HibernateDaoSupport 的…… |
|
返回顶楼 | |
发表时间:2006-09-19
Allen 写道 downpour 写道 都在搞什么搞啊,HibernateTemplate里面的getSession()方法是受到Spring管理的,拿到的是当前线程安全的Session,无需手动管理。你可以先把Spring和Hibernate的日志打成DEBUG,看一下Transaction和Session的日志。再不明白可以跟踪一下Spring的源码看一下。其实文档里面都有写。查阅一下吧。
根据你提供的情况,应该是你的Transaction的配置有问题,先修改Transaction的配置再试试。 温柔一刀提供的初始代码里面调用的 getSession() 方法是继承自 HibernateDaoSupport 的…… 恩 |
|
返回顶楼 | |
发表时间:2006-09-19
问题已经解决,非常感谢各位。 温柔一刀 写道 每执行依次查询,控制台就有这样的警告
java代码如下: public class Employee_sortDAOImpl extends HibernateDaoSupport implements Employee_sortDAO { public int getEmployee_sortCount() throws HibernateException { String querySentence = "SELECT count(*) FROM Employee_sort_info"; List list = this.getHibernateTemplate().find(querySentence); Integer count = (Integer) list.get(0); return count; } public List getEmployee_sortByPage(Page page) throws HibernateException { String querySentence = "FROM Employee_sort_info"; Query query = getSession().createQuery(querySentence); query.setFirstResult(page.getBeginIndex()).setMaxResults(page.getEveryPage()); return query.list(); } } 警告: finalizing with closed connection 2006-9-18 14:38:26 org.hibernate.jdbc.ConnectionManager finalize 执行几次查询之后就不能查询了,页面不动了,是因为connection没有关闭吗? 问题已经解决了,谢谢哈 修改如下: public class Employee_sortDAOImpl extends HibernateDaoSupport implements Employee_sortDAO { public int getEmployee_sortCount() throws HibernateException { String querySentence = "SELECT count(*) FROM Employee_sort_info"; List list = this.getHibernateTemplate().find(querySentence); Integer count = (Integer) list.get(0); return count; } public List getEmployee_sortByPage(Page page) throws HibernateException { String querySentence = "FROM Employee_sort_info"; Session session=this.getHibernateTemplate().getSessionFactory() .openSession(); Query query = session.createQuery(querySentence); query.setFirstResult(page.getBeginIndex()).setMaxResults(page.getEveryPage()); List list = query.list(); session.close(); return list; } } 还有个问题:如果要实现如下功能,但是让它自己管理session,如何解决,我没有找到HibernateTemplate里面有类似setFirstResult和setMaxResults的方法啊 public List getEmployee_sortByPage(Page page) throws HibernateException { String querySentence = "FROM Employee_sort_info"; Query query = this.getHibernateTemplate().getSessionFactory().openSession().createQuery(querySentence); query.setFirstResult(page.getBeginIndex()).setMaxResults(page.getEveryPage()); return query.list(); } 上面的问题都已经解决,谢谢各位,代码如下: public List getEmployee_sortByPage(final Page page) throws HibernateException { return (List)getHibernateTemplate().execute( new HibernateCallback() { public Object doInHibernate(Session session) throws HibernateException { String querySentence = "FROM Employee_sort_info"; Query query = session.createQuery(querySentence); query.setFirstResult(page.getBeginIndex()).setMaxResults(page.getEveryPage()); return query.list(); } } ); } |
|
返回顶楼 | |
发表时间:2006-09-19
在记忆中的方法没这么复杂
我记得好像在service层 用spring进行控制session 提交还是回滚 (如果测试好似会不回滚用来测试) |
|
返回顶楼 | |
发表时间:2006-09-20
可能是由于你没有用OpenSessionInView模式吧。所以getSession()出来的Session是受事务管理,但是不被Spring控制,需要自己关闭。如果你用OpenSessionInView,那么Filter会帮助你关闭的。
|
|
返回顶楼 | |
发表时间:2006-09-20
查了一下源码。解释一下这个问题吧。
如果你使用OpenSessionInView模式,那么你在DAO中使用getSession()后,不用做任何处理。只要拿过来用就好了,因为此时,Spring拿到的是由OpenSessionInViewFilter打开的线程安全的Session对象。在Filter中最终会帮你管理这个对象。 引用 SessionFactory sessionFactory = lookupSessionFactory(request); Session session = null; boolean participate = false; if (isSingleSession()) { // single session mode if (TransactionSynchronizationManager.hasResource(sessionFactory)) { // Do not modify the Session: just set the participate flag. participate = true; } else { logger.debug("Opening single Hibernate Session in OpenSessionInViewFilter"); session = getSession(sessionFactory); TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(session)); } } else { // deferred close mode if (SessionFactoryUtils.isDeferredCloseActive(sessionFactory)) { // Do not modify deferred close: just set the participate flag. participate = true; } else { SessionFactoryUtils.initDeferredClose(sessionFactory); } } try { filterChain.doFilter(request, response); } finally { if (!participate) { if (isSingleSession()) { // single session mode TransactionSynchronizationManager.unbindResource(sessionFactory); logger.debug("Closing single Hibernate Session in OpenSessionInViewFilter"); try { closeSession(session, sessionFactory); } catch (RuntimeException ex) { logger.error("Unexpected exception on closing Hibernate Session", ex); } } else { // deferred close mode SessionFactoryUtils.processDeferredClose(sessionFactory); } } } 以上是OpenSessionInViewFilter中的代码,可以看到finally代码段中会调用closeSession()方法。 如果你没有使用OpenSessionInView模式,那么在HibernateDaoSupport中使用getSession()获得的Session对象是需要你自己去管理的。在HibernateDaoSupport中,有两个配对的方法,分别是getSession()和releaseSession(Session session)。所以还是可以不用使用难看的callback就解决问题的。在你query.list()之后,调用一句releaseSession(Session session)即可。 |
|
返回顶楼 | |
发表时间:2006-09-20
downpour 写道 查了一下源码。解释一下这个问题吧。
如果你没有使用OpenSessionInView模式,那么在HibernateDaoSupport中使用getSession()获得的Session对象是需要你自己去管理的。在HibernateDaoSupport中,有两个配对的方法,分别是getSession()和releaseSession(Session session)。所以还是可以不用使用难看的callback就解决问题的。在你query.list()之后,调用一句releaseSession(Session session)即可。 谢谢您 我没有用OpenSessionInView模式,releaseSession确实可以关闭session,但是不比close先进,还是要手动管理session。 还是加了OpenSessionInViewInterceptor方便啊,session可以不管了,也不用使用难看的callback了,非常感谢您啊 <!-- SimpleUrlHandlerMapping --> <bean id="simpleUrlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="interceptors" ref="openSessionInViewInterceptor"/> <property name="mappings"> <props> 。。。 </props> </property> </bean> <bean id="openSessionInViewInterceptor" class="org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor"> <property name="sessionFactory" ref="sessionFactory"/> </bean> |
|
返回顶楼 | |
发表时间:2006-09-20
Session session = getSession(); 这样取出来的session是自动关闭的,我做过测试的,不知道楼主是怎么搞的! 我看了你的配置,几乎和我的一样!我用的是Hibernate2 |
|
返回顶楼 | |