0

I am having a backend deployment running on a glassfish server, the frontend of my application sends REST request when it needs some data. Also the backend is connected to a mongo database:

FRONTEND <----rest---->BACKEND<---->DB .

My backend is deployed on a ubuntu virtual machine via glassfish 5, the frontend is running on an apache web server.

After some use, may we say after 5 minutes, and especially when multiple user are using the application, the glassfish server crashes most of the time while attempting to acces the database with the error: java.net.SocketException: Too many open files .

Of course I have googled this issue already and there are many suggestions that say just to increase the number of open files which is right now at 1024.

My question to this point is now, what are these open files, and even if I increase them, would the limit not also be reached after some time. Is increasing the only slution or may there be an error in my codings? Thanks in advance.

Here is a snip of the netstat output of my ubuntu machine:

tcp        0      0 localhost:27017         localhost:44856         ESTABLISHED -               
tcp        0      0 localhost:27017         localhost:44980         ESTABLISHED -               
tcp        0      0 localhost:27017         localhost:44794         ESTABLISHED -               
tcp        0      0 localhost:27017         localhost:45132         ESTABLISHED -               
tcp        0      0 localhost:27017         localhost:44764         ESTABLISHED -               
tcp        0      0 localhost:27017         localhost:44814         ESTABLISHED -               
tcp        0      0 localhost:27017         localhost:44756         ESTABLISHED -               
tcp        0      0 localhost:27017         localhost:44374         ESTABLISHED -               
tcp        0      0 localhost:27017         localhost:44926         ESTABLISHED -               
tcp        0      0 localhost:27017         localhost:45040         ESTABLISHED -               
tcp        0      0 localhost:27017         localhost:45276         ESTABLISHED -               
tcp        0      0 localhost:27017         localhost:44858         ESTABLISHED -               
tcp        0      0 localhost:27017         localhost:45052         ESTABLISHED -               
tcp        0      0 localhost:27017         localhost:45310         ESTABLISHED -               
tcp        0      0 localhost:27017         localhost:44806         ESTABLISHED -               
tcp        0      0 localhost:27017         localhost:44840         ESTABLISHED -               
tcp        0      0 localhost:27017         localhost:45046         ESTABLISHED -               
tcp        0      0 localhost:27017         localhost:45222         ESTABLISHED -               
tcp        0      0 localhost:27017         localhost:45160         ESTABLISHED -               
tcp        0      0 localhost:27017         localhost:44990         ESTABLISHED -               
tcp        0      0 localhost:27017         localhost:44914         ESTABLISHED -               
tcp        0      0 localhost:27017         localhost:45274         ESTABLISHED -               
tcp        0      0 localhost:27017         localhost:44998         ESTABLISHED -               
tcp        0      0 localhost:27017         localhost:44788         ESTABLISHED -               
tcp6       0      0 localhost:45176         localhost:27017         ESTABLISHED 5204/java       
tcp6       0      0 localhost:45136         localhost:27017         ESTABLISHED 5204/java       
tcp6       0      0 localhost:45058         localhost:27017         ESTABLISHED 5204/java       
tcp6       0      0 localhost:44926         localhost:27017         ESTABLISHED 5204/java       
tcp6       0      0 localhost:44968         localhost:27017         ESTABLISHED 5204/java       
tcp6       0      0 localhost:45190         localhost:27017         ESTABLISHED 5204/java       
tcp6       0      0 localhost:45050         localhost:27017         ESTABLISHED 5204/java       
tcp6       0      0 localhost:44886         localhost:27017         ESTABLISHED 5204/java       
tcp6       0      0 localhost:45308         localhost:27017         ESTABLISHED 5204/java       
tcp6       0      0 localhost:44978         localhost:27017         ESTABLISHED 5204/java       
tcp6       0      0 localhost:45086         localhost:27017         ESTABLISHED 5204/java       
tcp6       0      0 localhost:45198         localhost:27017         ESTABLISHED 5204/java       
tcp6       0      0 localhost:44922         localhost:27017         ESTABLISHED 5204/java       
tcp6       0      0 localhost:44938         localhost:27017         ESTABLISHED 5204/java       
tcp6       0      0 localhost:45020         localhost:27017         ESTABLISHED 5204/java       
tcp6       0      0 localhost:44896         localhost:27017         ESTABLISHED 5204/java       
tcp6       0      0 localhost:44830         localhost:27017         ESTABLISHED 5204/java       
tcp6       0      0 localhost:44960         localhost:27017         ESTABLISHED 5204/java       
tcp6       0      0 localhost:44776         localhost:27017         ESTABLISHED 5204/java

As you can see there are way to many established connections with localhost:27017 which is the mongo database.

EDIT In my Java backend code I am having a class UtilityService with a method get database. It is returning the mongo database on request, whenever a part of the code needs to write from or to database.

public static MongoDatabase getDatabase(){
    String connectionStringProperty = getConfigProperty("MONGO_DB_CONNECTION_STRING");
    if(IsNullOrEmpty(connectionStringProperty)){
        return null;
    }

    MongoClientURI connectionString = new MongoClientURI(connectionStringProperty);
    MongoClient mongoClient = new MongoClient(connectionString);

    String databaseName = getConfigProperty("MONGO_DB_DATABASE_NAME");
    if(IsNullOrEmpty(databaseName)){
        return null;
    }

    return mongoClient.getDatabase(databaseName);
}
  • You are correct that increasing OFL may not be best solution as this could also exhaust after sometime. It seems your code is buggy which is not closing the files handlers efficiently. – Abhishek Mishra Aug 1 '18 at 14:08
  • I just updated my question with the code snip which is establishing connection to the mongo db. – Chinaedu Onwukwe Aug 1 '18 at 14:16
  • From your code, it appears you are only connecting to DB but not disposing off the connection after job is done which is creating the problem. You will need to close the DB connection as soon as application no longer needs DB – Abhishek Mishra Aug 1 '18 at 14:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.