RejectedSoftware Forums

Sign up

Disconnect ConnectionPool

I'm using a ConnectionPool (more specifically a MongoClient) to connect to a server and after some use I wish to terminate the connection.

I've see that removing the ConnectionPool variable does not actually destroy the object and the connection stays open.

I don't want to end up manually freeing memory since I believe it will cause several problems. So how can I terminate the connection?

Thanks for any help.

Re: Disconnect ConnectionPool

Am 20.07.2018 um 12:40 schrieb Ivo:

I'm using a ConnectionPool (more specifically a MongoClient) to connect to a server and after some use I wish to terminate the connection.

I've see that removing the ConnectionPool variable does not actually destroy the object and the connection stays open.

I don't want to end up manually freeing memory since I believe it will cause several problems. So how can I terminate the connection?

Thanks for any help.

So generally ConnectionPool assumes that each connection manages their
connection state individually, most importantly that it ensures that the
connection stays open/gets reconnected. Similarly, closing a connection
would also need to happen through the connection itself.

In the case of MongoClient, the code needs to be extended to enable
access to MongoConnection.disconnect() in some way, or to define a
closing logic within MongoConnection itself, for example a timeout.
The connection object itself would then still stay alive, but all
associated resources would be freed until it is used again. At that
point it would automatically reconnect.

Do you have specific semantics in mind for closing the connection(s)?

Re: Disconnect ConnectionPool

On Sun, 29 Jul 2018 10:57:12 +0200, Sönke Ludwig wrote:

So generally ConnectionPool assumes that each connection manages their
connection state individually, most importantly that it ensures that the
connection stays open/gets reconnected. Similarly, closing a connection
would also need to happen through the connection itself.

In the case of MongoClient, the code needs to be extended to enable
access to MongoConnection.disconnect() in some way, or to define a
closing logic within MongoConnection itself, for example a timeout.
The connection object itself would then still stay alive, but all
associated resources would be freed until it is used again. At that
point it would automatically reconnect.

Do you have specific semantics in mind for closing the connection(s)?

What I needed was simply to disconnect manually when required so that one can logout and login on MongoDB.
So my quick fix of the code was introducing a disconnect() method in the MongoClient class.
Here is the code:

void disconnect(){
	foreach (con ; m_connections.m_connections) {
		con.disconnect();
	}
}

I'm not sure at all this is enough to free all resources used by the connection pool, nevertheless I've been using it for a bit and it seems fine.