我应该多久关闭一次数据库连接?

时间:2022-09-16 11:18:33

Currently, I'm opening a database connection in my app's initialization. It's a fairly small app, PHP if that's relevant.

目前,我正在我的应用初始化中打开数据库连接。它是一个相当小的应用程序,PHP,如果这是相关的。

Should I be connecting to the database, making calls, then closing and repeating this process for each database function I write?

我应该连接数据库,进行调用,然后为我编写的每个数据库函数关闭并重复此过程吗?

For example, I have the following function which grabs the $db variable from my app's initialization.

例如,我有以下函数从我的应用程序初始化中获取$ db变量。

function get_all_sections()
{
    global $db;
    $sql = 'select * from sections'; 

    if (!$db->executeSQL($sql, $result))
    {
        throw new Exception($db->getDatabaseError());
        exit();
    }

    $sections = array();

    for ($i = 0; $i < $db->numberOfRows($result); $i++)
    {
        $sections[] = new Section($db->fetchArray($result, MYSQLI_ASSOC));
    }

    return $sections;
}

Would it be better if I opened the connection then closed it after I fetched the rows? That seems like a lot of connections that are opened and closed.

如果我打开连接然后在我取出行后关闭它会更好吗?这似乎是很多打开和关闭的连接。

5 个解决方案

#1


5  

If you have connection pooling on (http://en.wikipedia.org/wiki/Connection_pool) its ok to be grabbing a new connection when you need it. HOWEVER, I'd say to be in the habit of treating any resource as "limited" and if you open the db handle keep it around for as long as possible.

如果您在(http://en.wikipedia.org/wiki/Connection_pool)上有连接池,则可以在需要时获取新连接。但是,我会说习惯将任何资源视为“有限”,如果你打开数据库句柄,请尽可能长时间保持它。

#2


3  

Connecting to the database takes a finite amount of time. It's negligible when connecting over a domain socket or named pipe, but it can be much larger if over a network connection, or worse yet, the open Internet. Leave it connected for the life of the request at least.

连接数据库需要花费有限的时间。通过域套接字或命名管道进行连接时,它可以忽略不计,但如果通过网络连接,或者更糟糕的是开放式Internet,它可能会大得多。至少在请求期限内保持连接状态。

#3


3  

Use mysql_pconnect for connection pooling, and close at the end of each operation. The thread won't really close, and the thread will be re-used. This way its both safe and efficient.

使用mysql_pconnect进行连接池,并在每个操作结束时关闭。线程不会真正关闭,线程将被重用。这样既安全又高效。

#4


2  

Since PHP MySQL connections are fairly light-weight, you are probably OK opening and closing the connection when needed. The same is not true of other database connections, such as connecting to SQL Server, which has rather heavy connections.

由于PHP MySQL连接相当轻,因此您可以在需要时打开和关闭连接。其他数据库连接也是如此,例如连接到具有相当繁重连接的SQL Server。

In all cases, however, do whatever makes the most sense in terms of maintainability/logic/dependencies. Then, if you find you are feeling a measurable slowdown, you can optimize those areas that need the speed-boost.

但是,在所有情况下,在可维护性/逻辑/依赖性方面做任何最有意义的事情。然后,如果您发现自己感觉到了可测量的减速,那么您可以优化那些需要提速的区域。

When in doubt, follow the golden rule: Don't optimize prematurely.

如有疑问,请遵循黄金法则:不要过早优化。

#5


1  

The simplest solution would be to use mysql_pconnect() - see here

最简单的解决方案是使用mysql_pconnect() - 请参阅此处

This way if the connection is already open it will use it instead of creating a new one. If it's not it will connect again

这样,如果连接已经打开,它将使用它而不是创建一个新连接。如果不是它将再次连接

#1


5  

If you have connection pooling on (http://en.wikipedia.org/wiki/Connection_pool) its ok to be grabbing a new connection when you need it. HOWEVER, I'd say to be in the habit of treating any resource as "limited" and if you open the db handle keep it around for as long as possible.

如果您在(http://en.wikipedia.org/wiki/Connection_pool)上有连接池,则可以在需要时获取新连接。但是,我会说习惯将任何资源视为“有限”,如果你打开数据库句柄,请尽可能长时间保持它。

#2


3  

Connecting to the database takes a finite amount of time. It's negligible when connecting over a domain socket or named pipe, but it can be much larger if over a network connection, or worse yet, the open Internet. Leave it connected for the life of the request at least.

连接数据库需要花费有限的时间。通过域套接字或命名管道进行连接时,它可以忽略不计,但如果通过网络连接,或者更糟糕的是开放式Internet,它可能会大得多。至少在请求期限内保持连接状态。

#3


3  

Use mysql_pconnect for connection pooling, and close at the end of each operation. The thread won't really close, and the thread will be re-used. This way its both safe and efficient.

使用mysql_pconnect进行连接池,并在每个操作结束时关闭。线程不会真正关闭,线程将被重用。这样既安全又高效。

#4


2  

Since PHP MySQL connections are fairly light-weight, you are probably OK opening and closing the connection when needed. The same is not true of other database connections, such as connecting to SQL Server, which has rather heavy connections.

由于PHP MySQL连接相当轻,因此您可以在需要时打开和关闭连接。其他数据库连接也是如此,例如连接到具有相当繁重连接的SQL Server。

In all cases, however, do whatever makes the most sense in terms of maintainability/logic/dependencies. Then, if you find you are feeling a measurable slowdown, you can optimize those areas that need the speed-boost.

但是,在所有情况下,在可维护性/逻辑/依赖性方面做任何最有意义的事情。然后,如果您发现自己感觉到了可测量的减速,那么您可以优化那些需要提速的区域。

When in doubt, follow the golden rule: Don't optimize prematurely.

如有疑问,请遵循黄金法则:不要过早优化。

#5


1  

The simplest solution would be to use mysql_pconnect() - see here

最简单的解决方案是使用mysql_pconnect() - 请参阅此处

This way if the connection is already open it will use it instead of creating a new one. If it's not it will connect again

这样,如果连接已经打开,它将使用它而不是创建一个新连接。如果不是它将再次连接