Zend DB如何管理数据库连接 [英] How Zend DB Manage Database Connections

查看:108
本文介绍了Zend DB如何管理数据库连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Zend Framework进行PHP开发,这是我用来执行查询的一个小函数.这与错误无关.代码和一切正常.但是我想知道背后的一些概念.

I am using Zend Framework for my PHP developments and here is a small function I used to execute a query. This is not about an error. The code and everything works fine. But I want to know some concept behind this.

/** 
    * Get dataset by executing sql statement
    * 
    * @param  string $sql - SQL Statement to be executed
    * 
    * @return bool 
    */
    public function executeQuery($sql)
    {
        $this->sqlStatement = $sql;

        if ($this->isDebug)
        {
            echo $sql;
            exit;
        }

        $objSQL = $this->objDB->getAdapter()->prepare($sql);

        try
        {           
            return $objSQL->execute();

        }
        catch(Exception $error)
        {

            $this->logMessage($error->getMessage() . "  SQL : " .$sql);
            return false;
        }
        return false;
    }

贝for对我来说尚不清楚.

Bellow are unclear areas for me.

  1. Zend_Db_Table_Abstract如何维护数据库连接?
  2. 调用此函数时是否一直在建立新的连接?或者它是否具有一些连接池?
  3. 我没有编写任何代码来打开或关闭数据库连接.那么zend框架会自动关闭连接吗?
  4. 如果我执行此功能时此打开和关闭连接一直有效,那么是否存在性能问题?

谢谢,感谢您对此提出的建议和意见.

Thank you and appreciate your suggestions and opinion on this.

推荐答案

创建连接

创建Adapter类的实例不会立即连接到RDBMS服务器.适配器会保存连接参数,并根据需要(首次)执行实际连接,以执行查询.这样可以确保创建适配器对象既快速又便宜.即使不确定在应用程序正在服务的当前请求期间是否需要运行任何数据库查询,也可以创建适配器的实例.

Creating an instance of an Adapter class does not immediately connect to the RDBMS server. The Adapter saves the connection parameters, and makes the actual connection on demand, the first time you need to execute a query. This ensures that creating an Adapter object is quick and inexpensive. You can create an instance of an Adapter even if you are not certain that you need to run any database queries during the current request your application is serving.

如果需要强制适配器连接到RDBMS,请使用getConnection()方法.此方法返回由相应的PHP数据库扩展名表示的用于连接的对象.例如,如果将任何Adapter类用于PDO驱动程序,则getConnection()在将其作为到特定数据库的实时连接启动后将返回PDO对象.

If you need to force the Adapter to connect to the RDBMS, use the getConnection() method. This method returns an object for the connection as represented by the respective PHP database extension. For example, if you use any of the Adapter classes for PDO drivers, then getConnection() returns the PDO object, after initiating it as a live connection to the specific database.

如果您想捕获由于无效的帐户凭据或其他无法连接到RDBMS服务器而引发的异常,则强制连接很有用.在建立连接之前不会抛出这些异常,因此,如果您在一个地方(而不是在第一次查询数据库时)处理这些异常,则可以帮助简化应用程序代码.

It can be useful to force the connection if you want to catch any exceptions it throws as a result of invalid account credentials, or other failure to connect to the RDBMS server. These exceptions are not thrown until the connection is made, so it can help simplify your application code if you handle the exceptions in one place, instead of at the time of the first query against the database.

此外,适配器可以序列化以将其存储在例如会话变量中.这不仅对于适配器本身非常有用,而且对于聚集该适配器的其他对象(例如Zend_Db_Select对象)也非常有用.默认情况下,允许序列化适配器,如果不希望序列化,则应考虑将Zend_Db :: ALLOW_SERIALIZATION选项与FALSE一起传递,请参见上面的示例.为了遵守延迟连接原则,适配器在取消序列化后将不会重新连接自身.然后,您必须自己调用getConnection().您可以通过将Zend_Db :: AUTO_RECONNECT_ON_UNSERIALIZE传递为TRUE作为适配器选项来使适配器自动重新连接.

Additionally, an adapter can get serialized to store it, for example, in a session variable. This can be very useful not only for the adapter itself, but for other objects that aggregate it, like a Zend_Db_Select object. By default, adapters are allowed to be serialized, if you don't want it, you should consider passing the Zend_Db::ALLOW_SERIALIZATION option with FALSE, see the example above. To respect lazy connections principle, the adapter won't reconnect itself after being unserialized. You must then call getConnection() yourself. You can make the adapter auto-reconnect by passing the Zend_Db::AUTO_RECONNECT_ON_UNSERIALIZE with TRUE as an adapter option.

关闭连接

通常不需要关闭数据库连接. PHP自动清除所有资源并结束请求.数据库扩展被设计为在清理对资源对象的引用时关闭连接.

Normally it is not necessary to close a database connection. PHP automatically cleans up all resources and the end of a request. Database extensions are designed to close the connection as the reference to the resource object is cleaned up.

但是,如果您有一个长期的PHP脚本启动了许多数据库连接,则可能需要关闭该连接,以避免耗尽RDBMS服务器的容量.您可以使用适配器的closeConnection()方法来显式关闭基础数据库连接.

However, if you have a long-duration PHP script that initiates many database connections, you might need to close the connection, to avoid exhausting the capacity of your RDBMS server. You can use the Adapter's closeConnection() method to explicitly close the underlying database connection.

从1.7.2版开始,您可以使用isConnected()方法检查您当前是否连接到RDBMS服务器.这意味着连接资源已启动并且尚未关闭.该功能当前无法测试例如服务器端连接的关闭.这在内部用于关闭连接.它使您可以多次关闭连接而不会出现错误.对于PDO适配器,在1.7.2之前已经存在这种情况,而对于其他适配器则不是.

Since release 1.7.2, you could check you are currently connected to the RDBMS server with the method isConnected(). This means that a connection resource has been initiated and wasn't closed. This function is not currently able to test for example a server side closing of the connection. This is internally use to close the connection. It allow you to close the connection multiple times without errors. It was already the case before 1.7.2 for PDO adapters but not for the others.

更多信息

这篇关于Zend DB如何管理数据库连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆