mysqli - PHP gearman worker with shared DB connection -
i have php worker gearman running command line (through supervisord), acts kind of "api". worker uses mysqli connections retrieve data db , sends connected client.
class my_worker{ private $worker; static $sql; public function __construct($sql){ $this->worker = new gearmanworker(); $this->worker->addserver(); self::$sql = $sql; //register workers $this->worker->addfunction("testa", array($this, 'testa') ); $this->worker->addfunction("testb", array($this, 'testb') ); } public function run(){ while ($this->worker->work()); } static function testa(){ /* select field1 db; fetch row; return serialized; */} static function testb(){ /* select field2 db; fetch row; return serialized; */} } //start worker $sql = new db(sql_host, sql_user, sql_pwd, sql_db); // extends mysqli class $worker = new my_worker($sql); $worker->run(); }
so, have e.g. 2 functions 'testa' , 'testb' selects db. problem share same db connection, if there requests both functions @ same moment, 1 results "wrong" select i.e. testb fetches result select called testa , not testb.
is there way avoid this, except opening separate db connections in each function?
imho, queries handled in parallel , there shouldn't issues current setup. can imagine improved using pdo , transactions
Comments
Post a Comment