Duansg commented on code in PR #4184:
URL: https://github.com/apache/hertzbeat/pull/4184#discussion_r3542227715


##########
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:
   There is another issue here: when two threads access the same `cache 
identifier` simultaneously, `addCache`—because it is a non-atomic operation—may 
still cause a memory leak. For example: multiple monitoring tasks use the same 
URL. The connection being replaced may be silently overwritten without being 
closed.
   
   Could you please add to or edit this section?



-- 
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]

Reply via email to