致命错误:抱歉,有太多客户已从db中获取许多查询

时间:2013-06-22 10:42:25

标签: java database swing hibernate

我的连接尺寸问题...... 我知道问题来自哪里...... 我的程序负载很重,我必须从DB(这里使用postgres)获得很多查询... hibernate连接池大小对我没有帮助... 起初这是一个稳定性问题,但是当我的数据库变得越来越大时,这个问题显示得更快...... 这个问题是否存在任何解决方案? 正如控制台所说:

  

错误:致命:抱歉,已有太多客户

@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 org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions   警告:SQL错误:0,SQLState:53300   2013年6月22日下午2:41:36 org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions   错误:致命:抱歉,已有太多客户   org.hibernate.exception.GenericJDBCException:无法打开连接       在org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:54)       在org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:125)       在org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:110)       at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.obtainConnection(LogicalConnectionImpl.java:221)       at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.getConnection(LogicalConnectionImpl.java:157)       在org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.doBegin(JdbcTransaction.java:67)       at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.begin(AbstractTransactionImpl.java:160)       在org.hibernate.internal.SessionImpl.beginTransaction(SessionImpl.java:1426)

1 个答案:

答案 0 :(得分:3)

完成后,您必须关闭会话。在我的代码中没有任何地方我看到了

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 {
        scss.close();
    }