我在 glassfish 服务器上运行后端部署,当应用程序的前端需要某些数据时,它会发送 REST 请求。此外,后端还连接到 mongo 数据库:
前端<----rest---->后端<---->DB。
我的后端通过 glassfish 5 部署在 ubuntu 虚拟机上,前端在 apache 网络服务器上运行。
经过一段时间的使用后,比如说 5 分钟后,尤其是当多个用户使用该应用程序时,glassfish 服务器在尝试访问数据库时大多数时候都会崩溃并出现错误:java.net.SocketException: Too many open files
。
当然,我已经在 Google 上搜索过这个问题了,有很多建议说只要增加现在打开的文件的数量1024
。
我现在的问题是,这些打开的文件是什么?即使我增加它们的数量,一段时间后是否也会达到限制。增加是唯一的解决方案,还是我的编码可能有错误?提前致谢。
以下是我的 ubuntu 机器的 netstat 输出的片段:
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
如您所见,与 localhost:27017(即 mongo 数据库)建立了许多连接。
编辑
在我的 Java 后端代码中,我有一个带有UtilityService
获取数据库方法的类。每当代码的一部分需要从数据库写入数据或向数据库写入数据时,它都会根据请求返回 mongo 数据库。
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);
}