我做了一个用java开发的网站,但是使用中出现人多于20来个时就不能再登录进系统的问题。本人经验不足,确定不了问题出在哪。后来检查代码发现,登录时到数据库取出用户名和密码的那个函数是这样的:
public People PeopleLogin(String loginGonghao, String loginPwd)
throws Exception {
String hql = "from People where gonghao=:loginGonghao and password=:loginPwd";
People people=null;
Transaction tx=null;
try{
//取得一个Hibernate的会话实例
Session session = MySessionFactory.getSession();
//开始Hibernate的事务管理
tx = session.beginTransaction();
//在当前会话上建立一个查询
Query query = session.createQuery(hql);
//设置其中的变量
query.setString("loginGonghao", loginGonghao);
query.setString("loginPwd", loginPwd);
//得到查询结果集
query.setMaxResults(1);
//得到查询记录条数
people = (People)query.uniqueResult();
//提交事务
tx.commit();
logger.info("在执行PeopleServiceImpl类中的PeopleLogin方法时成功:\n");
}catch(Exception e){
if(tx!=null)tx.rollback();
logger.info("在执行PeopleServiceImpl类中的PeopleLogin方法时出错:\n");
e.printStackTrace();
}
return people;
}
而其他访问过数据库的代码在最后有一个finally: finally{
//关闭当前会话
MySessionFactory.closeSession();
}
请问是不是这里没有这段代码,导致没有关闭连接,才产生的问题?
public People PeopleLogin(String loginGonghao, String loginPwd)
throws Exception {
String hql = "from People where gonghao=:loginGonghao and password=:loginPwd";
People people=null;
Transaction tx=null;
try{
//取得一个Hibernate的会话实例
Session session = MySessionFactory.getSession();
//开始Hibernate的事务管理
tx = session.beginTransaction();
//在当前会话上建立一个查询
Query query = session.createQuery(hql);
//设置其中的变量
query.setString("loginGonghao", loginGonghao);
query.setString("loginPwd", loginPwd);
//得到查询结果集
query.setMaxResults(1);
//得到查询记录条数
people = (People)query.uniqueResult();
//提交事务
tx.commit();
logger.info("在执行PeopleServiceImpl类中的PeopleLogin方法时成功:\n");
}catch(Exception e){
if(tx!=null)tx.rollback();
logger.info("在执行PeopleServiceImpl类中的PeopleLogin方法时出错:\n");
e.printStackTrace();
}
return people;
}
而其他访问过数据库的代码在最后有一个finally: finally{
//关闭当前会话
MySessionFactory.closeSession();
}
请问是不是这里没有这段代码,导致没有关闭连接,才产生的问题?
throws Exception {
String hql = "from People where gonghao=:loginGonghao and password=:loginPwd";
People people=null;
Transaction tx=null;
try{ //取得一个Hibernate的会话实例
Session session = MySessionFactory.getSession();
//开始Hibernate的事务管理
tx = session.beginTransaction();
//在当前会话上建立一个查询
Query query = session.createQuery(hql);
//设置其中的变量
query.setString("loginGonghao", loginGonghao);
query.setString("loginPwd", loginPwd);
//得到查询结果集
query.setMaxResults(1);
//得到查询记录条数
people = (People)query.uniqueResult();
//提交事务
tx.commit();
return people;
logger.info("在执行PeopleServiceImpl类中的PeopleLogin方法时成功:\n");
}catch(Exception e){
if(tx!=null)tx.rollback();
logger.info("在执行PeopleServiceImpl类中的PeopleLogin方法时出错:\n");
e.printStackTrace();
}
而其他访问过数据库的代码在最后有一个finally:
finally{
//关闭当前会话
MySessionFactory.closeSession();
}