gpt4 book ai didi

java - fatal error :sorry, 已经有太多客户端从数据库获取许多查询

转载 作者:搜寻专家 更新时间:2023-10-30 20:18:52 24 4
gpt4 key购买 nike

我有连接大小的问题...我知道问题出在哪里...我的程序负载很重,我必须从数据库中获取很多查询(这里使用的是 postgres)... hibernate 连接池大小也没有帮助我......起初这是一个稳定性问题,但是当我的数据库越来越大时,这个问题也显示得更快..这个问题有什么解决办法吗?正如控制台所说:

ERROR: FATAL: sorry, too many clients already

@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;
}
}

康斯洛:

Jun 22, 2013 2:41:36 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions WARN: SQL Error: 0, SQLState: 53300 Jun 22, 2013 2:41:36 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions ERROR: FATAL: sorry, too many clients already org.hibernate.exception.GenericJDBCException: Could not open connection at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:54) at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:125) at 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) at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.doBegin(JdbcTransaction.java:67) at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.begin(AbstractTransactionImpl.java:160) at org.hibernate.internal.SessionImpl.beginTransaction(SessionImpl.java:1426)

最佳答案

当你完成它们时,你必须关闭你的 session 。我在你的代码中没有看到

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;
}

您必须关闭 session 。所以,添加 finally block :

    finally {
scss.close();
}

关于java - fatal error :sorry, 已经有太多客户端从数据库获取许多查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17249826/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com