hutiefang76 commented on code in PR #4184:
URL: https://github.com/apache/hertzbeat/pull/4184#discussion_r3560397261
##########
hertzbeat-collector/hertzbeat-collector-basic/src/main/java/org/apache/hertzbeat/collector/collect/database/JdbcCommonCollect.java:
##########
@@ -348,17 +348,36 @@ private Statement getConnection(String username, String
password, String url, In
return statement;
}
}
- // renew connection when failed
- Connection connection = DriverManager.getConnection(url, username,
password);
- connection.setReadOnly(true);
- statement = connection.createStatement();
- int timeoutSecond = timeout / 1000;
- timeoutSecond = timeoutSecond <= 0 ? 1 : timeoutSecond;
- statement.setQueryTimeout(timeoutSecond);
- statement.setMaxRows(1000);
- if (reuseConnection) {
- JdbcConnect jdbcConnect = new JdbcConnect(connection);
- connectionCommonCache.addCache(identifier, jdbcConnect);
+ Connection connection = null;
+ try {
+ // renew connection when failed
+ connection = DriverManager.getConnection(url, username, password);
+ connection.setReadOnly(true);
+ statement = connection.createStatement();
+ int timeoutSecond = timeout / 1000;
+ timeoutSecond = timeoutSecond <= 0 ? 1 : timeoutSecond;
+ statement.setQueryTimeout(timeoutSecond);
+ statement.setMaxRows(1000);
+ if (reuseConnection) {
+ JdbcConnect jdbcConnect = new JdbcConnect(connection);
+ connectionCommonCache.addCache(identifier, jdbcConnect);
Review Comment:
Addressed in a9acae3d6. Reuse-enabled JDBC acquisition now uses striped
locks keyed by CacheIdentifier, covering the cache lookup, physical connection
creation, and cache insertion as one per-target critical section. This prevents
two concurrent cache misses from opening separate physical connections instead
of trying to close an overwritten connection after the race. The new regression
test deterministically pauses the first open and starts a second request for
the same identifier; it failed before the fix because the second physical open
occurred, and now both requests use one connection. JdbcCommonCollectTest
passes 9 tests with checkstyle clean.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]