防止CakePHP在实例化模型时自动连接到数据库 [英] Prevent CakePHP from automatically connecting to database when instantiating model

查看:187
本文介绍了防止CakePHP在实例化模型时自动连接到数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我公司的DBA有一个脚本,可以自动杀死长时间运行的数据库连接并查询我们的生产数据库。我编写了一个CakePHP Shell应用程序,它在循环中运行,类似于守护程序,需要定期进行数据库读取和写入。如果运行时间太长,数据库连接将被维护脚本和我的应用程序错误关闭。



查看CakePHP源代码,看起来像是当模型实例化,它会自动尝试连接到相应的数据库。

解决方案

也许你可以手动连接/断开连接当你需要吗?



DboSource有很多方法可供你使用。这里有一些可能有用的函数:

  $ db = ConnectionManager :: getDataSource('local'); 

$ isconnected = $ db-> isConnected(); //是连接打开?
$ db-> close(); //关闭连接
$ db-> reconnect(); //重新连接到db


更多方法列在 DboSource API文档


The DBA at my company has a script that automatically kills long-running database connections and queries on our production databases. I've written a CakePHP Shell application that runs in a loop, sort of like a daemon, that periodically needs to make database reads and writes. If it runs for too long, the database connection is closed by the maintenance script and my app errors out.

Looking at the CakePHP source, it seems like when a model is instantiated, it automatically tries to connect to the appropriate database. Is there a way to connect to the database only when making a query, then disconnect?

解决方案

Maybe you can manually connect/disconnect when you need to?

DboSource has lots of methods for you to play with. Here's a list of functions that may be useful:

$db = ConnectionManager::getDataSource('local');

$isconnected = $db->isConnected();  //is the connection open?
$db->close();  //close the connection
$db->reconnect();  //reconnect to the db

More methods are listed in the DboSource API docs

这篇关于防止CakePHP在实例化模型时自动连接到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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