From 1f460f41740577f9d3a52aac92da07fa07acd5a7 Mon Sep 17 00:00:00 2001 From: Daan Hoogland Date: Mon, 3 Aug 2015 20:11:18 +0200 Subject: [PATCH] CLOUDSTACK-8656: messages on SQL exception in DbUtils! --- .../config/dao/ConfigurationDaoImpl.java | 14 ++-- .../db/src/com/cloud/utils/db/DbUtil.java | 70 ++++++------------- 2 files changed, 28 insertions(+), 56 deletions(-) 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 f9c2433754b..afc20e181a5 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 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); } - } }