PHP PDO:多个对象扩展dbwrapper;有多少 MySQL 连接?
情况:
- 我使用数据库类作为包装器(dbwrapper)通过 PHP PDO 打开和关闭数据库连接
- 我有 3 个独立的类扩展此数据库包装器
- 我从所述类实例化 3 个对象并在在它们存在的过程中,它们各自进行
- 本例中使用的 MySQL 数据库查询。
当这 3 个类的方法需要数据库访问时,它们各自在内部创建 PDO 对象,并使用它们扩展的 dbwrapper。每个对象都将其 PDO 对象存储在成员/字段中以供自身引用。
问题:我的问题是……每个对象是否都创建一个单独的数据库连接,总共有 3 个吗? Apache 的每个客户端是否只创建一个到数据库的连接,或者是在 php 应用程序中使用数据库的每个对象都创建一个连接。 (听起来效率很低!)
原因:我想了解如何处理这些连接。
我试图决定是否让每个类扩展 dbwrapper 更好,或者是否最好在不自动连接数据库的情况下初始化 dbwrapper,并在需要时进行处理。相反,我会在初始化时将 dbwrapper 传递给每个对象构造函数...让它们使用该引用。如果发生多个数据库连接,那么我认为这将是最小化开销同时克服对象范围和访问问题的最佳方法。
提前致谢!
The Situation:
- I am using a db class as a wrapper (dbwrapper) to open and close db connections via PHP PDO
- I have 3 separate classes which extend this db wrapper
- I instantiate 3 objects from said classes and in the course of their existence they each make db queries
- MySQL used in this case.
When the methods of the 3 classes require db access they each create PDO objects internally, making use of the dbwrapper which they extended. Each object is storing its PDO object in a member/field for reference by itself.
The Question: My question is this... is each object creating a separate connection to the database, making 3 in total? Is each client of Apache creating only one connection to the database or is each object using the db in a php application creating a connection. (sounds inefficient!)
The Reason: I would like to understand how these connections are handled.
I am trying to decide if it would be better to have each class extend the dbwrapper or if it would be better to initialize the dbwrapper without making an automatic connection the db, handle that when its needed. Instead I would pass the dbwrapper to each objects constructor as they are initialized... letting them use that reference. If multiple db connections are happening then I think this would be the best way to minimize overhead while overcoming issues of object's scope and access.
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
或许。我不知道,但是以下是如何找到答案。当您的脚本运行时,通过您选择的客户端(如命令行)连接到 MySQL 并发出命令
SHOW PROCESSLIST;
您将获得活动连接的列表。您可能需要在脚本中插入一个
sleep
,以使其保持足够长的活动时间,以便在实例化并处理所有三个对象时运行进程列表命令。您将看到一个或三个连接。然后你就会得到答案。
(答案将根据底层驱动程序而有所不同。如果 DSN 相同,某些驱动程序将重用该连接。)
这是常见的做法。数据库句柄也是 Singleton 模式的主要候选者。
Maybe. I don't know, but here's how to find out. While your script is running, connect to MySQL via your client of choice (like the command line) and issue the command
SHOW PROCESSLIST;
You'll get a list of active connections.You might need to insert a
sleep
in your script to keep it alive long enough for you to run the process list command when you've instantiated and are working on all three objects.You'll see either one connection or three. Then you'll have your answer.
(The answer will vary depending on the underlying driver. Some drivers will reuse the connection if the DSN is identical.)
This is the common practice. Database handles are prime candidates for the Singleton pattern as well.