ZF-12008: Adding a dbAdapter into session (for faster load) causes exception

Issue Type: Bug Created: 2012-01-17T17:45:35.000+0000 Last Updated: 2012-01-17T18:03:46.000+0000 Status: Resolved Fix version(s): Reporter: Mathieu Decaffmeyer (mathi) Assignee: Matthew Weier O'Phinney (matthew) Tags: - Zend_Db

Related issues: Attachments:


I add the dbAdapter into session to avoid initializing it for every request. I have the code below in my bootstrap file

if(empty($ns->dbAdapter)) { $ns = new Zend_Session_Namespace('test'); $ns->dbAdapter = Zend_Db::factory($config->db->adapter, $config->db->config->toArray()); } $ns->dbAdapter->setFetchMode(Zend_Db::FETCH_OBJ); Zend_Db_Table::setDefaultAdapter($ns->dbAdapter);

Sometimes (about 1 click out of 10), an exception is thrown when I start a new request:

You cannot serialize or unserialize PDO instances

  1. File: ========

C:\Data\websites\xyz\library\Zend\Session.php Line: 480


Posted by Matthew Weier O'Phinney (matthew) on 2012-01-17T18:03:37.000+0000

This is unreliable, and not recommended. It's a limitation (or feature!) of PHP: each new request has its own scope. While you can serialize a number of objects to sessions, PHP resources -- such as file handles or database connections -- can NOT be serialized. You will need to re-initialize such resources in each new request.

Some databases support "persistent connections". In such cases, if you connect to the database and request a persistent connection, the adapter will attempt to re-use a persistent connection if available. This feature varies widely in both availability and reliability between DB adapters that PHP supports, however, so YMMV.

Have you found an issue?

See the Overview section for more details.


© 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.