Question

A commonly problem I see in lots of web languages is that database connections need to be closed otherwise the number of total connections gradually increases and then it grinds to a halt in whatever form.

HTTP is stateless, when the request has finished processing why can't these languages just drop any connections that request opened? Are there any legitimate reasons for why you might keep it open?

Was it helpful?

Solution

Because the cost of opening, authenticating and authorising access to a database is quite expensive. That is why normally everybody uses a databases connection pool. Connections are still open while request handlers pick up a available-already-opened connection from a pool. When one closes a connection what is really happening is that the connection is being freed for others to use.

To answer ...

why can't these languages just drop any connections that request opened? Are there any legitimate reasons for why you might keep it open?

Connections might stay opened after the request is complete and use for other purposes. For instance asynchronous updates of data. But I am with you, in 90% of the cases when the request is finished the connections opened should be returned back to the pool. Depending on the Web Framework you use (Spring, DJANGO, ...) this kind of behaviour can be configured or at least implemented with minimum effort.

OTHER TIPS

Checking for an open connection while closing an http connection gives more overhead so I guess that's why some languages don't close it by default.

And if you don't close it explicitly, it will have to be done by the garbage collector which can take a while.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top