Using Swoole with Yii2 to handle database connections efficiently requires some care because Yii2's default DB connection is designed for a traditional PHP request lifecycle (one request = one process, connection opened and closed per request). Swoole, on the other hand, is a long-running process, so you want to avoid reconnecting to the database on every request and also avoid sharing the same DB connection instance across multiple coroutines or requests without proper handling.
Here’s a general approach to integrate Yii2 DB connection with Swoole:
1. Understand the problem
- Yii2 DB connection is typically created per request.
- Swoole runs as a long-lived process.
- You want to reuse DB connections efficiently without reconnecting every time.
- You must avoid sharing the same DB connection instance across coroutines because it’s not coroutine-safe.
2. Use Swoole Coroutine MySQL client or Yii2 DB with coroutine support
- Swoole provides a coroutine MySQL client (
Swoole\Coroutine\MySQL) which is coroutine-safe.
- Yii2’s default
yii\db\Connection uses PDO, which is not coroutine-safe.
- You can either:
- Use Swoole coroutine MySQL client directly in your Swoole server code.
- Or, use Yii2 DB connection but reconnect per coroutine or use a connection pool.
3. Recommended approach: Use Yii2 DB connection with Swoole and reconnect per coroutine
You can override the DB connection component to reconnect on each coroutine start.
Example:
// In your Yii2 config or bootstrap file for Swoole
use Swoole\Coroutine;
Yii::$app->db->close(); // Close any existing connection
// Hook into Swoole coroutine lifecycle
Swoole\Runtime::enableCoroutine();
go(function () {
// This runs inside a coroutine
Yii::$app->db->close(); // Close previous connection if any
Yii::$app->db->open(); // Open a new connection for this coroutine
// Now you can safely use Yii::$app->db in this coroutine
$command = Yii::$app->db->createCommand('SELECT * FROM user');
$users = $command->queryAll();
var_dump($users);
});
4. Using connection pool (advanced)
For better performance, you can implement a connection pool that manages multiple DB connections and assigns them to coroutines on demand.
There are some third-party libraries or you can implement a simple pool yourself:
- Create a pool of DB connections.
- When a coroutine needs DB access, it borrows a connection from the pool.
- After the coroutine finishes, it returns the connection to the pool.
5. Example minimal Swoole server with Yii2 DB connection
use Swoole\Http\Server;
$server = new Server("127.0.0.1", 9501);
$server->on("request", function ($request, $response) {
// Reconnect DB for each request (each request runs in a coroutine)
Yii::$app->db->close();
Yii::$app->db->open();
$users = Yii::$app->db->createCommand('SELECT * FROM user')->queryAll();
$response->header("Content-Type", "application/json");
$response->end(json_encode($users));
});
$server->start();
Summary
- Do not share a single DB connection instance across coroutines.
- Close and reopen DB connection inside each coroutine or request.
- Consider using Swoole coroutine MySQL client for better coroutine support.
- For production, consider implementing or using a connection pool.
If you want, I can help you with a more detailed example or a connection pool implementation!