Issues

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

Issue Type: Bug Created: 2012-12-07T08:45:42.000+0000 Last Updated: 2013-04-05T16:07:06.000+0000 Status: Closed Fix version(s): Reporter: Cosmin Budrica (cosmin.budrica) Assignee: Ralph Schindler (ralph) Tags: - Zend_Session

  • Session

Related issues: Attachments:

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.

<pre class="highlight">
$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:

<pre class="highlight">
$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

Posted by Frank Br├╝ckner (frosch) on 2012-12-07T09:12:13.000+0000

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

Posted by Ralph Schindler (ralph) on 2013-04-05T16:07:06.000+0000

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

Have you found an issue?

See the Overview section for more details.

Copyright

© 2006-2016 by Zend, a Rogue Wave Company. Made with by awesome contributors.

This website is built using zend-expressive and it runs on PHP 7.

Contacts