ConnectionResolver
class ConnectionResolver implements ConnectionResolverInterface
Resolves database connections from a connection pool.
Retain logical connections while allowing idle physical sessions to be returned early.
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 ConnectionLease|PooledConnection> | $nonCoroutineConnections | Pooled wrappers retained by non-coroutine task execution. |
Methods
Get a database connection instance.
Get the current execution's connection without borrowing a pool slot.
Check the current connection's transaction state without borrowing a pool slot.
Get the query grammar date format of a connection without holding one.
Return idle physical sessions owned by the current execution.
Release connections retained by non-coroutine task execution.
Discard connections retained by non-coroutine task execution.
Get the default connection name.
Set the default connection name for the current execution context.
Get the connection name a request resolves to: the given name, or the current default.
Get the context key for storing a connection.
Get the context key for the current logical connection's lease.
Discard a failed connection owner while preserving cancellation precedence.
Details
at line 55
__construct(Container $container)
Create a new connection resolver instance.
at line 68
Connection
connection(UnitEnum|string|null $name = null)
Get a database connection instance.
Store a stable logical connection in the current coroutine's context. The current physical lease is settled when the coroutine ends.
at line 167
Connection|null
getHeldConnection(UnitEnum|string|null $name = null)
Get the current execution's connection without borrowing a pool slot.
at line 201
bool
hasApplicableTransaction(UnitEnum|string|null $name = null)
Check the current connection's transaction state without borrowing a pool slot.
at line 216
string
connectionDateFormat(UnitEnum|string|null $name = null)
Get the query grammar date format of a connection without holding one.
A connection the current coroutine holds answers for itself. Otherwise the pool's recorded driver format is used. A cold lookup resolves the connection as usual, then returns only its idle physical session; connections without session leases retain their normal ownership.
at line 259
static void
releaseIdleConnections()
Return idle physical sessions owned by the current execution.
at line 273
void
releaseConnections()
| internal |
Release connections retained by non-coroutine task execution.
at line 287
void
discardConnections()
| internal |
Discard connections retained by non-coroutine task execution.
at line 303
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.
at line 315
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.
at line 327
protected string
connectionName(UnitEnum|string|null $name)
Get the connection name a request resolves to: the given name, or the current default.
at line 339
protected string
getContextKey(string $name)
Get the context key for storing a connection.
at line 347
protected string
getLeaseContextKey(string $name)
Get the context key for the current logical connection's lease.
at line 355
protected void
discardFailedConnection(ConnectionLease|PooledConnection $owner, Throwable $exception)
Discard a failed connection owner while preserving cancellation precedence.
at line 371
protected void
terminateConnections(Closure $terminate)
Detach and terminate retained non-coroutine connections.