求助Hibernate问题

J2EE 专栏收录该内容
1 篇文章 0 订阅
我使用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();

}
  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 扫一扫,分享海报

©️2022 CSDN 皮肤主题:大白 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值