当前位置: 首页 > 知识库问答 >
问题:

致命错误:对不起,太多的客户端已经从数据库中获取许多查询

陶乐生
2023-03-14
@SuppressWarnings("deprecation")
public class DBUtilU {
private static Session scss;
private static SessionFactory sfactory;
static {
    Configuration cfg = new Configuration();
    cfg.addAnnotatedClass(Userdetails.class);
    cfg.addAnnotatedClass(Code.class);
    cfg.addAnnotatedClass(Messages.class);
    cfg.addAnnotatedClass(Comments.class);
    cfg.configure();
    new SchemaExport(cfg);
    sfactory = cfg.buildSessionFactory();

}

public static boolean saveObject(Object object) {
    try {
        scss = sfactory.getCurrentSession();
        scss.beginTransaction();
        scss.save(object);
        scss.getTransaction().commit();
    } catch (Exception e) {
        e.printStackTrace();
        scss.getTransaction().rollback();
        return false;
    }
    return true;
}

public static boolean updateObject(Object object) {
    try {
//          scss = sfactory.getCurrentSession();
//          scss.beginTransaction();
        scss.update(object);
//          scss.getTransaction().commit();
    } catch (Exception e) {
        e.printStackTrace();
        scss.getTransaction().rollback();
        return false;
    }
    return true;
}

public static boolean removeObject(Object object) {
    try {
        scss = sfactory.getCurrentSession();
        scss.beginTransaction();
        scss.delete(object);
        scss.getTransaction().commit();
        return true;
    } catch (Exception e) {
        e.printStackTrace();
        scss.getTransaction().rollback();
    }
    return false;
}

public static Userdetails loadUser(String username, String password) {
    Userdetails userdetails;
    try {
        scss = sfactory.openSession();
        scss.beginTransaction();
        String query = "from Userdetails where username=\'" + username
                + "\' and password=\'" + password + "\'";
        Query queryResult = scss.createQuery(query);
        userdetails = (Userdetails) queryResult.list().get(0);
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }

    return userdetails;

}

        public static Userdetails loadUserForMsging(String username) {
    Userdetails userdetails;
    try {
        scss = sfactory.openSession();
        scss.beginTransaction();
        String query = "from Userdetails where username=\'" +username+"\'";
        Query queryResult = scss.createQuery(query);
        userdetails = (Userdetails) queryResult.list().get(0);
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }

    return userdetails;

}

        public static Code loadCode(String Path) {
    Code code;
    try {
        scss = sfactory.openSession();
        scss.beginTransaction();
        String query = "from Code where fileName=\'" + Path + "\'";
        Query queryResult = scss.createQuery(query);
        code = (Code) queryResult.list().get(0);
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }

    return code;

}

public static void Ratermethod(String filename,int rate) {
    Code code = null;
    try {
        scss = sfactory.openSession();
        scss.beginTransaction();
        String query = "from Code where fileName=\'" + filename + "\'";
        Query queryResult = scss.createQuery(query);
        code = (Code) queryResult.list().get(0);
    } catch (Exception e) {
        e.printStackTrace();
    }
    int nor=code.getNORaters()+1;
    int prate=code.getRating();
    int nrate = ((((nor-1)*prate)+rate)/nor);
    code.setNORaters(nor);
    code.setRating(nrate);
    DBUtilU.updateObject(code);
    scss.getTransaction().commit();

}

        public static void AddCmtmethod(String filename,String cm,String cmplacer)         {
    Code code = null;
    try {
        scss = sfactory.openSession();
        scss.beginTransaction();
        String query = "from Code where fileName=\'" + filename + "\'";
        Query queryResult = scss.createQuery(query);
        code = (Code) queryResult.list().get(0);
    } catch (Exception e) {
        e.printStackTrace();
    }

            Comments cmt =new Comments(cm,cmplacer);
            cmt.setTargetCode(code);
            DBUtilU.updateObject(code);
            DBUtilU.saveObject(cmt);
//      scss.getTransaction().commit();

}

public static boolean LogoutUser() {
    try {
                if (scss.isOpen()) {
                    scss.getTransaction().commit();
                }
    } catch (Exception e) {
        e.printStackTrace();
        scss.getTransaction().rollback();
        return false;
    }
    return true;
}

public static boolean isUser(String username,String pass) {
    Userdetails userdetails = null;
    try {
        scss = sfactory.getCurrentSession();
        scss.beginTransaction();
        String query = "from Userdetails where username=\'" + username
                + "\' and password=\'" + pass + "\'";
        Query queryResult = scss.createQuery(query);
        userdetails = (Userdetails) queryResult.list().get(0);
        if (userdetails != null)
            scss.getTransaction().commit();
            return true;
    } catch (Exception e) {
        System.err.println(e);
    }
    scss.getTransaction().commit();
    return false;
}

        public static boolean UniqueUser(String username) {
    Userdetails userdetails = null;
    try {
        scss = sfactory.getCurrentSession();
        scss.beginTransaction();
        String query = "from Userdetails where username=\'" + username +     "\'";
                    Query queryResult = scss.createQuery(query);
        userdetails = (Userdetails) queryResult.list().get(0);
        if (userdetails != null)
            scss.getTransaction().commit();
            return true;
    } catch (Exception e) {
        System.err.println(e);
    }
    scss.getTransaction().commit();
    return false;
}

public static List<String> ListOfUsers() {
    List<String> Userlist = new ArrayList<String>();
    try {
        scss = sfactory.getCurrentSession();
        scss.beginTransaction();
        String query = "select username from Userdetails";
        Query queryResult = scss.createQuery(query);
        for(Object c : queryResult.list())
            Userlist.add((String)c);
                    scss.getTransaction().commit();
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
    return Userlist;
}

public static List<Code> searchCodeTags(String tags) {
    List<Code> codelist = new ArrayList<Code>();
    try {
        scss = sfactory.getCurrentSession();
        scss.beginTransaction();
        String query = "from Code where Tags='" + tags+"'";
        Query queryResult = scss.createQuery(query);
        for(Object c : queryResult.list())
            codelist.add((Code)c);
                    String query2 = "from Code where Tags2='" + tags+"'";
                    Query queryResult2 = scss.createQuery(query2);
                    for(Object c : queryResult2.list())
            codelist.add((Code)c);
                    scss.getTransaction().commit();
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
    return codelist;
}

        public static List<Code> searchCodeAll(String all) {
    List<Code> codelist = new ArrayList<Code>();
    try {
        scss = sfactory.getCurrentSession();
        scss.beginTransaction();
        String query = "from Code where Tags='" + all+"'";
        Query queryResult = scss.createQuery(query);
        for(Object c : queryResult.list())
            codelist.add((Code)c);
                    String query2 = "from Code where Tags2='" + all+"'";
                    Query queryResult2 = scss.createQuery(query2);
                    for(Object c : queryResult2.list())
            codelist.add((Code)c);
                    String query3 = "from Code where CodeLang='" + all+"'";
                    Query queryResult3 = scss.createQuery(query3);
                    for(Object c : queryResult3.list())
            codelist.add((Code)c);
                    String query4 = "from Code where OwnerName='" + all+"'";
                    Query queryResult4 = scss.createQuery(query4);
                    for(Object c : queryResult4.list())
            codelist.add((Code)c);
                    scss.getTransaction().commit();
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
    return codelist;
}
                public static List<Code> searchCodePrj(String prj) {
    List<Code> codelist = new ArrayList<Code>();
    try {
        scss = sfactory.getCurrentSession();
        scss.beginTransaction();
        String query = "from Code where PrjName='" + prj+"'";
        Query queryResult = scss.createQuery(query);
        for(Object c : queryResult.list())
            codelist.add((Code)c);
                    scss.getTransaction().commit();
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
    return codelist;
}
            public static List<Code> searchCodeLang(String lang) {
    List<Code> codelist = new ArrayList<Code>();
    try {
        scss = sfactory.getCurrentSession();
        scss.beginTransaction();
        String query = "from Code where CodeLang='" + lang+"'";
        Query queryResult = scss.createQuery(query);
        for(Object c : queryResult.list())
            codelist.add((Code)c);
                    scss.getTransaction().commit();
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
    return codelist;
}

        public static List<Messages> MessageLoader(String Reciever) {
    List<Messages> MSGlist = new ArrayList<Messages>();
    try {
        scss = sfactory.getCurrentSession();
        scss.beginTransaction();
                    String query = "from Messages where Reciever='" + Reciever + "'";
        Query queryResult = scss.createQuery(query);
        for(Object c : queryResult.list())
            MSGlist.add((Messages)c);
                    scss.getTransaction().commit();
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
    return MSGlist;
            }
}

Consloe:

2013年6月22日2:41:36 PM org.hibernate.engine.jdbc.spi.sqlExceptionHelper logExceptions警告:SQL错误:0,SQLState:53300 2013年6月22日2:41:36 PM org.hibernate.engine.jdbc.spi.sqlExceptionHelper logExceptions错误:致命:抱歉,已经有太多客户端org.hibernate.exception.genericJDBExceptions:无法在在org.hibernate.engine.jdbc.internal.logicalConnectionImpl.GetConnection(logicalConnectionImpl.java:221)在org.hibernate.engine.jdbc.internal.jdbc.geticalConnectionImpl.getConnection(logicalConnectionImpl.java:157)在org.hibernate.engine.transaction.jdbegin(jdbcTransaction.dobegin(jdbcTransaction.java:67)在org.hibernate.internal.SessionImpl.BeginTransaction(SessionImpl.java:1426)的(AbstractTransactionImpl.java:160)中

共有1个答案

蔡理
2023-03-14

当你用完它们时,你必须关闭你的会话。在你的代码中我看不到任何地方

session.close();

例如这里:

public static Code loadCode(String Path) {
    Code code;
    try {
        scss = sfactory.openSession();
        scss.beginTransaction();
        String query = "from Code where fileName=\'" + Path + "\'";
        Query queryResult = scss.createQuery(query);
        code = (Code) queryResult.list().get(0);
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }

必须关闭会话。因此,添加finally块:

    finally {
        scss.close();
    }
 类似资料:
  • 问题内容: 我正在PostgreSQL 9.1中使用数据库,该数据库中的条目连续来自另一个程序。我正在6秒后从Ajax发送请求以获取最新条目.tomcat输出窗口显示异常— 之后程序也可以正常工作。当我通过查询检查我的postgres时- 它表明连接不断增加,但是在每个请求之后我都关闭了连接。我正在使用netbeans和struts 1.3。 //连接bean中的关闭方法是 每次在控制台“ con

  • 我试图测试的速度,反对“手动”乘法,并偶然发现了这个错误: Java 运行时环境检测到致命错误: EXCEPTION_ACCESS_VIOLATION (0xc0000005) 在 pc=0x000000005ac46888, pid=1508, tid=6016 JRE版本:Java(TM)SE运行时环境(8.0_25-b18)(构建1.8.0-25-B16) Java VM:Java热点(TM

  • 明显感觉到越问越难,HTTP/HTTPS这块连环拷打问了快二十多个问题。。。 吐槽一下并夕夕的编译器,真的很难用 项目拷打40min(中间穿插着问了一些八股) 网关在接收设备时,如何保证信息不丢失? 有没有遇到粘包?怎么解决的? 那怎么拆包呢? 如何保证公钥不被篡改? 怎么确保你的token安全送达(用HTTPS) 为什么用HTTPS就安全了 有没有自己抓过包? 此处省略几个https相关问题..

  • 亲爱的妖精猎人(开发者),我已经尝试解决这个问题两天了..我试图建立一个基于PHP邮件器的公司的营销目的邮件器,当我们发送一个HTML文件给6-8个(或多或少)收件人时,我的代码工作良好,但当我们试图通过我的本地主机发送HTML文件给100多个收件人时,我得到以下错误: 我的代码如下:

  • 我的应用程序中出现“未授权”错误。我使用的是Spring Security和oauth2。我的客户端和用户存储在数据库中。当我开始使用数据库中的客户端时,PostMan中出现了错误401。客户端正在保存到数据库中,但当我想从localhost:8080/oauth/token获取令牌访问时,仍然出现错误。以下是我的来源: 授权服务器配置: public class AuthorizationSer