-
-
Notifications
You must be signed in to change notification settings - Fork 27
feat: enhance DB connection tracing with real-time server address detection #1028
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
84215ce
feat: enhance DB connection tracing with real-time server address det…
huangdijia b8e5a06
feat: improve server address tracing in DbAspect with default values
huangdijia 3417d53
feat: improve driver detection logic in DbAspect for database connect…
huangdijia File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
验证初次连接时的缓存填充逻辑。
当前实现仅在
reconnect时填充serverCache。如果连接池返回的是已建立的连接且未触发重连,serverCache可能不包含该连接的配置信息,导致 Line 72 获取不到服务器地址。请验证以下场景是否正常工作:
DB::getConnection()获取连接serverCache中是否有该连接的配置运行以下脚本验证连接生命周期中
reconnect的调用时机:🏁 Script executed:
Length of output: 0
🏁 Script executed:
Length of output: 11210
[/web_search]
🌐 Web query:
💡 Result:
Short answer — what happens when the pool needs a "new"/reconnected DB connection in Hyperf:
When a connection is borrowed the Connection->getActiveConnection() first calls check(). check() compares now against connection->lastUseTime + pool.maxIdleTime; if the connection is considered expired/invalid, getActiveConnection() calls reconnect(). If reconnect() fails an exception is thrown. [1]
If there are no idle connections available and the pool hasn't reached its configured max connections, the pool will create a new connection (up to max). If the pool is at capacity, callers wait up to wait_timeout (or get a "pool exhausted" error). Pool options that control this are min/max connections, maxIdleTime, maxLifetime/heartbeat, connect_timeout and wait_timeout. [2][3]
Practical implications:
Sources: Hyperf Connection implementation showing check()/reconnect() and lastUseTime logic, and Hyperf/third‑party docs explaining pool options and behavior. [1][2][3]
References
[1] Source showing Connection::getActiveConnection() / check() logic.
[2] Hyperf connection‑pool configuration and lifecycle documentation.
[3] Community posts/examples on pool exhaustion and pool option effects.
getConnection() 处理器应同时缓存新连接的配置信息
验证确认了原始问题的有效性。根据 Hyperf 连接池生命周期:
reconnect()仅在连接超时失效时调用,新创建的连接不会触发重连。因此serverCache仅在第 64-65 行的reconnect()处理器中填充。对于从连接池获取的新连接:
$server = $this->serverCache[$connection] ?? null返回 null修复:在 getConnection() 处理器(第 69-76 行)中,当缓存未命中时也应捕获配置:
这确保新连接和重连连接的配置都被正确缓存和上下文化。
🤖 Prompt for AI Agents