M
M
mirus362016-04-11 11:58:48
Database
mirus36, 2016-04-11 11:58:48

What happens if you don't close the database connection?

Tell me, why is it so bad not to close the connection to the database? What kind of resources can run out? Is it possible to bring the base to the point that it will no longer be possible to connect to it at all? Maybe there are articles on this topic?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
nirvimel, 2016-04-11
@nirvimel

Each DBMS connection is an open socket (TCP, IPC, or UNIX socket), plus on both sides (client and server) data structures that store the state of that connection, and (most importantly) data caches that can grow up to rather large sizes (for example, if the results of all data fetches made through this connection are stuck there, this option is possible). But this is not the worst. Much worse is that in the configs of almost any DBMS there is a limit of active connections, after which the server stops accepting new connections. And this limit is set with the expectation that the server can cope with the corresponding amount of realactive (that is, relatively loaded) connections, would not freeze at the same time and would not scoop out all the memory when trying to execute requests from all connections at the same time. Accordingly, this limit is set quite low and is not designed for hanging connections to accumulate in tens of thousands.
But there is good news: short-lived processes (such as scripts on a web server) can not care about closing connections at all, because when the process is destroyed, all open descriptors, including sockets, are closed anyway, and when one side of the socket is broken, another one is automatically closed, all functions that are waiting on this socket crash with an error, the server is certainly designed for this, it immediately releases all allocated resources related to the connection, the socket of which was broken.
Keep in mind that depending on the implementation of the web server, the process itself may not be killed when the script ends.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question