You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cloudstack.apache.org by da...@apache.org on 2015/08/03 23:45:10 UTC

[11/13] git commit: updated refs/heads/master to d32d6a2

CLOUDSTACK-8656: messages on SQL exception in DbUtils!


Project: http://git-wip-us.apache.org/repos/asf/cloudstack/repo
Commit: http://git-wip-us.apache.org/repos/asf/cloudstack/commit/1f460f41
Tree: http://git-wip-us.apache.org/repos/asf/cloudstack/tree/1f460f41
Diff: http://git-wip-us.apache.org/repos/asf/cloudstack/diff/1f460f41

Branch: refs/heads/master
Commit: 1f460f41740577f9d3a52aac92da07fa07acd5a7
Parents: 87ae150
Author: Daan Hoogland <da...@onecht.net>
Authored: Mon Aug 3 20:11:18 2015 +0200
Committer: Daan Hoogland <da...@onecht.net>
Committed: Mon Aug 3 20:49:02 2015 +0200

----------------------------------------------------------------------
 .../config/dao/ConfigurationDaoImpl.java        | 14 ++--
 framework/db/src/com/cloud/utils/db/DbUtil.java | 70 ++++++--------------
 2 files changed, 28 insertions(+), 56 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cloudstack/blob/1f460f41/framework/config/src/org/apache/cloudstack/framework/config/dao/ConfigurationDaoImpl.java
----------------------------------------------------------------------
diff --git a/framework/config/src/org/apache/cloudstack/framework/config/dao/ConfigurationDaoImpl.java b/framework/config/src/org/apache/cloudstack/framework/config/dao/ConfigurationDaoImpl.java
index f9c2433..afc20e1 100644
--- a/framework/config/src/org/apache/cloudstack/framework/config/dao/ConfigurationDaoImpl.java
+++ b/framework/config/src/org/apache/cloudstack/framework/config/dao/ConfigurationDaoImpl.java
@@ -158,12 +158,14 @@ public class ConfigurationDaoImpl extends GenericDaoBase<ConfigurationVO, String
     @Override
     public boolean update(String name, String category, String value) {
         TransactionLegacy txn = TransactionLegacy.currentTxn();
-        value = ("Hidden".equals(category) || "Secure".equals(category)) ? DBEncryptionUtil.encrypt(value) : value;
-        try (PreparedStatement stmt = txn.prepareStatement(UPDATE_CONFIGURATION_SQL);) {
-            stmt.setString(1, value);
-            stmt.setString(2, name);
-            stmt.executeUpdate();
-            return true;
+        try {
+            value = ("Hidden".equals(category) || "Secure".equals(category)) ? DBEncryptionUtil.encrypt(value) : value;
+            try (PreparedStatement stmt = txn.prepareStatement(UPDATE_CONFIGURATION_SQL);) {
+                stmt.setString(1, value);
+                stmt.setString(2, name);
+                stmt.executeUpdate();
+                return true;
+            }
         } catch (Exception e) {
             s_logger.warn("Unable to update Configuration Value", e);
         }

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/1f460f41/framework/db/src/com/cloud/utils/db/DbUtil.java
----------------------------------------------------------------------
diff --git a/framework/db/src/com/cloud/utils/db/DbUtil.java b/framework/db/src/com/cloud/utils/db/DbUtil.java
index 43ed462..d868905 100644
--- a/framework/db/src/com/cloud/utils/db/DbUtil.java
+++ b/framework/db/src/com/cloud/utils/db/DbUtil.java
@@ -61,10 +61,7 @@ public class DbUtil {
                     try {
                         connection.setAutoCommit(true);
                     } catch (SQLException e) {
-                        try {
-                            connection.close();
-                        } catch (SQLException sqlException) {
-                        }
+                        closeAutoCloseable(connection, "error closing connection for global locks");
                         return null;
                     }
                     s_connectionForGlobalLocks.put(name, connection);
@@ -203,37 +200,28 @@ public class DbUtil {
             return false;
         }
 
-        PreparedStatement pstmt = null;
-        ResultSet rs = null;
-        try {
-            pstmt = conn.prepareStatement("SELECT COALESCE(GET_LOCK(?, ?),0)");
-
+        try (PreparedStatement pstmt = conn.prepareStatement("SELECT COALESCE(GET_LOCK(?, ?),0)");) {
             pstmt.setString(1, name);
             pstmt.setInt(2, timeoutSeconds);
 
-            rs = pstmt.executeQuery();
-            if (rs != null && rs.first()) {
-                if (rs.getInt(1) > 0) {
-                    return true;
-                } else {
-                    if (s_logger.isDebugEnabled())
-                        s_logger.debug("GET_LOCK() timed out on lock : " + name);
+            try (ResultSet rs = pstmt.executeQuery();) {
+                if (rs != null && rs.first()) {
+                    if (rs.getInt(1) > 0) {
+                        return true;
+                    } else {
+                        if (s_logger.isDebugEnabled())
+                            s_logger.debug("GET_LOCK() timed out on lock : " + name);
+                    }
                 }
             }
         } catch (SQLException e) {
             s_logger.error("GET_LOCK() throws exception ", e);
         } catch (Throwable e) {
             s_logger.error("GET_LOCK() throws exception ", e);
-        } finally {
-            closeStatement(pstmt);
-            closeResultSet(rs);
         }
 
         removeConnectionForGlobalLocks(name);
-        try {
-            conn.close();
-        } catch (SQLException e) {
-        }
+        closeAutoCloseable(conn, "connection for global lock");
         return false;
     }
 
@@ -285,45 +273,27 @@ public class DbUtil {
     }
 
     public static void closeResultSet(final ResultSet resultSet) {
-
-        try {
-
-            if (resultSet != null) {
-                resultSet.close();
-            }
-
-        } catch (SQLException e) {
-            s_logger.warn("Ignored exception while closing result set.", e);
-        }
-
+        closeAutoCloseable(resultSet, "exception while closing result set.");
     }
 
     public static void closeStatement(final Statement statement) {
-
-        try {
-
-            if (statement != null) {
-                statement.close();
-            }
-
-        } catch (SQLException e) {
-            s_logger.warn("Ignored exception while closing statement.", e);
-        }
-
+        closeAutoCloseable(statement, "exception while closing statement.");
     }
 
     public static void closeConnection(final Connection connection) {
+        closeAutoCloseable(connection, "exception while close connection.");
+    }
 
+    public static void closeAutoCloseable(AutoCloseable ac, String message) {
         try {
 
-            if (connection != null) {
-                connection.close();
+            if (ac != null) {
+                ac.close();
             }
 
-        } catch (SQLException e) {
-            s_logger.warn("Ignored exception while close connection.", e);
+        } catch (Exception e) {
+            s_logger.warn("[ignored] " + message, e);
         }
-
     }
 
 }