Amal Raghav

Design. Develop. Destroy.

Symfony2 - Using PDO for Session Storage

Using the File System for session storage has it's limitations, especially the session locking. One of the approaches to solve this is to use database for session storage. Symfony2 comes with a built in PdoSessionStorage. In this brief tutorial you will see how you can switch to PDO for session storage.

First we need to create a table to store the session values. You can use the SQL given below to get started.

Next we have to create a service for using PdoSessionStorage. Add the following to your app/config/config.yml.

That's it! You should be able to use database as your session storage now. This is yet another showcase of the power of Symfony2's DIC!

UPDATE: Using DBAL connection

Most of the time you just want to reuse the DBAL connection for session storage. This too is pretty easy with the power of DI!

Copyright © 2013 Amal Raghav