保持数据库连接打开是否昂贵?

时间:2022-04-03 16:50:44

I was in a meeting with a few software developers, and was recommended to close database connections as soon as possible in my application code? Can somebody please tell me what is the harm of keeping a connection open in an application.

我正在与一些软件开发人员会面,并建议在我的应用程序代码中尽快关闭数据库连接?有人可以告诉我在应用程序中保持连接打开的危害是什么。

I was reading the data from a single table of a database

我正在从数据库的单个表中读取数据

1 个解决方案

#1


8  

Think of it like seats on a bus.

把它想象成公共汽车上的座位。

As you open connections, you fill up those seats - eventually, the bus is full and can no longer accept passengers (or open more database connections). Any time the bus has to refuse a passenger because it's at capacity, that passenger has to wait for another bus to come by.

当您打开连接时,您将填满这些座位 - 最终,公交车已满,无法再接受乘客(或打开更多数据库连接)。任何时候公共汽车必须拒绝乘客,因为它的容量,乘客必须等待另一辆公共汽车来。

By closing your connections when you're done with them, you free up room for more connections - which means more programs that need to interact with the database can do what they need to, without having to wait around for connections to free up. Not closing your connections means the database needs to figure out what to do with all the connections sitting around, which can cause problems if your database isn't closing connections as quickly as you're opening new ones.

通过在完成连接后关闭连接,可以腾出空间来获得更多连接 - 这意味着需要与数据库交互的更多程序可以执行他们需要的操作,而无需等待连接释放。不关闭连接意味着数据库需要弄清楚如何处理所有连接,如果您的数据库没有像打开新连接那样快速关闭连接,这可能会导致问题。

This changes when you're using a connection pool (see comments below); in those situations you'll want your pool to handle opening and closing connections for you. If you're not pooling your connections, keeping them open any longer than you need to is wasting resources.

当您使用连接池时,这会发生变化(请参阅下面的评论);在这些情况下,您会希望您的池为您处理打开和关闭连接。如果你没有汇集你的连接,那么让它们保持打开的时间比你需要的时间长,浪费资源。

#1


8  

Think of it like seats on a bus.

把它想象成公共汽车上的座位。

As you open connections, you fill up those seats - eventually, the bus is full and can no longer accept passengers (or open more database connections). Any time the bus has to refuse a passenger because it's at capacity, that passenger has to wait for another bus to come by.

当您打开连接时,您将填满这些座位 - 最终,公交车已满,无法再接受乘客(或打开更多数据库连接)。任何时候公共汽车必须拒绝乘客,因为它的容量,乘客必须等待另一辆公共汽车来。

By closing your connections when you're done with them, you free up room for more connections - which means more programs that need to interact with the database can do what they need to, without having to wait around for connections to free up. Not closing your connections means the database needs to figure out what to do with all the connections sitting around, which can cause problems if your database isn't closing connections as quickly as you're opening new ones.

通过在完成连接后关闭连接,可以腾出空间来获得更多连接 - 这意味着需要与数据库交互的更多程序可以执行他们需要的操作,而无需等待连接释放。不关闭连接意味着数据库需要弄清楚如何处理所有连接,如果您的数据库没有像打开新连接那样快速关闭连接,这可能会导致问题。

This changes when you're using a connection pool (see comments below); in those situations you'll want your pool to handle opening and closing connections for you. If you're not pooling your connections, keeping them open any longer than you need to is wasting resources.

当您使用连接池时,这会发生变化(请参阅下面的评论);在这些情况下,您会希望您的池为您处理打开和关闭连接。如果你没有汇集你的连接,那么让它们保持打开的时间比你需要的时间长,浪费资源。