【发布时间】:2011-02-03 09:01:48
【问题描述】:
我有一个连接到多个数据库(Oracle、MySQL 和 MSSQL)的脚本,每次脚本运行时可能不会使用每个数据库连接,但都可以在单个脚本执行中使用。我的问题是,“最好在脚本开头连接一次所有数据库,即使所有连接都可能不使用。还是根据需要连接到它们更好,唯一的问题是我需要在循环中进行连接调用(因此数据库连接在循环中 X 次是新的)。
是的示例代码 #1:
// Connections at the beginning of the script
$dbh_oracle = connect2db();
$dbh_mysql = connect2db();
$dbh_mssql = connect2db();
for ($i=1; $i<=5; $i++) {
// NOTE: might not use all the connections
$rs = queryDb($query,$dbh_*); // $dbh can be any of the 3 connections
}
是的示例代码 #2:
// Connections in the loop
for ($i=1; $i<=5; $i++) {
// NOTE: Would use all the connections but connecting multiple times
$dbh_oracle = connect2db();
$dbh_mysql = connect2db();
$dbh_mssql = connect2db();
$rs_oracle = queryDb($query,$dbh_oracle);
$rs_mysql = queryDb($query,$dbh_mysql);
$rs_mssql = queryDb($query,$dbh_mssql);
}
现在我知道您可以使用持久连接,但是对于循环中的每个数据库也都打开一个连接吗?比如mysql_pconnect()、mssql_pconnect() 和adodb for Oracle persistent connection method。我知道持久连接也可能会占用资源,因为我正在寻找最佳性能/实践。
【问题讨论】:
标签: php loops persistence database-connection