DatabaseManager
class DatabaseManager implements ConnectionResolverInterface mixin Connection
Traits
Constants
| QUERY_DURATION_HANDLERS_CONTEXT_KEY |
Context key for query duration handlers that have run in the current coroutine. |
Properties
| static protected array | $macros | The registered string macros. |
from Macroable |
| protected Connection> | $connections | The active connection instances. |
|
| protected Closure | $reconnector | The callback to be executed to reconnect to a database. |
|
| protected array<int, array{connection: string, key: string, threshold: float|int, handler: callable}> | $queryDurationHandlers | All registered query duration handlers. |
|
| protected bool | $queryDurationListenerRegistered | Indicates if the manager-level query duration listener has been registered. |
Methods
Get the number of seconds until the given DateTime.
Get the "available at" UNIX timestamp.
If the given value is an interval, convert it to a DateTime instance.
Given a start time, format the total run time for human readability.
Mix another object into the class.
Dynamically handle calls to the class.
Dynamically pass methods to the default connection.
Get a database connection instance.
Resolve a connection directly without using the connection pool.
Build a database connection instance from the given configuration.
Get a database connection instance from the given configuration.
Dispatch the ConnectionEstablished event if the event dispatcher is available.
Disconnect from the given database and flush its pool.
Disconnect from the given database.
Reconnect to the given database.
Set the default database connection for the callback execution.
Refresh the PDO connections on a given connection.
Get the default connection name.
Register a callback to be invoked when the current connection queries for longer than a given amount of time.
Allow all the query duration handlers to run again for the current connection.
Set the default connection name for the current execution context.
Get a config value.
Get the context key for storing a connection.
Get all coroutine/cache variants for a connection name.
Get the current effective connection name.
Register the manager-level query duration listener.
Get the coroutine-local key for a query duration handler registration.
Get all of the supported drivers.
Get all of the drivers that are actually available.
Register an extension connection resolver.
Remove an extension connection resolver.
Return all of the created connections.
Purge all connections on the current manager instance.
Set the database reconnector callback.
Flush all static state.
Details
in
InteractsWithTime at line 17
protected int
secondsUntil(DateInterval|DateTimeInterface|int $delay)
Get the number of seconds until the given DateTime.
in
InteractsWithTime at line 29
protected int
availableAt(DateInterval|DateTimeInterface|int|null $delay = 0)
Get the "available at" UNIX timestamp.
in
InteractsWithTime at line 41
protected DateTimeInterface|int
parseDateInterval(DateInterval|DateTimeInterface|int|null $delay)
If the given value is an interval, convert it to a DateTime instance.
in
InteractsWithTime at line 57
protected int
currentTime()
Get the current system time as a UNIX timestamp.
in
InteractsWithTime at line 65
protected string
runTimeForHumans(float $startTime, float|null $endTime = null)
Given a start time, format the total run time for human readability.
in
Macroable at line 28
static void
macro(string $name, callable|object $macro)
Register a custom macro.
Boot-only. Macros persist in a static property for the worker lifetime and apply to every subsequent call on the macroable class.
in
Macroable at line 41
static void
mixin(object $mixin, bool $replace = true)
Mix another object into the class.
Boot-only. Delegates to macro() for each method; registered macros persist in a static property for the worker lifetime.
in
Macroable at line 57
static bool
hasMacro(string $name)
Check if macro is registered.
in
Macroable at line 68
static void
flushMacros()
Flush the existing macros.
Boot or tests only. Clears worker-wide macros; concurrent coroutines may resolve different methods depending on timing.
in
Macroable at line 78
static mixed
__callStatic(string $method, array $parameters)
Dynamically handle calls to the class.
at line 736
mixed
__call(string $method, array $parameters)
Dynamically pass methods to the default connection.
at line 78
__construct(Container $app, ConnectionFactory $factory)
Create a new database manager instance.
at line 102
ConnectionInterface
connection(UnitEnum|string|null $name = null)
Get a database connection instance.
Delegates to ConnectionResolver for pooled, per-coroutine connection management. Resolves the default connection name here (checking Context for usingConnection override) before passing to the resolver.
at line 124
ConnectionInterface
resolveConnectionDirectly(string $name)
| internal | For use by SimpleConnectionResolver only |
Resolve a connection directly without using the connection pool.
This method is used by SimpleConnectionResolver for testing and Capsule environments where connection pooling is not needed. It manages connections in the $connections array like Laravel's original DatabaseManager.
at line 150
ConnectionInterface
build(array $config)
Build a database connection instance from the given configuration.
at line 163
ConnectionInterface
connectUsing(string $name, array $config, bool $force = false)
Get a database connection instance from the given configuration.
at line 174
protected Connection
makeConnection(ConnectionName|string $name)
Make the database connection instance.
at line 187
protected array
configuration(ConnectionName|string $name)
Get the configuration for a connection.
at line 212
protected Connection
configure(Connection $connection)
Prepare the database connection instance.
at line 233
protected void
dispatchConnectionEstablishedEvent(Connection $connection)
Dispatch the ConnectionEstablished event if the event dispatcher is available.
at line 257
void
purge(UnitEnum|string|null $name = null)
Disconnect from the given database and flush its pool.
In pooled mode, this disconnects the current coroutine's connection, clears its context key (so the next connection() call gets a fresh pooled connection), and flushes the pool. Use this when connection configuration has changed and you need to fully reset.
Boot or tests only. Flushes the shared pool used by every coroutine; concurrent coroutines holding pooled connections will release them back into a discarded pool, and the current coroutine may briefly hold two pooled connections (the old one releases via defer at coroutine end).
at line 306
void
disconnect(UnitEnum|string|null $name = null)
Disconnect from the given database.
In pooled mode, this nulls the PDOs on the current coroutine's connection (if one exists), forcing a reconnect on the next query. Does not clear context or affect the pool - the connection is still released at coroutine end.
In non-pooled mode (SimpleConnectionResolver), disconnects the connection stored in the $connections array.
at line 336
Connection
reconnect(UnitEnum|string|null $name = null)
Reconnect to the given database.
In pooled mode, if this coroutine already has a connection, reconnects its PDOs and returns it. In non-pooled mode, refreshes the existing connection's PDOs in-place. Otherwise gets a fresh connection.
at line 381
mixed
usingConnection(UnitEnum|string $name, callable $callback)
Set the default database connection for the callback execution.
Uses Context for coroutine-safe state management, ensuring concurrent requests don't interfere with each other's default connection.
at line 404
protected Connection
refreshPdoConnections(string $name)
Refresh the PDO connections on a given connection.
at line 421
string|null
getDefaultConnection()
Get the default connection name.
Checks Context first for per-coroutine override (from usingConnection()), then falls back to the global config default.
at line 435
void
whenQueryingForLongerThan(DateTimeInterface|CarbonInterval|float|int $threshold, callable $handler)
Register a callback to be invoked when the current connection queries for longer than a given amount of time.
Boot-only. The callback persists on the database manager for the worker lifetime and affects every subsequent request for the same connection.
at line 461
void
allowQueryDurationHandlersToRunAgain()
Allow all the query duration handlers to run again for the current connection.
at line 487
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 config('database.default').
at line 499
protected mixed
configValue(string $key, mixed $default = null)
Get a config value.
at line 512
protected string
getConnectionContextKey(string $name)
Get the context key for storing a connection.
Uses the same format as ConnectionResolver for consistency.
at line 522
protected array
connectionNameVariants(UnitEnum|string|null $name)
Get all coroutine/cache variants for a connection name.
at line 540
protected string
getEffectiveConnectionName()
Get the current effective connection name.
at line 548
protected void
registerQueryDurationListener()
Register the manager-level query duration listener.
at line 598
protected string
queryDurationHandlerKey(int $key, string $connectionName)
Get the coroutine-local key for a query duration handler registration.
at line 608
array
supportedDrivers()
Get all of the supported drivers.
at line 618
array
availableDrivers()
Get all of the drivers that are actually available.
at line 636
void
extend(string $name, callable $resolver)
Register an extension connection resolver.
Extensions are stored on the ConnectionFactory so they are consulted by both the pooled path (PooledConnection → factory) and the non-pooled path (DatabaseManager → factory).
Boot-only. The resolver persists on the singleton ConnectionFactory for the worker lifetime and applies to every subsequent connection.
at line 648
void
forgetExtension(string $name)
Remove an extension connection resolver.
Boot or tests only. Mutates the singleton ConnectionFactory's extension registry; concurrent coroutines establishing connections may see the resolver removed mid-resolution.
at line 663
array
getConnections()
Return all of the created connections.
Note: In Hypervel's pooled connection mode, connections are stored per-coroutine in Context rather than in this array. This method returns an empty array in normal pooled operation. Use the pool infrastructure to inspect active connections if needed.
at line 684
static void
purgeConnections()
Purge all connections on the current manager instance.
Resolves the DatabaseManager from the container and disconnects every active connection. Safe to call when no container or no database manager exists (e.g., unit tests).
Uses bound() - not has() - because Application::has() falls through to class_exists(), so any string that case-insensitively matches a loaded PHP class name would falsely report bound. bound() only checks bindings/instances/aliases.
Boot or tests only. Calls purge() on every connection (flushes the shared pools); concurrent coroutines lose their cached connection references mid-request.
at line 707
void
setReconnector(callable $reconnector)
Set the database reconnector callback.
Boot-only. The reconnector persists on the singleton DatabaseManager for the worker lifetime and is invoked for every disconnected connection across all coroutines.
at line 718
DatabaseManager
setApplication(Application $app)
Set the application instance used by the manager.
Tests only. Swaps the singleton's application reference; per-request use races across coroutines and breaks every concurrent database operation.
at line 728
static void
flushState()
Flush all static state.