Use try-with-resource statement

This commit is contained in:
Thomas Stokes
2024-03-21 12:43:58 +01:00
parent a97f3044d4
commit 43bdeb1b87
2 changed files with 7 additions and 7 deletions
@@ -5248,11 +5248,10 @@ Replicator {
String message = e.getMessage();
if (connection instanceof HttpURLConnection) {
// try to obtain an error message from the connection's error stream:
try {
message = new BufferedReader(
try (BufferedReader reader = new BufferedReader(
new InputStreamReader(((HttpURLConnection) connection).getErrorStream(),
HttpUrlConnectionHelper.getCharsetFromConnectionOrDefault(connection, "UTF-8")))
.readLine();
HttpUrlConnectionHelper.getCharsetFromConnectionOrDefault(connection, "UTF-8")))) {
message = reader.readLine();
} catch (Exception exceptionTryingToReadErrorStream) {
// in this case we just stay with the exception's message
}
@@ -142,9 +142,10 @@ public class MongoDBServiceImpl implements MongoDBService {
@Override
public MongoClient getMongo(ConnectionString mongoConnectionString) {
MongoClient mongo = mongos.computeIfAbsent(mongoConnectionString,
k-> MongoClients.create(mongoConnectionString));
return mongo;
try (MongoClient mongo = mongos.computeIfAbsent(mongoConnectionString,
k-> MongoClients.create(mongoConnectionString))) {
return mongo;
}
}
@Override