2

I am developing a game with client-server, client of this game can be from Flash Web, J2ME or Android Mobile. Every day there are 300-400 users play this game. I am faced with headache problem, and I spent many days to resolve it, but hopeless ...When I run this app on server Linux (8 core processors): after one day this app consumes CPU about 100% (one core is used 100%), and the second day it consumes CPU 300%, and next day 500%... about 4-5 days, I must restart this app to prevent it not be crashed. In my project, each player I use a thread to transfer data (between client and server) and manipulate with DB, beside it, I also use many threads to manage play game of players or log information, these threads also manipulate with DataBase when necessary. Every time player or thread wants to get some information or saves something into DataBase, I create a connection for each query and close this connection after finish the query, it looks like this:

getSomething(int id) {
    ...
    String sql = "SELECT * FROM mail WHERE id = ?";
    connection = ConnectionManager.getDbConnection();
    pstmt = connection.prepareStatement(sql);
    ....
    while (rs.next()) {
    ...
    }
    connection.close();
}

It means if 400 users login or save something at the same time, 400 connections will be created to get or save data for each of user, and 400 connections will be released after finish query. I not sure this strategy is good ... or it can be the reason of my problem ...

In my log, sometime there are log with "too many connections..." it make me doubt about closing connection in my app, and this can cause CPU consumes over 100%, more connections are not released, more CPU consumes...

Now I think I should use JDBC Connection Pool to manage connections from clients, and hope it can resolve my problem ... but I am not sure, it can be not work ...

So, if you have any idea, please suggest to me, thank you so much!

  • 1
    the result of Too many connection may be dead lock. 300-400 users not so much. – chrome Oct 21 '12 at 16:22
  • I am not sure but, may be you must implement connection pooling. [link](http://docs.oracle.com/javase/jndi/tutorial/ldap/connect/pool.html) – chrome Oct 21 '12 at 16:47
  • 2
    ``connection.close()`` will be skipped if an exception is thrown. It needs to be in a ``finally`` clause. Have a look here: http://stackoverflow.com/questions/2225221/closing-database-connections-in-java to see the correct pattern to use. You may consider using some form of transaction manager. – BeRecursive Oct 21 '12 at 17:42
  • Is there a reason why you can't run the server side of the game on application server? Reinventing the wheel is never such an good idea – Sami Korhonen Oct 21 '12 at 21:09
  • Maybe your MySQL tables are badly indexed.... – Basile Starynkevitch Oct 22 '12 at 19:10
  • Have you closed your statement and resultset? Another thing you may want to check is memory usage. If your app has memory leak, garbage collector will be cpu intensive to clean memory when it hits the threshold. – Eddie Mar 27 '13 at 21:24
  • As already wrote the **connection.close()** should be in a **finally** clause. Same procedure for **resultset** and **pstmt** **close()** method. There is the possibility some connection may remain in an append state. – Oscerd Jul 24 '13 at 07:16

0 Answers0