ZF-12485: Writing session to table on request close fails over multiple servers

Description

Scenario: Same user, same browser, sends multiple ajax requests to the server at the same time. Requests are being balanced by a load balancer, and sent to multiple servers, with the same database as storage.

Error: "PHP Fatal Error: Exception thrown without a stack frame in Unknown on line 0" - very helpful of course

Issue: The code that finds the session in the database and updates or inserts it is not doing this in one go.


$rows = call_user_func_array(array(&$this, 'find'), $this->_getPrimary($id));

if (count($rows)) {
   $data[$this->_lifetimeColumn] = $this->_getLifetime($rows->current());

   if ($this->update($data, $this->_getPrimary($id, self::PRIMARY_TYPE_WHERECLAUSE))) {
       $return = true;
   }  
} else {
   $data[$this->_lifetimeColumn] = $this->_lifetime;

   if ($this->insert(array_merge($this->_getPrimary($id, self::PRIMARY_TYPE_ASSOC), $data))) {
       $return = true;
   }
}

Problem is, that anywhere between the find and insert, other servers might have already inserted that session, resulting this particular request to fail, with a real error message of: Duplicate entry on primary key "$session id".

My solution for the time being was to wrap this up in a database transaction, but all that code there should translate into something like:


$rows = $dbAdapter->findOrCreate($this->_getPrimary($id), $data);

and let the adapter(mysql, postgres, oracle, whatever) decide what the best method of inserting or updating is. For example, on mysql, most probably an INSERT () ON DUPLICATE KEY UPDATE would be the best solution.

PS: this is going on in Zend Framework 2 also.

Comments

Please also add your report in the bugtracker at Github (ZF2).

This issue has been closed on Jira and moved to GitHub for issue tracking. To continue following the resolution of this issues, please visit: https://github.com/zendframework/zf1/issues/41