Skip to content

Commit 1f460f4

Browse files
committed
CLOUDSTACK-8656: messages on SQL exception in DbUtils!
1 parent 87ae150 commit 1f460f4

2 files changed

Lines changed: 28 additions & 56 deletions

File tree

‎framework/config/src/org/apache/cloudstack/framework/config/dao/ConfigurationDaoImpl.java‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -158,12 +158,14 @@ public boolean update(String name, String value) {
158158
@Override
159159
public boolean update(String name, String category, String value) {
160160
TransactionLegacy txn = TransactionLegacy.currentTxn();
161-
value = ("Hidden".equals(category) || "Secure".equals(category)) ? DBEncryptionUtil.encrypt(value) : value;
162-
try (PreparedStatement stmt = txn.prepareStatement(UPDATE_CONFIGURATION_SQL);) {
163-
stmt.setString(1, value);
164-
stmt.setString(2, name);
165-
stmt.executeUpdate();
166-
return true;
161+
try {
162+
value = ("Hidden".equals(category) || "Secure".equals(category)) ? DBEncryptionUtil.encrypt(value) : value;
163+
try (PreparedStatement stmt = txn.prepareStatement(UPDATE_CONFIGURATION_SQL);) {
164+
stmt.setString(1, value);
165+
stmt.setString(2, name);
166+
stmt.executeUpdate();
167+
return true;
168+
}
167169
} catch (Exception e) {
168170
s_logger.warn("Unable to update Configuration Value", e);
169171
}

‎framework/db/src/com/cloud/utils/db/DbUtil.java‎

Lines changed: 20 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,7 @@ public static Connection getConnectionForGlobalLocks(String name, boolean forLoc
6161
try {
6262
connection.setAutoCommit(true);
6363
} catch (SQLException e) {
64-
try {
65-
connection.close();
66-
} catch (SQLException sqlException) {
67-
}
64+
closeAutoCloseable(connection, "error closing connection for global locks");
6865
return null;
6966
}
7067
s_connectionForGlobalLocks.put(name, connection);
@@ -203,37 +200,28 @@ public static boolean getGlobalLock(String name, int timeoutSeconds) {
203200
return false;
204201
}
205202

206-
PreparedStatement pstmt = null;
207-
ResultSet rs = null;
208-
try {
209-
pstmt = conn.prepareStatement("SELECT COALESCE(GET_LOCK(?, ?),0)");
210-
203+
try (PreparedStatement pstmt = conn.prepareStatement("SELECT COALESCE(GET_LOCK(?, ?),0)");) {
211204
pstmt.setString(1, name);
212205
pstmt.setInt(2, timeoutSeconds);
213206

214-
rs = pstmt.executeQuery();
215-
if (rs != null && rs.first()) {
216-
if (rs.getInt(1) > 0) {
217-
return true;
218-
} else {
219-
if (s_logger.isDebugEnabled())
220-
s_logger.debug("GET_LOCK() timed out on lock : " + name);
207+
try (ResultSet rs = pstmt.executeQuery();) {
208+
if (rs != null && rs.first()) {
209+
if (rs.getInt(1) > 0) {
210+
return true;
211+
} else {
212+
if (s_logger.isDebugEnabled())
213+
s_logger.debug("GET_LOCK() timed out on lock : " + name);
214+
}
221215
}
222216
}
223217
} catch (SQLException e) {
224218
s_logger.error("GET_LOCK() throws exception ", e);
225219
} catch (Throwable e) {
226220
s_logger.error("GET_LOCK() throws exception ", e);
227-
} finally {
228-
closeStatement(pstmt);
229-
closeResultSet(rs);
230221
}
231222

232223
removeConnectionForGlobalLocks(name);
233-
try {
234-
conn.close();
235-
} catch (SQLException e) {
236-
}
224+
closeAutoCloseable(conn, "connection for global lock");
237225
return false;
238226
}
239227

@@ -285,45 +273,27 @@ public static void closeResources(final Statement statement, final ResultSet res
285273
}
286274

287275
public static void closeResultSet(final ResultSet resultSet) {
288-
289-
try {
290-
291-
if (resultSet != null) {
292-
resultSet.close();
293-
}
294-
295-
} catch (SQLException e) {
296-
s_logger.warn("Ignored exception while closing result set.", e);
297-
}
298-
276+
closeAutoCloseable(resultSet, "exception while closing result set.");
299277
}
300278

301279
public static void closeStatement(final Statement statement) {
302-
303-
try {
304-
305-
if (statement != null) {
306-
statement.close();
307-
}
308-
309-
} catch (SQLException e) {
310-
s_logger.warn("Ignored exception while closing statement.", e);
311-
}
312-
280+
closeAutoCloseable(statement, "exception while closing statement.");
313281
}
314282

315283
public static void closeConnection(final Connection connection) {
284+
closeAutoCloseable(connection, "exception while close connection.");
285+
}
316286

287+
public static void closeAutoCloseable(AutoCloseable ac, String message) {
317288
try {
318289

319-
if (connection != null) {
320-
connection.close();
290+
if (ac != null) {
291+
ac.close();
321292
}
322293

323-
} catch (SQLException e) {
324-
s_logger.warn("Ignored exception while close connection.", e);
294+
} catch (Exception e) {
295+
s_logger.warn("[ignored] " + message, e);
325296
}
326-
327297
}
328298

329299
}

0 commit comments

Comments
 (0)