使用hibernate进行注册后在ssms中查询表一直显示正在查询中(net sqlclient data provider 执行超时已过期)

文章目录

    • 关联代码
      • HibernateUtil.java
      • BaseHibernateDao.java
      • CustomerDao.java
      • UserService.java
      • UserAction.java
    • 解决方法

关联代码

HibernateUtil.java

package cn.edu.zjut.dao;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {

	public HibernateUtil() {
		// TODO Auto-generated constructor stub
	}

	private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml"; 
	private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
	private static Configuration configuration = new Configuration(); 
	private static SessionFactory sessionFactory; 
	private static String configFile = CONFIG_FILE_LOCATION;
	
	static {
		try {
			configuration.configure(configFile);
			sessionFactory = configuration.buildSessionFactory();
		}catch(Exception e) {
			System.err .println("%%%%ErrorCreatingSessionFactory%%%%"); 
			e.printStackTrace();
		}
	}

	public static SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public static void rebuildSessionFactory() {
		try {
			configuration.configure(configFile);
			sessionFactory = configuration.buildSessionFactory();
		}catch(Exception e) {
			System.err .println("%%%%ErrorCreatingSessionFactory%%%%"); 
			e.printStackTrace();
		}
	}

	public static Configuration getConfiguration() {
		return configuration;
	}

	public static void setConfigFile(String configFile) {
		HibernateUtil.configFile = configFile;
		sessionFactory  = null;//这句干嘛的?
	}
	
	public static Session getSession() throws HibernateException {
		Session session = (Session) threadLocal.get(); 
		if (session == null || !session.isOpen()) { 
			if (sessionFactory == null) { 
				rebuildSessionFactory(); 
			} 
			session = (sessionFactory != null) ? sessionFactory.openSession(): null; 
			threadLocal.set(session); 
		} 
		return session;
	} 
	
	public static void closeSession() throws HibernateException { 
		Session session = (Session) threadLocal.get(); 
		threadLocal.set(null); 
		if (session != null) { 
			session.close(); 
		} 
	}

}

BaseHibernateDao.java

package cn.edu.zjut.dao;

import org.hibernate.Session;

public class BaseHibernateDao {

	public BaseHibernateDao() {
		// TODO Auto-generated constructor stub
	}

	public Session getSession() {
		return HibernateUtil.getSession();
	}
	/*
	public void closeSession() {
		HibernateUtil.closeSession();
	}
	 */
	
}

CustomerDao.java

package cn.edu.zjut.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import cn.edu.zjut.po.Customer;

public class CustomerDao extends BaseHibernateDao{

	public CustomerDao() {
		// TODO Auto-generated constructor stub
	}
	public List findByHql(String hql) { 
		
		try {
			String queryString = hql;
			Query queryObject = getSession().createQuery(queryString); 
			return queryObject.list();
		}catch (RuntimeException re) {
			//log.error("find by hql failed", re); 
			throw re;
		}finally {
			getSession().close();
		}
	}
	
	public void save(Customer customer) {
		
		try {
			getSession().save(customer);
			//getSession().flush();
		}catch(RuntimeException re) {
			throw re;
		}
	}


}

UserService.java

package cn.edu.zjut.service;

import java.util.List;
import java.util.Map;

import com.opensymphony.xwork2.ActionContext;

import cn.edu.zjut.dao.CustomerDao;
import cn.edu.zjut.po.Customer;

public class UserService {
	
	private Map<String, Object> request, session;

	public boolean login(Customer loginUser) {
		ActionContext ctx= ActionContext.getContext(); 
		session=(Map) ctx.getSession(); 
		request=(Map) ctx.get("request");
		String account = loginUser.getAccount();
		String password = loginUser.getPassword();
		String hql = "from Customer as user where account='" +account+ "' and password='" + password +"'";
		CustomerDao dao = new CustomerDao();
		List list = dao.findByHql(hql);
		//dao.getSession().close();
		if(list.isEmpty())
			return false;
		else {
			session.put("user", account); 
			request.put("tip", "登录成功!"); 
			loginUser=(Customer)list.get(0); 
			request.put("loginUser", loginUser);
			return true;
		}
	}

	public boolean register(Customer loginUser) {
		ActionContext ctx= ActionContext.getContext(); 
		session=(Map) ctx.getSession(); 
		request=(Map) ctx.get("request"); 
		CustomerDao dao = new CustomerDao(); 
		try {
			//dao.getSession();
			dao.save(loginUser);
			session.put("reguser", loginUser);
			return true;
		}catch(Exception e) {
			e.printStackTrace();
			return false;
		}finally {
			dao.getSession().close();
		}
		
	}
	
}

UserAction.java

package cn.edu.zjut.action;

import cn.edu.zjut.po.Customer;
import cn.edu.zjut.service.UserService;

public class UserAction {

	private Customer loginUser;

	public Customer getLoginUser() {
		return loginUser;
	}

	public void setLoginUser(Customer loginUser) {
		this.loginUser = loginUser;
	}
	
	public String login() {
		UserService userServ = new UserService();
		if(userServ.login(loginUser)) {
			return "loginSuccess";
		}
		return "loginFail";
	}
	
	public String reg() {
		UserService userServ = new UserService();
		if(userServ.register(loginUser)) {
			return "regSuccess";
		}
		return "regFail";
	}
}

解决方法

需要在service中使用事务,service的register方法:

public boolean register(Customer loginUser) {
		ActionContext ctx= ActionContext.getContext(); 
		session=(Map) ctx.getSession(); 
		request=(Map) ctx.get("request"); 
		CustomerDao dao = new CustomerDao(); 
		try {
			Transaction tran = dao.getSession().beginTransaction();
			//dao.getSession();
			dao.save(loginUser);
			tran.commit();
			session.put("reguser", loginUser);
			return true;
		}catch(Exception e) {
			e.printStackTrace();
			return false;
		}finally {
			dao.getSession().close();
		}	
	}

非常有意思的一点是,如果在这里不使用transaction,而加一句dao.getSession()——上面被注释掉的那句,那么数据库可以正常显示,但是注册的内容却并没有被写入数据库中。但是这里为什么还要再getSession?明明在save函数里面已经有过这句了呀。
另:在用dao.getSession()后,如果在CustomerDao中加入session.flush(),(被注释掉的那句),那么数据库又要查询超时无法正常显示了。而开了transaction之后是否有session.flush()都不会有影响。
另另:如果在.cfg.xml文件中加入true,那么不需要dao.getSession(),也不需要使用transaction,即可正常写入数据库,不过这时一定要在CustomerDao中加入session.flush()。

你可能感兴趣的:(ssh)