无法初始化类

时间:2013-10-04 21:56:53

标签: java database gson

我正在编写一个依赖于数据库的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;
 }

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

1 个答案:

答案 0 :(得分:1)

第一个问题

db上的连接数总是有限的(至少是db服务器上的RAM),所以:

  1. 将连接数增加到100,如果此数字足以进行测试,但这将消耗更多服务器上的资源或(更好)
  2. 使用连接池管理服务器上的连接,这是最好的解决方案,因为连接将被重用,您可以尝试每次测试的号码。您可以在网上找到许多随时可用的解决方案。例如,请参阅here
  3. 第二个问题

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