根據(jù)GatewayWorker文檔中的描述:
DB屬于單例模式,在onXXX里初始化的數(shù)據(jù)庫單例只屬于當前子進程自己所有;
那么我是不是可以如下理解:
<?php
$worker = new BusinessWorker();
$worker->name = 'YourAppBusinessWorker';
$worker->count = 4;
$worker->registerAddress = '127.0.0.1:1238';
$worker->onWorkerStart = function()
{
Db::instance('db1');
};
$worker->onMessage = function()
{
$ret = Db->select('name,age')->from('users')->where('age>12')->query();
}
if(!defined('GLOBAL_START'))
{
Worker::runAll();
}
那么這樣做的話,我就有一個問題了,如果這里我需要進行2個數(shù)據(jù)庫實例怎么辦?下面這么做我感覺是不正確的:
<?php
$worker = new BusinessWorker();
$worker->name = 'YourAppBusinessWorker';
$worker->count = 4;
$worker->registerAddress = '127.0.0.1:1238';
$db1 = null;
$db2 = null;
$worker->onWorkerStart = function()
{
$db1 = Db::instance('db1');
$db2 = Db::instance('db2');
};
$worker->onMessage = function()
{
$ret1 = $db1->select('name,age')->from('users')->where('age>12')->query();
$ret2 = $db2->select('name,age')->from('users')->where('age>12')->query();
}
if(!defined('GLOBAL_START'))
{
Worker::runAll();
}
大概知道這么解決這個問題了,不知道這么做對不對,請老大審閱:
<?php
$worker = new BusinessWorker();
$worker->name = 'YourAppBusinessWorker';
$worker->count = 4;
$worker->registerAddress = '127.0.0.1:1238';
$worker->onWorkerStart = function()
{
Db::instance('db1');
Db::instance('db2');
};
$worker->onMessage = function()
{
Db::instance('db1')->select('name,age')->from('users')->where('age>12')->query();
Db::instance('db1')->select('name,age')->from('users')->where('age>12')->query();
}