PHP数据库连接实践

发布于 2024-08-27 12:50:32 字数 1520 浏览 5 评论 0原文

我有一个连接到多个数据库(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()< /a>、mssql_pconnect()adodb for Oracle持久连接方法。我知道持久连接也可能会占用资源,正如我所言正在寻找最佳性能/实践。

这是一篇关于 为什么持久连接可能会导致问题

I have a script that connects to multiple databases (Oracle, MySQL and MSSQL), each database connection might not be used each time the script runs but all could be used in a single script execution. My question is, "Is it better to connect to all the databases once in the beginning of the script even though all the connections might not be used. Or is it better to connect to them as needed, the only catch is that I would need to have the connection call in a loop (so the database connection would be new for X amount of times in the loop).

Yeah Example Code #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
}

Yeah Example Code #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);
}

now I know you could use a persistent connection but would that be one connection open for each database in the loop as well? Like mysql_pconnect(), mssql_pconnect() and adodb for Oracle persistent connection method. I know that persistent connection can also be resource hogs and as I'm looking for best performance/practice.

Here is a good post on why persistent connections could cause problems

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

走走停停 2024-09-03 12:50:32

使用惰性连接包装类:

class Connection
{
    private $pdo;
    private $dsn;

    public __construct($dsn)
    {
        $this->dsn = $dsn;
    }

    public query($sql)
    {
        //the connection will get established here if it hasn't been already
        if (is_null($this->pdo))
            $this->pdo = new PDO($this->dsn);

        //use pdo to do a query here

    }
}

我希望您已经在使用 PDO。如果没有,你应该是。 PDO 是独立于数据库的。如果您使用过程函数执行此操作,则必须为每种数据库类型创建一个新类。

无论如何,这只是一个框架(例如,您希望在 query() 中添加一个 $params 选项),但您应该能够明白这个想法。仅当您调用 query() 时才会尝试连接。构造对象并不建立连接。

顺便说一句,考虑使用 Doctrine。它具有惰性连接,总体上使生活更轻松。

Use a lazy connection wrapper class:

class Connection
{
    private $pdo;
    private $dsn;

    public __construct($dsn)
    {
        $this->dsn = $dsn;
    }

    public query($sql)
    {
        //the connection will get established here if it hasn't been already
        if (is_null($this->pdo))
            $this->pdo = new PDO($this->dsn);

        //use pdo to do a query here

    }
}

I hope you're already using PDO. If not, you should be. PDO is database independent. If you did this using procedural functions, you'd have to create a new class for each database type.

Anyways, this is just a skeleton (you'd want to add a $params option in query(), for example), but you should be able to get the idea. The connection is only attempted when you call query(). Constructing the object does not make a connection.

As an aside, consider using Doctrine. It has lazy connections and makes life easier in general.

新一帅帅 2024-09-03 12:50:32

最佳性能/实践规则很简单:仅连接到一个数据库。

至于连接 - 尝试实现一些数据库访问类。可以按需自动连接。

The best performance/practice rule is simple: do connect to one database only.

As for the connects - try to implement some database access class. Which can connect automatically on demand.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文