class ConnectionResolver implements ConnectionResolverInterface

Resolves database connections from a connection pool.

Retains pooled wrappers until their owning coroutine or task ends.

Constants

DEFAULT_CONNECTION_CONTEXT_KEY

Context key for per-coroutine default connection override.

Shared with DatabaseManager::usingConnection() to ensure all access paths respect the override.

Properties

protected string|null $default

The config-derived default connection name, captured at construction.

protected PoolManager $poolManager
protected PooledConnection> $nonCoroutineConnections

Pooled wrappers retained by non-coroutine task execution.

Methods

__construct(Container $container)

Create a new connection resolver instance.

connection(UnitEnum|string|null $name = null)

Get a database connection instance.

void
releaseConnections()

Release connections retained by non-coroutine task execution.

void
discardConnections()

Discard connections retained by non-coroutine task execution.

string|null
getDefaultConnection()

Get the default connection name.

void
setDefaultConnection(string|null $name)

Set the default connection name for the current execution context.

string
getContextKey(string $name)

Get the context key for storing a connection.

void
discardFailedConnection(PooledConnection $pooledConnection, Throwable $exception)

Discard a failed connection while preserving cancellation precedence.

void
terminateConnections(Closure $terminate)

Detach and terminate retained non-coroutine connections.

Details

at line 55
__construct(Container $container)

Create a new connection resolver instance.

Parameters

Container $container

at line 69
ConnectionInterface connection(UnitEnum|string|null $name = null)

Get a database connection instance.

The connection is retrieved from a pool and stored in the current coroutine's context. When the coroutine ends, the connection is automatically released back to the pool.

Parameters

UnitEnum|string|null $name

Return Value

ConnectionInterface

at line 159
void releaseConnections()

internal  
 

Release connections retained by non-coroutine task execution.

Return Value

void

at line 173
void discardConnections()

internal  
 

Discard connections retained by non-coroutine task execution.

Return Value

void

at line 189
string|null getDefaultConnection()

Get the default connection name.

Checks Context first for per-coroutine override (from setDefaultConnection() or DatabaseManager::usingConnection()), then falls back to the config-derived default captured at construction.

Return Value

string|null

at line 201
void setDefaultConnection(string|null $name)

Set the default connection name for the current execution context.

Writes to coroutine Context so concurrent requests in the same Swoole worker are not affected. A null value clears the override and getDefaultConnection() falls back to the config-derived default.

Parameters

string|null $name

Return Value

void

at line 213
protected string getContextKey(string $name)

Get the context key for storing a connection.

Parameters

string $name

Return Value

string

at line 221
protected void discardFailedConnection(PooledConnection $pooledConnection, Throwable $exception)

Discard a failed connection while preserving cancellation precedence.

Parameters

PooledConnection $pooledConnection
Throwable $exception

Return Value

void

at line 237
protected void terminateConnections(Closure $terminate)

Detach and terminate retained non-coroutine connections.

Parameters

Closure $terminate

Return Value

void