You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by zh...@apache.org on 2021/09/22 13:39:53 UTC

[shardingsphere] branch master updated: Improve resource release logic to prevent memory leaks. (#12324)

This is an automated email from the ASF dual-hosted git repository.

zhangliang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git


The following commit(s) were added to refs/heads/master by this push:
     new 54aed2a  Improve resource release logic to prevent memory leaks. (#12324)
54aed2a is described below

commit 54aed2abd1e81c77f0163117b936d0a5a75b2494
Author: yx9o <ya...@163.com>
AuthorDate: Wed Sep 22 21:39:16 2021 +0800

    Improve resource release logic to prevent memory leaks. (#12324)
---
 .../builder/dialect/MySQLPrivilegeHandler.java     | 52 ++++++++++++----------
 .../builder/dialect/OraclePrivilegeHandler.java    | 39 ++++++++--------
 .../dialect/PostgreSQLPrivilegeHandler.java        | 39 ++++++++--------
 .../builder/dialect/SQLServerPrivilegeHandler.java | 52 ++++++++++++----------
 4 files changed, 98 insertions(+), 84 deletions(-)

diff --git a/shardingsphere-kernel/shardingsphere-authority/shardingsphere-authority-core/src/main/java/org/apache/shardingsphere/authority/provider/natived/builder/dialect/MySQLPrivilegeHandler.java b/shardingsphere-kernel/shardingsphere-authority/shardingsphere-authority-core/src/main/java/org/apache/shardingsphere/authority/provider/natived/builder/dialect/MySQLPrivilegeHandler.java
index ee6ea4b..6501291 100644
--- a/shardingsphere-kernel/shardingsphere-authority/shardingsphere-authority-core/src/main/java/org/apache/shardingsphere/authority/provider/natived/builder/dialect/MySQLPrivilegeHandler.java
+++ b/shardingsphere-kernel/shardingsphere-authority/shardingsphere-authority-core/src/main/java/org/apache/shardingsphere/authority/provider/natived/builder/dialect/MySQLPrivilegeHandler.java
@@ -56,12 +56,13 @@ public final class MySQLPrivilegeHandler implements StoragePrivilegeHandler {
     @Override
     public Collection<ShardingSphereUser> diff(final Collection<ShardingSphereUser> users, final DataSource dataSource) throws SQLException {
         Collection<Grantee> grantees = new LinkedList<>();
-        try (Connection connection = dataSource.getConnection()) {
-            Statement statement = connection.createStatement();
-            try (ResultSet resultSet = statement.executeQuery(getGlobalPrivilegesSQL(users))) {
-                while (resultSet.next()) {
-                    grantees.add(new Grantee(resultSet.getString("user"), resultSet.getString("host")));
-                }
+        try (
+                Connection connection = dataSource.getConnection();
+                Statement statement = connection.createStatement();
+                ResultSet resultSet = statement.executeQuery(getGlobalPrivilegesSQL(users))
+        ) {
+            while (resultSet.next()) {
+                grantees.add(new Grantee(resultSet.getString("user"), resultSet.getString("host")));
             }
         }
         return users.stream().filter(each -> !grantees.contains(each.getGrantee())).collect(Collectors.toList());
@@ -105,12 +106,13 @@ public final class MySQLPrivilegeHandler implements StoragePrivilegeHandler {
     
     private void fillGlobalPrivileges(final Map<ShardingSphereUser, NativePrivileges> userPrivilegeMap, 
                                       final DataSource dataSource, final Collection<ShardingSphereUser> users) throws SQLException {
-        try (Connection connection = dataSource.getConnection()) {
-            Statement statement = connection.createStatement();
-            try (ResultSet resultSet = statement.executeQuery(getGlobalPrivilegesSQL(users))) {
-                while (resultSet.next()) {
-                    fillGlobalPrivileges(userPrivilegeMap, resultSet);
-                }
+        try (
+                Connection connection = dataSource.getConnection();
+                Statement statement = connection.createStatement();
+                ResultSet resultSet = statement.executeQuery(getGlobalPrivilegesSQL(users))
+        ) {
+            while (resultSet.next()) {
+                fillGlobalPrivileges(userPrivilegeMap, resultSet);
             }
         }
     }
@@ -125,12 +127,13 @@ public final class MySQLPrivilegeHandler implements StoragePrivilegeHandler {
     
     private void fillSchemaPrivileges(final Map<ShardingSphereUser, NativePrivileges> userPrivilegeMap, 
                                       final DataSource dataSource, final Collection<ShardingSphereUser> users) throws SQLException {
-        try (Connection connection = dataSource.getConnection()) {
-            Statement statement = connection.createStatement();
-            try (ResultSet resultSet = statement.executeQuery(getSchemaPrivilegesSQL(users))) {
-                while (resultSet.next()) {
-                    fillSchemaPrivileges(userPrivilegeMap, resultSet);
-                }
+        try (
+                Connection connection = dataSource.getConnection();
+                Statement statement = connection.createStatement();
+                ResultSet resultSet = statement.executeQuery(getSchemaPrivilegesSQL(users))
+        ) {
+            while (resultSet.next()) {
+                fillSchemaPrivileges(userPrivilegeMap, resultSet);
             }
         }
     }
@@ -147,12 +150,13 @@ public final class MySQLPrivilegeHandler implements StoragePrivilegeHandler {
     
     private void fillTablePrivileges(final Map<ShardingSphereUser, NativePrivileges> userPrivilegeMap, 
                                      final DataSource dataSource, final Collection<ShardingSphereUser> users) throws SQLException {
-        try (Connection connection = dataSource.getConnection()) {
-            Statement statement = connection.createStatement();
-            try (ResultSet resultSet = statement.executeQuery(getTablePrivilegesSQL(users))) {
-                while (resultSet.next()) {
-                    fillTablePrivileges(userPrivilegeMap, resultSet);
-                }
+        try (
+                Connection connection = dataSource.getConnection();
+                Statement statement = connection.createStatement();
+                ResultSet resultSet = statement.executeQuery(getTablePrivilegesSQL(users))
+        ) {
+            while (resultSet.next()) {
+                fillTablePrivileges(userPrivilegeMap, resultSet);
             }
         }
     }
diff --git a/shardingsphere-kernel/shardingsphere-authority/shardingsphere-authority-core/src/main/java/org/apache/shardingsphere/authority/provider/natived/builder/dialect/OraclePrivilegeHandler.java b/shardingsphere-kernel/shardingsphere-authority/shardingsphere-authority-core/src/main/java/org/apache/shardingsphere/authority/provider/natived/builder/dialect/OraclePrivilegeHandler.java
index 4efb72a..1625d18 100644
--- a/shardingsphere-kernel/shardingsphere-authority/shardingsphere-authority-core/src/main/java/org/apache/shardingsphere/authority/provider/natived/builder/dialect/OraclePrivilegeHandler.java
+++ b/shardingsphere-kernel/shardingsphere-authority/shardingsphere-authority-core/src/main/java/org/apache/shardingsphere/authority/provider/natived/builder/dialect/OraclePrivilegeHandler.java
@@ -57,12 +57,13 @@ public final class OraclePrivilegeHandler implements StoragePrivilegeHandler {
     @Override
     public Collection<ShardingSphereUser> diff(final Collection<ShardingSphereUser> users, final DataSource dataSource) throws SQLException {
         Collection<Grantee> grantees = new LinkedList<>();
-        try (Connection connection = dataSource.getConnection()) {
-            Statement statement = connection.createStatement();
-            try (ResultSet resultSet = statement.executeQuery(getSysPrivilegesSQL(users))) {
-                while (resultSet.next()) {
-                    grantees.add(new Grantee(resultSet.getString("GRANTEE"), ""));
-                }
+        try (
+                Connection connection = dataSource.getConnection();
+                Statement statement = connection.createStatement();
+                ResultSet resultSet = statement.executeQuery(getSysPrivilegesSQL(users))
+        ) {
+            while (resultSet.next()) {
+                grantees.add(new Grantee(resultSet.getString("GRANTEE"), ""));
             }
         }
         return users.stream().filter(each -> !grantees.contains(each.getGrantee())).collect(Collectors.toList());
@@ -98,12 +99,13 @@ public final class OraclePrivilegeHandler implements StoragePrivilegeHandler {
     private void fillTablePrivileges(final Map<ShardingSphereUser, NativePrivileges> userPrivilegeMap, final DataSource dataSource,
                                      final Collection<ShardingSphereUser> users) throws SQLException {
         Map<ShardingSphereUser, Map<String, Map<String, List<PrivilegeType>>>> privilegeCache = new HashMap<>();
-        try (Connection connection = dataSource.getConnection()) {
-            Statement statement = connection.createStatement();
-            try (ResultSet resultSet = statement.executeQuery(getTablePrivilegesSQL(users))) {
-                while (resultSet.next()) {
-                    collectTablePrivileges(privilegeCache, resultSet);
-                }
+        try (
+                Connection connection = dataSource.getConnection();
+                Statement statement = connection.createStatement();
+                ResultSet resultSet = statement.executeQuery(getTablePrivilegesSQL(users))
+        ) {
+            while (resultSet.next()) {
+                collectTablePrivileges(privilegeCache, resultSet);
             }
         }
         fillTablePrivileges(privilegeCache, userPrivilegeMap);
@@ -143,12 +145,13 @@ public final class OraclePrivilegeHandler implements StoragePrivilegeHandler {
     private void fillSysPrivileges(final Map<ShardingSphereUser, NativePrivileges> userPrivilegeMap, final DataSource dataSource,
                                    final Collection<ShardingSphereUser> users) throws SQLException {
         Map<ShardingSphereUser, List<PrivilegeType>> privilegeCache = new HashMap<>();
-        try (Connection connection = dataSource.getConnection()) {
-            Statement statement = connection.createStatement();
-            try (ResultSet resultSet = statement.executeQuery(getSysPrivilegesSQL(users))) {
-                while (resultSet.next()) {
-                    collectSysPrivileges(privilegeCache, resultSet);
-                }
+        try (
+                Connection connection = dataSource.getConnection();
+                Statement statement = connection.createStatement();
+                ResultSet resultSet = statement.executeQuery(getSysPrivilegesSQL(users))
+        ) {
+            while (resultSet.next()) {
+                collectSysPrivileges(privilegeCache, resultSet);
             }
         }
         fillSysPrivileges(privilegeCache, userPrivilegeMap);
diff --git a/shardingsphere-kernel/shardingsphere-authority/shardingsphere-authority-core/src/main/java/org/apache/shardingsphere/authority/provider/natived/builder/dialect/PostgreSQLPrivilegeHandler.java b/shardingsphere-kernel/shardingsphere-authority/shardingsphere-authority-core/src/main/java/org/apache/shardingsphere/authority/provider/natived/builder/dialect/PostgreSQLPrivilegeHandler.java
index a7e0a4f..db55e1b 100644
--- a/shardingsphere-kernel/shardingsphere-authority/shardingsphere-authority-core/src/main/java/org/apache/shardingsphere/authority/provider/natived/builder/dialect/PostgreSQLPrivilegeHandler.java
+++ b/shardingsphere-kernel/shardingsphere-authority/shardingsphere-authority-core/src/main/java/org/apache/shardingsphere/authority/provider/natived/builder/dialect/PostgreSQLPrivilegeHandler.java
@@ -57,12 +57,13 @@ public final class PostgreSQLPrivilegeHandler implements StoragePrivilegeHandler
     @Override
     public Collection<ShardingSphereUser> diff(final Collection<ShardingSphereUser> users, final DataSource dataSource) throws SQLException {
         Collection<Grantee> grantees = new LinkedList<>();
-        try (Connection connection = dataSource.getConnection()) {
-            Statement statement = connection.createStatement();
-            try (ResultSet resultSet = statement.executeQuery(getRolePrivilegesSQL(users))) {
-                while (resultSet.next()) {
-                    grantees.add(new Grantee(resultSet.getString("rolname"), ""));
-                }
+        try (
+                Connection connection = dataSource.getConnection();
+                Statement statement = connection.createStatement();
+                ResultSet resultSet = statement.executeQuery(getRolePrivilegesSQL(users))
+        ) {
+            while (resultSet.next()) {
+                grantees.add(new Grantee(resultSet.getString("rolname"), ""));
             }
         }
         return users.stream().filter(each -> !grantees.contains(each.getGrantee())).collect(Collectors.toList());
@@ -106,12 +107,13 @@ public final class PostgreSQLPrivilegeHandler implements StoragePrivilegeHandler
     private void fillTablePrivileges(final Map<ShardingSphereUser, NativePrivileges> userPrivilegeMap,
                                      final DataSource dataSource, final Collection<ShardingSphereUser> users) throws SQLException {
         Map<ShardingSphereUser, Map<String, Map<String, List<PrivilegeType>>>> privilegeCache = new HashMap<>();
-        try (Connection connection = dataSource.getConnection()) {
-            Statement statement = connection.createStatement();
-            try (ResultSet resultSet = statement.executeQuery(getTablePrivilegesSQL(users))) {
-                while (resultSet.next()) {
-                    collectPrivileges(privilegeCache, resultSet);
-                }
+        try (
+                Connection connection = dataSource.getConnection();
+                Statement statement = connection.createStatement();
+                ResultSet resultSet = statement.executeQuery(getTablePrivilegesSQL(users))
+        ) {
+            while (resultSet.next()) {
+                collectPrivileges(privilegeCache, resultSet);
             }
         }
         fillTablePrivileges(privilegeCache, userPrivilegeMap);
@@ -150,12 +152,13 @@ public final class PostgreSQLPrivilegeHandler implements StoragePrivilegeHandler
     
     private void fillRolePrivileges(final Map<ShardingSphereUser, NativePrivileges> userPrivilegeMap, 
                                     final DataSource dataSource, final Collection<ShardingSphereUser> users) throws SQLException {
-        try (Connection connection = dataSource.getConnection()) {
-            Statement statement = connection.createStatement();
-            try (ResultSet resultSet = statement.executeQuery(getRolePrivilegesSQL(users))) {
-                while (resultSet.next()) {
-                    fillRolePrivileges(userPrivilegeMap, resultSet);
-                }
+        try (
+                Connection connection = dataSource.getConnection();
+                Statement statement = connection.createStatement();
+                ResultSet resultSet = statement.executeQuery(getRolePrivilegesSQL(users))
+        ) {
+            while (resultSet.next()) {
+                fillRolePrivileges(userPrivilegeMap, resultSet);
             }
         }
     }
diff --git a/shardingsphere-kernel/shardingsphere-authority/shardingsphere-authority-core/src/main/java/org/apache/shardingsphere/authority/provider/natived/builder/dialect/SQLServerPrivilegeHandler.java b/shardingsphere-kernel/shardingsphere-authority/shardingsphere-authority-core/src/main/java/org/apache/shardingsphere/authority/provider/natived/builder/dialect/SQLServerPrivilegeHandler.java
index 6f021dc..82d6210 100644
--- a/shardingsphere-kernel/shardingsphere-authority/shardingsphere-authority-core/src/main/java/org/apache/shardingsphere/authority/provider/natived/builder/dialect/SQLServerPrivilegeHandler.java
+++ b/shardingsphere-kernel/shardingsphere-authority/shardingsphere-authority-core/src/main/java/org/apache/shardingsphere/authority/provider/natived/builder/dialect/SQLServerPrivilegeHandler.java
@@ -75,12 +75,13 @@ public final class SQLServerPrivilegeHandler implements StoragePrivilegeHandler
     @Override
     public Collection<ShardingSphereUser> diff(final Collection<ShardingSphereUser> users, final DataSource dataSource) throws SQLException {
         Collection<Grantee> grantees = new LinkedList<>();
-        try (Connection connection = dataSource.getConnection()) {
-            Statement statement = connection.createStatement();
-            try (ResultSet resultSet = statement.executeQuery(getGlobalPrivilegesSQL(users))) {
-                while (resultSet.next()) {
-                    grantees.add(new Grantee(resultSet.getString("GRANTEE"), ""));
-                }
+        try (
+                Connection connection = dataSource.getConnection();
+                Statement statement = connection.createStatement();
+                ResultSet resultSet = statement.executeQuery(getGlobalPrivilegesSQL(users))
+        ) {
+            while (resultSet.next()) {
+                grantees.add(new Grantee(resultSet.getString("GRANTEE"), ""));
             }
         }
         return users.stream().filter(each -> !grantees.contains(each.getGrantee())).collect(Collectors.toList());
@@ -137,12 +138,13 @@ public final class SQLServerPrivilegeHandler implements StoragePrivilegeHandler
     
     private void fillGlobalPrivileges(final Map<ShardingSphereUser, NativePrivileges> userPrivilegeMap,
                                       final DataSource dataSource, final Collection<ShardingSphereUser> users) throws SQLException {
-        try (Connection connection = dataSource.getConnection()) {
-            Statement statement = connection.createStatement();
-            try (ResultSet resultSet = statement.executeQuery(getGlobalPrivilegesSQL(users))) {
-                while (resultSet.next()) {
-                    fillGlobalPrivileges(userPrivilegeMap, resultSet);
-                }
+        try (
+                Connection connection = dataSource.getConnection();
+                Statement statement = connection.createStatement();
+                ResultSet resultSet = statement.executeQuery(getGlobalPrivilegesSQL(users))
+        ) {
+            while (resultSet.next()) {
+                fillGlobalPrivileges(userPrivilegeMap, resultSet);
             }
         }
     }
@@ -155,12 +157,13 @@ public final class SQLServerPrivilegeHandler implements StoragePrivilegeHandler
     }
     
     private void fillSchemaPrivileges(final Map<ShardingSphereUser, NativePrivileges> userPrivilegeMap, final DataSource dataSource, final Collection<ShardingSphereUser> users) throws SQLException {
-        try (Connection connection = dataSource.getConnection()) {
-            Statement statement = connection.createStatement();
-            try (ResultSet resultSet = statement.executeQuery(getSchemaPrivilegesSQL(users))) {
-                while (resultSet.next()) {
-                    fillSchemaPrivileges(userPrivilegeMap, resultSet);
-                }
+        try (
+                Connection connection = dataSource.getConnection();
+                Statement statement = connection.createStatement();
+                ResultSet resultSet = statement.executeQuery(getSchemaPrivilegesSQL(users))
+        ) {
+            while (resultSet.next()) {
+                fillSchemaPrivileges(userPrivilegeMap, resultSet);
             }
         }
     }
@@ -177,12 +180,13 @@ public final class SQLServerPrivilegeHandler implements StoragePrivilegeHandler
     
     private void fillTablePrivileges(final Map<ShardingSphereUser, NativePrivileges> userPrivilegeMap, final DataSource dataSource, final Collection<ShardingSphereUser> users) throws SQLException {
         Map<ShardingSphereUser, Map<String, Map<String, List<PrivilegeType>>>> privilegeCache = new HashMap<>();
-        try (Connection connection = dataSource.getConnection()) {
-            Statement statement = connection.createStatement();
-            try (ResultSet resultSet = statement.executeQuery(getTablePrivilegesSQL(users))) {
-                while (resultSet.next()) {
-                    collectPrivileges(privilegeCache, resultSet);
-                }
+        try (
+                Connection connection = dataSource.getConnection();
+                Statement statement = connection.createStatement();
+                ResultSet resultSet = statement.executeQuery(getTablePrivilegesSQL(users))
+        ) {
+            while (resultSet.next()) {
+                collectPrivileges(privilegeCache, resultSet);
             }
         }
         fillTablePrivileges(privilegeCache, userPrivilegeMap);