I am working on implementing use of the mysql class found here in an existing script. The script almost always needs to interact with the database, even if there are times when it does not. What is the best practice in this case? Should I open a connection and keep that open until the end of the script or should I open a connection when I need one, closing it when I'm done, to avoid opening a connection when the script does not need it?
我正在实现使用现有脚本中找到的mysql类。脚本几乎总是需要与数据库进行交互,即使有时不需要。在这种情况下,最好的做法是什么?我应该打开一个连接并一直打开直到脚本结束,还是应该在需要时打开一个连接,在完成时关闭它,以避免在脚本不需要连接时打开一个连接?
4 个解决方案
#1
13
Because connections are rather expensive, as others have pointed out, I'd recommend using a "lazy connect" technique in your database layer. If you have structured your application effectively, your application logic should not be concerned with when connections are opened and closed as this would be encapsulated in the database layer. The database layer, when asked to perform a query, would first check to see if it has an active connection and if not, create one. This way you'll avoid opening connections that are never used and you'll also have a nice separation of logic between your application and the database code.
因为连接非常昂贵,正如其他人指出的那样,我建议在数据库层中使用“惰性连接”技术。如果您已经有效地结构化了应用程序,那么应用程序逻辑不应该关心何时打开和关闭连接,因为这将被封装在数据库层中。当要求数据库层执行查询时,它首先会检查它是否有一个活动连接,如果没有,则创建一个。这样,您将避免打开从未使用过的连接,并且在应用程序和数据库代码之间也有很好的逻辑分离。
#2
2
Well, if you are using a class, the connection should be opened automatically when you instantiate the class, or when the first query is performed. If you never use the class, the connection wouldn't be opened. While it is good practice to close it when you don't need it, it doesn't hurt to let it be closed when the request thread dies.
如果您正在使用一个类,那么当您实例化这个类时,或者当执行第一个查询时,应该自动打开连接。如果从不使用类,则不会打开连接。虽然在不需要时关闭它是一种很好的实践,但是在请求线程终止时关闭它并没有什么坏处。
This can be bad if you don't have a resource limits set in your php.ini file, the request could possible live forever and never close the connection.
如果在php中没有设置资源限制,那么这种情况就很糟糕。ini文件,请求可能永远存在,永远不会关闭连接。
If you have a medium-to-high traffic site, you should be thinking about using mysql_pconnect anyways so there is always a connection open and you don't need the overhead of opening one on every request.
如果您有一个中到高通信量的站点,您应该考虑使用mysql_pconnect的任何方式,这样就总有一个连接是打开的,并且您不需要在每个请求上打开一个连接的开销。
#3
1
Usually you'd only want to open a connection to your database when you need to use that connection. Keeping connections open can increase the chance that part of your code will accidentally, or maliciously through the actions of others, cause unwanted queries to be performed on the database.
通常,您只希望在需要使用该连接时打开到数据库的连接。保持连接打开可以增加您的部分代码意外或恶意地通过其他人的操作导致在数据库上执行不需要的查询的可能性。
That being the case, you should only open the connection before you want to run your queries. If you have a large number of queries, try to open your connection as late in the process as possible.
在这种情况下,您应该只在想要运行查询之前打开连接。如果您有大量查询,请尝试在过程中尽可能晚地打开连接。
It is better to have one connection left open for a longer duration than to open and close multiple connections.
最好让一个连接保持长时间打开,而不是打开和关闭多个连接。
#4
0
If your code is performance-sensitive, then the preferred technique tends to be to use some form of connection pooling and/or persistent processes so that you can open one database connection and then use that connection to service many page requests rather than opening a new connection for each request that needs one.
如果你的代码性能敏感,那么首选技术往往是使用某种形式的连接池和/或持续的过程,这样您就可以打开一个数据库连接,然后使用该连接服务许多页面请求而不是打开一个新连接的每个请求需要一个。
If your code is not performance-sensitive, then it doesn't really matter anyhow.
如果您的代码不是性能敏感的,那么它实际上并不重要。
Either way, the exact timing of when the database is accessed in the course of handling a specific request isn't that great of a cause for concern.
不管怎样,在处理特定请求的过程中访问数据库的确切时间并不是那么值得关注。
My personal practice is to open a database connection immediately when a new handler process is spawned, then verify that it's still alive when I start processing each request. The rest of the code is then free to just assume that the connection is available when needed without incurring the cost of connecting while a user is waiting for a response.
我的个人实践是,当一个新的处理程序生成时,立即打开一个数据库连接,然后在我开始处理每个请求时验证它是否还活着。然后,剩下的代码就可以*地假设在需要时连接是可用的,而不会在用户等待响应时产生连接成本。
#1
13
Because connections are rather expensive, as others have pointed out, I'd recommend using a "lazy connect" technique in your database layer. If you have structured your application effectively, your application logic should not be concerned with when connections are opened and closed as this would be encapsulated in the database layer. The database layer, when asked to perform a query, would first check to see if it has an active connection and if not, create one. This way you'll avoid opening connections that are never used and you'll also have a nice separation of logic between your application and the database code.
因为连接非常昂贵,正如其他人指出的那样,我建议在数据库层中使用“惰性连接”技术。如果您已经有效地结构化了应用程序,那么应用程序逻辑不应该关心何时打开和关闭连接,因为这将被封装在数据库层中。当要求数据库层执行查询时,它首先会检查它是否有一个活动连接,如果没有,则创建一个。这样,您将避免打开从未使用过的连接,并且在应用程序和数据库代码之间也有很好的逻辑分离。
#2
2
Well, if you are using a class, the connection should be opened automatically when you instantiate the class, or when the first query is performed. If you never use the class, the connection wouldn't be opened. While it is good practice to close it when you don't need it, it doesn't hurt to let it be closed when the request thread dies.
如果您正在使用一个类,那么当您实例化这个类时,或者当执行第一个查询时,应该自动打开连接。如果从不使用类,则不会打开连接。虽然在不需要时关闭它是一种很好的实践,但是在请求线程终止时关闭它并没有什么坏处。
This can be bad if you don't have a resource limits set in your php.ini file, the request could possible live forever and never close the connection.
如果在php中没有设置资源限制,那么这种情况就很糟糕。ini文件,请求可能永远存在,永远不会关闭连接。
If you have a medium-to-high traffic site, you should be thinking about using mysql_pconnect anyways so there is always a connection open and you don't need the overhead of opening one on every request.
如果您有一个中到高通信量的站点,您应该考虑使用mysql_pconnect的任何方式,这样就总有一个连接是打开的,并且您不需要在每个请求上打开一个连接的开销。
#3
1
Usually you'd only want to open a connection to your database when you need to use that connection. Keeping connections open can increase the chance that part of your code will accidentally, or maliciously through the actions of others, cause unwanted queries to be performed on the database.
通常,您只希望在需要使用该连接时打开到数据库的连接。保持连接打开可以增加您的部分代码意外或恶意地通过其他人的操作导致在数据库上执行不需要的查询的可能性。
That being the case, you should only open the connection before you want to run your queries. If you have a large number of queries, try to open your connection as late in the process as possible.
在这种情况下,您应该只在想要运行查询之前打开连接。如果您有大量查询,请尝试在过程中尽可能晚地打开连接。
It is better to have one connection left open for a longer duration than to open and close multiple connections.
最好让一个连接保持长时间打开,而不是打开和关闭多个连接。
#4
0
If your code is performance-sensitive, then the preferred technique tends to be to use some form of connection pooling and/or persistent processes so that you can open one database connection and then use that connection to service many page requests rather than opening a new connection for each request that needs one.
如果你的代码性能敏感,那么首选技术往往是使用某种形式的连接池和/或持续的过程,这样您就可以打开一个数据库连接,然后使用该连接服务许多页面请求而不是打开一个新连接的每个请求需要一个。
If your code is not performance-sensitive, then it doesn't really matter anyhow.
如果您的代码不是性能敏感的,那么它实际上并不重要。
Either way, the exact timing of when the database is accessed in the course of handling a specific request isn't that great of a cause for concern.
不管怎样,在处理特定请求的过程中访问数据库的确切时间并不是那么值得关注。
My personal practice is to open a database connection immediately when a new handler process is spawned, then verify that it's still alive when I start processing each request. The rest of the code is then free to just assume that the connection is available when needed without incurring the cost of connecting while a user is waiting for a response.
我的个人实践是,当一个新的处理程序生成时,立即打开一个数据库连接,然后在我开始处理每个请求时验证它是否还活着。然后,剩下的代码就可以*地假设在需要时连接是可用的,而不会在用户等待响应时产生连接成本。