title: Sharing connection to database between DBAL and session handler Symfony2
body: <p>I'm trying to use same connection to my database between session handler and doctrine dbal:</p>
  
  <p>config.yml</p>
  
  <pre><code>framework:
      session:
          handler_id:  session.handler.one_connection_pdo
  </code></pre>
  
  <p>services.yml</p>
  
  <pre><code>session.handler.one_connection_pdo:
      class:     AppBundle\Session\OneConnectionPdoHandler
      public:    false
      arguments:
          - "@database_connection"
          - []
  </code></pre>
  
  <p>AppBundle/Session/OneConnectionPdoHandler.php</p>
  
  <pre><code>namespace AppBundle\Session;
  
  
  use Doctrine\DBAL\Connection;
  use Symfony\Component\HttpFoundation\Session\Storage\Handler\PdoSessionHandler;
  
  class OneConnectionPdoHandler extends PdoSessionHandler
  {
  
      public function __construct($pdoOrDsn, array $options)
      {
          if ($pdoOrDsn instanceof Connection) {
              $pdoOrDsn = $pdoOrDsn-&gt;getWrappedConnection();
          }
          parent::__construct($pdoOrDsn, $options);
      }
  
  }
  </code></pre>
  
  <p>Everything seems to work when browsing application but I can't update any entity because I get error: </p>
  
  <pre><code>PDOException: There is already an active transaction
  at n/a
      in .../vendor/doctrine/dbal/lib/Doctrine/DBAL/Connection.php line 1176
  
  at PDO-&gt;beginTransaction()
      in .../vendor/doctrine/dbal/lib/Doctrine/DBAL/Connection.php line 1176
  
  at Doctrine\DBAL\Connection-&gt;beginTransaction()
      in .../vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php line 373
  
  at Doctrine\ORM\UnitOfWork-&gt;commit(null)
      in .../vendor/doctrine/orm/lib/Doctrine/ORM/EntityManager.php line 356
  
  at Doctrine\ORM\EntityManager-&gt;flush()
      in .../src/AppBundle/Controller/Admin/DistributorsController.php line 66
  
  at AppBundle\Controller\Admin\DistributorsController-&gt;editAction(object(Distributor), object(Request))
      in  line 
  
  at call_user_func_array(array(object(DistributorsController), 'editAction'), array(object(Distributor), object(Request)))
      in .../vendor/symfony/symfony/src/Symfony/Component/HttpKernel/HttpKernel.php line 139
  
  at Symfony\Component\HttpKernel\HttpKernel-&gt;handleRaw(object(Request), '1')
      in .../vendor/symfony/symfony/src/Symfony/Component/HttpKernel/HttpKernel.php line 62
  
  at Symfony\Component\HttpKernel\HttpKernel-&gt;handle(object(Request), '1', true)
      in .../vendor/symfony/symfony/src/Symfony/Component/HttpKernel/Kernel.php line 169
  
  at Symfony\Component\HttpKernel\Kernel-&gt;handle(object(Request))
      in .../web/app_dev.php line 31
  </code></pre>
  
  <p>Is there a way to share connection between doctrine dbal and custom pdo handler?</p>
  
  <p><strong>// EDIT</strong></p>
  
  <p>I finally found a solution inside <code>PdoSessionHandler</code> class. </p>
  
  <p>By default PDO handler uses transaction when reading and writing to session. It start transaction on first <code>read()</code> and commit on <code>close()</code>. In between there were some database operation <code>$em-&gt;persist($entity); $em-&gt;flush()</code> which spawned another transaction which generated error. </p>
  
  <p>Inside <code>PdoSessionHandler</code> class I found that there is a <code>lock_mode</code> option which can be set like this:</p>
  
  <pre><code>session.handler.one_connection_pdo:
      class:     AppBundle\Session\OneConnectionPdoHandler
      public:    false
      arguments:
          - "@database_connection"
          - { lock_mode: 1 }
  </code></pre>
  
  <p>When lock_mode is set to 1 (<code>PdoSessionHandler::LOCK_ADVISORY</code>) PDO handler will use advisory lock instead of transaction and there will be no more transaction errors.</p>