English 中文(简体)
如果HTTP是无状态的,为什么我需要关闭数据库连接?
原标题:If HTTP is stateless why do I need to close a database connection?

我在许多网络语言中看到的一个常见问题是,数据库连接需要关闭,否则总连接数会逐渐增加,然后以任何形式停止。

HTTP是无状态的,当请求完成处理时,为什么这些语言不能丢弃请求打开的任何连接?有什么正当理由可以让它继续开放吗?

最佳回答

因为打开、验证和授权访问数据库的成本相当昂贵。这就是为什么通常每个人都使用数据库连接池的原因。当请求处理程序从池中提取可用的已打开连接时,连接仍处于打开状态。当一个人关闭一个连接时,真正发生的事情是该连接被释放给其他人使用。

要回答。。。

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

请求完成后,连接可能会保持打开状态,并用于其他目的。例如,数据的异步更新。但我同意你的看法,在90%的情况下,当请求完成时,打开的连接应该返回到池中。根据您使用的Web框架(Spring、DJANGO…),可以配置或至少以最少的工作量实现这种行为。

问题回答

在关闭http连接的同时检查打开的连接会带来更多的开销,所以我想这就是为什么有些语言默认情况下不关闭它的原因。

如果不显式关闭它,则必须由垃圾收集器来完成,这可能需要一段时间。





相关问题
what is wrong with this mysql code

$db_user="root"; $db_host="localhost"; $db_password="root"; $db_name = "fayer"; $conn = mysqli_connect($db_host,$db_user,$db_password,$db_name) or die ("couldn t connect to server"); // perform query ...

Users asking for denormalized database

I am in the early stages of developing a database-driven system and the largest part of the system revolves around an inheritance type of relationship. There is a parent entity with about 10 columns ...

Easiest way to deal with sample data in Java web apps?

I m writing a Java web app in my free time to learn more about development. I m using the Stripes framework and eventually intend to use hibernate and MySQL For the moment, whilst creating the pages ...

join across databases with nhibernate

I am trying to join two tables that reside in two different databases. Every time, I try to join I get the following error: An association from the table xxx refers to an unmapped class. If the ...

How can I know if such value exists in database? (ADO.NET)

For example, I have a table, and there is a column named Tags . I want to know if value programming exists in this column. How can I do this in ADO.NET? I did this: OleDbCommand cmd = new ...

Convert date to string upon saving a doctrine record

I m trying to migrate one of my PHP projects to Doctrine. I ve never used it before so there are a few things I don t understand. In my current code, I have a class similar to this: class ...

热门标签