用hibernate进行查询,刷新5,到6次action后,页面就卡死了,神马原因?是因为每次都openSession的原因么?还是因为。。。??没关闭session?我用session.getTransaction().commit();提交事务

巴顿 发布于 2013/01/01 13:28
阅读 3K+
收藏 0

用hibernate进行查询,刷新5,到6次action后,不停的执行了5到6次查询页面就卡死了,页面就卡死了,神马原因?是因为每次都openSession的原因么?还是因为。。。??没关闭session?我用session.getTransaction().commit();提交事务

查询方法在下面

/**
	 * 将所有的座位记录都返回去
	 */
	@SuppressWarnings("unchecked")
	public List<Seat> fingAll() {
		session = HibernateServiceProvider.getSession();
		Transaction tx = null;
		tx = session.beginTransaction();
		queryString = "from Seat";
		List <Seat> seats = session.createQuery(queryString).list();
		session.getTransaction().commit();
		return seats;
	}

拿到session的方法如下


HibernateServiceProvider
package com.gdut.librarySeatingManagementSystem.util;

import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

/**
 * session提供者
 * @author ken
 *
 */
public class HibernateServiceProvider {
	
	private static final Logger log = Logger.getLogger(HibernateServiceProvider.class);
	
	private static SessionFactory sessionFactory;
	
	/*万一spring中注入sessionFactory失败,再次通过hibernate.cfg.xml生成sessionFactory实例*/
	public static void initHibernate(){
		if(getSessionFactory() == null){
			Configuration conf = new Configuration();//new Configuration()里面已经包括了new AnnotationCongfiguration方法
			sessionFactory  = conf.configure().buildSessionFactory();
			log.info("Hibernate initialized!");
		}
	}
	
	private static SessionFactory getSessionFactory(){
		return sessionFactory;
	}
	
	public static Session getSession(){
		try{
			initHibernate();
			System.out.println("\n\n\n\n111"+sessionFactory+"\n\n\n\n\n\n");
			return getSessionFactory().getCurrentSession();
		}catch(HibernateException e){
			System.out.println("\n\n\n\n222"+sessionFactory+"\n\n\n\n\n\n");
			return createNewSession();
		}
	}
	
	public static void setSessionFactory(SessionFactory sessionFactory) {
		System.out.println("\n\n\n\n333"+sessionFactory+"\n\n\n\n\n\n");
		HibernateServiceProvider.sessionFactory = sessionFactory;
	}

	public static Session createNewSession(){
		System.out.println("\n\n\n\n444"+sessionFactory+"\n\n\n\n\n\n");
		return getSessionFactory().openSession();
	}
}

刷新了8次页面后,就卡住了

如图

一直在等待,以前遇过这样的问题的,现在忘了神马原因了。

后台执行如下


Hibernate: select seat0_.id as id1_, seat0_.number as number1_ from seats seat0_
2013-01-01 13:28:59,038 DEBUG [CommonsLogger.java:68] : Forwarding to location /page/seat/seat-list.jsp
2013-01-01 13:29:00,422 DEBUG [CommonsLogger.java:68] : Bypassing /seat/seat-list
2013-01-01 13:29:00,423 DEBUG [CommonsLogger.java:68] : Validating /seat/seat-list with method execute.




111org.hibernate.impl.SessionFactoryImpl@14f025b










222org.hibernate.impl.SessionFactoryImpl@14f025b










444org.hibernate.impl.SessionFactoryImpl@14f025b


看到上面的打印可以知道

每次都执行了

createNewSession()
所以每次都打开了新的session,这样正常的么?好像没有一次执行到

return getSessionFactory().getCurrentSession();
会不会就是这里的原因?
怎么才能拿到当前的session呢?


加载中
0
巴顿
巴顿
楼主自己搞定了

就加了这一行

<prop key="hibernate.current_session_context_class">thread</prop>

然后后台打印如下

2013-01-01 13:39:03,932 DEBUG [CommonsLogger.java:68] : Forwarding to location /                                                                             page/seat/seat-list.jsp
2013-01-01 13:39:05,203 DEBUG [CommonsLogger.java:68] : Bypassing /seat/seat-lis                                                                             t
2013-01-01 13:39:05,205 DEBUG [CommonsLogger.java:68] : Validating /seat/seat-li                                                                             st with method execute.




111org.hibernate.impl.SessionFactoryImpl@a3cf94






Hibernate: select seat0_.id as id1_, seat0_.number as number1_ from seats seat0

可以看到相比较之前少了

222org.hibernate.impl.SessionFactoryImpl@14f025b
444org.hibernate.impl.SessionFactoryImpl@14f025b
所以 returngetSessionFactory().getCurrentSession();是正确执行了,而且不用再去打开新的session
0
0
09网络

你可以参考一下这个,我试了还是挺管用的:参考资料

返回顶部
顶部