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.

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.