我正在编写一个依赖于数据库的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;
}
我该怎么做才能避免“太多打开连接”错误,为什么我得到第二个错误? 该类应该初始化一次,并在应用程序中使用
答案 0 :(得分:1)
第一个问题
db上的连接数总是有限的(至少是db服务器上的RAM),所以:
第二个问题
您的类com.electronicarena.utils.EAGson
不是Java所期望的。与Gson本身无关。所以你必须检查你的PATH变量。如果您使用的是Eclipse,则可能是该类未编译,或者您必须检查项目首选项的“部署”部分。