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
Maybe you could use the callbacks in the AppModel for this.
I'd guess that you could use beforeFind
and beforeSafe
to make a connection to the database and then use afterFind
and afterSafe
to kill your connection.
As for a 'right' way of opening and closing database connections using core Cake functionality, I'm not sure but Costa's answer seems like a good (and clean!) plan.
(1) http://book.cakephp.org/1.3/en/view/922/Database-Configuration (Link stopped working, guess you should look here now: http://book.cakephp.org/1.3/en/The-Manual/Developing-with-CakePHP/Configuration.html )
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