Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Couldnt initialize class

I am writing a webapp that relies on a database

I tried checking what would happen if many users tried to register at once and I'm using Gson in order to get the object from the database

if i register 50 users at once (by spawning new threads), everything is fine but if i try to register 100 or more, i get 2 exceptions, one saying that i have too many connections open, and the other says failed to load the gson class

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)

this is my code for inserting a user into the database

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

this is my code for the Gson class

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

what can i do to avoid the "too many open connections" error , and why am i getting the 2nd error ? the class was supposed to be initialized once, and used throught the app

like image 650
Lena Bru Avatar asked Sep 24 '26 22:09

Lena Bru


1 Answers

First problem

Number of connections on a db will be always limited (at least by RAM on db server) so:

  1. Increase the number of connection to 100, if this number if enough for your test, but this will consume more resources on server or (better)
  2. Use a connection pool to manage connection on server, this is the best solution since connection will be reused and you can try whenever number for your test. You can find many ready to use solutions around the net. See here for example.

Second problem

Your class com.electronicarena.utils.EAGson is not where Java expects to be. Nothing related with Gson itself. So you have to check your PATH variable. If you are using Eclipse it may be that class is not compiled or that you have to check Deploy section on project preferences.

like image 101
giampaolo Avatar answered Sep 26 '26 10:09

giampaolo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!