質問

I have following database configuration in database.php file from my CakePHP app:

    public $default = array(
            'datasource' => 'Database/Mysql',
            'persistent' => false,
            'host' => 'localhost',
            'login' => 'root',
            'password' => '',
            'database' => 'database',
            'prefix' => '',
    );

All is working fine, except of one queue shell script. The script is looping and waiting for commands to run (for example to update some reports). After a while 1-2 days database data is changing, but the script will still "see" the old data, and results of command is wrong. If I restart shell script, the results are OK... for few days.

I have to mention that I had "lost database connection" issue before in the script and I have solved it by runing every 10-15 min:

 $user = $this->User->find('first');

Now I am affraid this is making the connection persistent somehow...

How can I reset the database connection ?

EDIT: I was just refactoring the code to check if I can set $cacheQueries to false on the Model. But in few parts of the code I am using ConnectionManager directly, and only then I have "cache" problem. If I query database from Model->find results are ok. I need direct queries for performance reasons in few places...

  $query = "SELECT COUNT(1) as result
            FROM 
            ......
            ";

$db = ConnectionManager::getDataSource('default');
$result = $db->query($query);
役に立ちましたか?

解決

The property $cacheQueries which @burzum mentioned, seems not to be in use in any cake model method.

But I found another interesting fact in the source of the DboSource.

You need to use the second parameter of the DboSource::query() method to turn off the caching. Or the third if you want to provide additional parameters for the DboSource::fetchAll() method.

Eventhough this will fix your problem, you should write your queries with the Model::find() method that CakePHP offers.

You should only not use them if they are seriously impacting your performance.

func0der

他のヒント

Try to set these two model properties to false:

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top