我使用Criteria 查询数据库表时,当客户端调用到第3次就出现问题 org.hibernate.HibernateException: Not able to obtain connection at org.hibernate.jdbc.ConnectionManager.getConnection(ConnectionManager.java:113) at org.hibernate.jdbc.AbstractBatcher.prepareQueryStatement(AbstractBatcher.java:88) at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1162) at org.hibernate.loader.Loader.doQuery(Loader.java:390) at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:218) at org.hibernate.loader.Loader.doList(Loader.java:1593) at org.hibernate.loader.Loader.list(Loader.java:1577) 以下是实现的代码 public List executequery(Session session, Object obj,int type,int initialize) throws Exception {
if(obj==null){ throw new Exception("对像没有创建"); } try{ DetachedCriteria cr=DetachedCriteria.forClass(Tuser.class); Criteria crt=null; List list=null; Tuser usert=(Tuser)obj;
switch(type){ case 0: cr.add(Example.create(usert));
break; case 1: if(usert.getUsername()!=null && !usert.getUsername().equals("")) cr.add(Expression.like("username","'%"+usert.getUsername()+"%'")); if(usert.getOthername()!=null && !usert.getOthername().equals("")) cr.add(Expression.like("othername","'%"+usert.getOthername()+"%'")); if(usert.getEmail()!=null && !usert.getEmail().equals("")) cr.add(Expression.like("email","'%"+usert.getEmail()+"%'")); if(usert.getState()!=null && !usert.getState().equals("")) cr.add(Expression.like("state","'%"+usert.getState()+"%'")); // crt=cr.getExecutableCriteria(session); break; } crt=cr.getExecutableCriteria(session); list=crt.list(); initialize(list);
return list; }catch(Exception ex){ System.out.println("ryhyjutyjyj"); ex.printStackTrace(); } return null; } 这是调用上面方法的代码 public Object executequery(CommandDAO cmd,Object obj,int type,int initialize){ Session session=null;
try{ try{ session=(Session) HibernateFactory.currentSession(); }catch(Exception ex){ System.out.println("wwwwwwwwwwwww"); ex.printStackTrace(); } Object objy=cmd.executequery(session,obj,type,initialize);
if(session!=null){ session.close(); } return objy;
}catch(Exception ex){
if(session!=null){ session.close(); }
} finally{ if(session!=null){ session.close(); } } return null; } 以下是SERVLET凋用 public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html"); //PrintWriter out = response.getOutputStream(); DataOutputStream out=new DataOutputStream(response.getOutputStream()); DataInputStream ino=new DataInputStream(request.getInputStream()); if(ino!=null ){ int msgtype=ino.readInt(); switch(msgtype){ case MsgType.MSG_TYPE_LOGIN: GameUsermolile login=new GameUsermolile(); Tuser user1=login.login(out,ino); Hashtable hb=(Hashtable)this.getServl etContext().getAttribute("userlist"); if(hb==null){ hb=new Hashtable();} hb.put(user1.getUserid(),user1); this.getServletContext().setAttribute("userlist",hb);
request.getSession().setAttribute(user1.getUserid().toString(),user1); break; case MsgType.MSG_TYPE_REGISTER:
Tuser user=new Tuser(); TuserDAOmolile userdao=new TuserDAOmolile(); user.setUsername(ino.readUTF()); user.setPassword(ino.readUTF()); user.setOthername(ino.readUTF()); user.setEmail(ino.readUTF()); user.setState("0"); try{ GameCommandDAO comdao=new GameCommandDAO(); userdao.setUser(user); Tuser usert=(Tuser)comdao.executesava(userdao); System.out.println(usert.getOthername()); //userdao.sava(user);
out.writeInt(usert.getUserid().intValue());}catch(Exception ex){ ex.printStackTrace(); }
break;
}
}else{ System.out.println("rerererererererere"); }
out.close();
} |