CLOUDSTACK-8656: messages on SQL exception in DbUtils!

This commit is contained in:
Daan Hoogland 2015-08-03 20:11:18 +02:00
parent 87ae150159
commit 1f460f4174
2 changed files with 28 additions and 56 deletions

View File

@ -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);
}

View File

@ -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);
}
}
}