Preserve dynamic database connections on reconnect #53914
Merged
+69
−9
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This pull request is intended as a fix to Issue #53886 #53886
It introduces a way to automatically reconnect to dynamically built database connections.
Currently calling
DB::build()
with a configuration that is not registered in configuration files will create a new 'on demand' connection. However, should this connection fail, when Laravel attempts to reconnect it will fail with anInvalidArgumentException
due to the connection configuration not existing.With this PR, dynamically built connections are stored in a local registry within the
DatabaseManager
. When a reconnection is attempted, the framework can now correctly retrieve the configuration and re-establish the PDO connection without error.Benefit to End Users:
Developers who dynamically create database connections at runtime will no longer need to manually register them in
config()
arrays or handle reconnections themselves. The framework now supports transparent reconnections out-of-the-box for these on-demand connections.How It Makes Building Web Applications Easier:
Before this change, any "lost connection" scenario for dynamically built connections required workarounds, such as manually re-registering configurations or catching and recreating connections on each failure. Now, developers can rely on Laravel’s built-in reconnection logic, reducing boilerplate and complexity.
Code example:
Before: Dynamically built connections would fail on reconnection:
After: Dynamically built connections are registered and can reconnect seamlessly:
The user experience is improved by removing a hidden pitfall in dynamic connection usage.