I have a php worker for gearman running from command line (through supervisord), that acts as a kind of "API". The worker uses mysqli connections to retrieve data from a DB and sends it back to a 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 from DB; fetch row; return serialized; */}
static function testB(){ /* select field2 from 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, I have e.g. two functions 'testA' and 'testB' that do certain selects from the db. The problem is that they share the same db connection, so if there are requests to both functions at the same moment, one could get results from "wrong" select i.e. testB fetches result from select that was called from testA and not testB.
Is there any way to avoid this, except by opening separate DB connections in each function?