You are calling close on your find object, which is a DBCursor. As mentioned in the API Documentation this will close only the cursor, not the underlying connection. To close the connections you should call close on the Mongo/MongoClient object, which either inherits or has a close function that will as per the API close all connections to the MongoDB server instance.
MongoDB, Java: accessing cursor opens connection that cannot be closed
-
28-06-2023 - |
Pergunta
I have problem to close a connection that is always created after accessing the MongoDB cursor.
I am gathering the information about the connections from the console of the running mongod instance. I am not using any mongodb replication nor sharding yet.
For example a sample code causing an opened connection (assume 'myQuery' is just some query, coll is mapped using setInternalClass to the class MyObject):
DBCursor find = coll.find(myQuery);
List<MyObject> myObjects = new ArrayList<MyObject>();
while(find.hasNext()) { // this line opens the connection
MyObject next = (MyObject) find.next();
myObjects.add(next);
}
find.close(); // this line will not close the connection
Solução
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow