gpt4 book ai didi

java - 无法初始化类

转载 作者:搜寻专家 更新时间:2023-10-30 19:51:06 27 4
gpt4 key购买 nike

我正在编写一个依赖于数据库的 webapp

我尝试检查如果许多用户尝试同时注册会发生什么我正在使用 Gson 从数据库中获取对象

如果我一次注册 50 个用户(通过生成新线程),一切都很好但是如果我尝试注册 100 个或更多,我会得到 2 个异常,一个说我打开了太多连接,另一个说加载 gson 类失败

com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Data source rejected establishment of connection,  message from server: "Too many connections"
at sun.reflect.GeneratedConstructorAccessor13.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.Util.getInstance(Util.java:386)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1014)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:988)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:974)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1110)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2465)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2498)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2283)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:822)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
at sun.reflect.GeneratedConstructorAccessor1.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:404)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:317)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at com.database.Database.openDatabase(Database.java:229)
at com.database.Database.log(Database.java:253)
at com.electronicarena.utils.Log.debug(Log.java:54)
at com.electronicarena.utils.Log.info(Log.java:68)
at com.database.Database.findAll(Database.java:434)
at com.database.Database.find(Database.java:490)
at com.database.Database.find(Database.java:514)
at com.database.adapters.UserAdapter.getUserByLoginName(UserAdapter.java:53)
at com.database.adapters.UserAdapter.isUserExist(UserAdapter.java:41)
at com.database.adapters.UserAdapter.register(UserAdapter.java:173)
at com.lenabru.webservice.ElectronicArenaWebService.memberRegister(ElectronicArenaWebService.java:65)
at test.UserAdapterTest$UserThread.run(UserAdapterTest.java:168)



Exception in thread "Thread-87" java.lang.NoClassDefFoundError: Could not initialize class com.electronicarena.utils.EAGson
at com.database.DatabaseObject.get(DatabaseObject.java:299)
at com.database.DatabaseObject.set(DatabaseObject.java:365)
at com.database.DatabaseObject.set(DatabaseObject.java:337)
at com.database.DatabaseObject.getFindByKeysPreparedStatement(DatabaseObject.java:97)
at com.database.Database.findAll(Database.java:435)
at com.database.Database.find(Database.java:490)
at com.database.Database.find(Database.java:514)
at com.database.adapters.UserAdapter.getUserByLoginName(UserAdapter.java:53)
at com.database.adapters.UserAdapter.isUserExist(UserAdapter.java:41)
at com.database.adapters.UserAdapter.register(UserAdapter.java:173)
at com.lenabru.webservice.ElectronicArenaWebService.memberRegister(ElectronicArenaWebService.java:65)
at test.UserAdapterTest$UserThread.run(UserAdapterTest.java:168)

这是我将用户插入数据库的代码

public int insert(DatabaseObject object) {
int dbEntry = -1;
Connection con = null;
PreparedStatement statement = null;
String sql = null;
try {
Log.info("inserting object: " + object);
con = openDatabase();
statement = object.getInsertPreparedStatement(con);
sql = statement.toString();
dbEntry = statement.executeUpdate();
Log.info("dbEntry: " + dbEntry);
} catch (SQLException e) {
Log.error("failed to insert object " + object + " into database ", e);
} finally {

close(null, statement, con);
backupSQL(sql);
}
return dbEntry;
}

这是我的 Gson 类代码

    public static JSONObject toJsonObject(Object o) {
Log.info(o);
JSONObject result = null;
try {
String jsonString = toJson(o);
result = new JSONObject(jsonString);
} catch (JSONException e) {
Log.warning("toJSonObject failed '" + o + "'",e);
}
return result;
}

我该怎么做才能避免“太多打开的连接”错误,为什么我会收到第二个错误?该类应该被初始化一次,并在整个应用程序中使用

最佳答案

第一个问题

数据库上的连接数将始终受到限制(至少受数据库服务器上 RAM 的限制),因此:

  1. 将连接数量增加到 100,如果这个数量足以满足您的测试需求,但这会消耗更多服务器资源或(更好)
  2. 使用连接池来管理服务器上的连接,这是最好的解决方案,因为连接将被重用,您可以随时尝试进行测试。您可以在网上找到许多现成的解决方案。参见 here例如。

第二个问题

您的类 com.electronicarena.utils.EAGson 不在 Java 期望的位置。与 Gson 本身无关。所以你必须检查你的 PATH 变量。如果您使用的是 Eclipse,则可能是该类未编译,或者您必须检查项目首选项的“部署”部分。

关于java - 无法初始化类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19191428/

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