Defer release must in coroutine environment.

This commit is contained in:
李铭昕 2019-03-13 11:56:46 +08:00
parent 0b84674140
commit 7834fdd471

View File

@ -16,6 +16,7 @@ use Hyperf\Database\ConnectionInterface;
use Hyperf\Database\ConnectionResolverInterface;
use Hyperf\DbConnection\Pool\PoolFactory;
use Hyperf\Utils\Context;
use Hyperf\Utils\Coroutine;
use Psr\Container\ContainerInterface;
class ConnectionResolver implements ConnectionResolverInterface
@ -65,9 +66,11 @@ class ConnectionResolver implements ConnectionResolverInterface
$pool = $this->factory->getPool($name);
$connection = $pool->get()->getConnection();
Context::set($id, $connection);
defer(function () use ($connection) {
$connection->release();
});
if (Coroutine::inCoroutine()) {
defer(function () use ($connection) {
$connection->release();
});
}
}
return $connection;