DEV Community

Cover image for OpenSwoole and Doctrine

Posted on

OpenSwoole and Doctrine

Connection issue

The doctrinal issue is that coroutines use the same connection.

On a service with many connections, users end up waiting for SQL queries to finish one after the other.


1) Create a connection for each request

This solution has been adopted in the package diego-ninja/swoole-mysql-doctrine-driver : diego-ninja/swoole-mysql-doctrine-driver

Two issues arise:

  • We are in a long-time process, and connections consume RAM.
  • Under heavy load, database server may block due to a large number of open connections.

2) Use a connection pool

The connection pool is a class replacing native driver that store and reuse connections. You can define the max number of concurent connections, determined by the size of your database server.

The process is simple :

For example : we use a pool of 10 connections.

If there is a free connection, we get the connection. Send the connection and release the connection.

If there is no free connections, we sleep a short time and retry.


This package use small/swoole-patterns abstract pool's class.

Here is an example to configure it with symfony :

        driver: Small\SwooleDbalPdo\PDODriver
        url: '%env(resolve:DATABASE_URL)%'
Enter fullscreen mode Exit fullscreen mode

The source code is available on

The composer package is available on

Top comments (0)