Prevent CakePHP from automatically connecting to database when instantiating model

后端 未结 2 1111
旧巷少年郎
旧巷少年郎 2021-02-11 02:40

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

相关标签:
2条回答
  • 2021-02-11 02:49

    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 )

    0 讨论(0)
  • 2021-02-11 03:05

    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

    0 讨论(0)
提交回复
热议问题