Fixes "a bunch of small leaks (closes missing, reported as warnings in Eclipse)" - https://issues.apache.org/jira/browse/OFBIZ-8115

No functional changes, simply use the try-with-resources statement everywhere it's possible, mostly to clean the finally statements and have consistent code

git-svn-id: https://svn.apache.org/repos/asf/ofbiz/trunk@1759088 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Jacques Le Roux
2016-09-03 12:49:47 +00:00
parent 317174b7af
commit 4dd6a0475b
@@ -1613,8 +1613,6 @@ public class DatabaseUtil {
}
Connection connection = null;
Statement stmt = null;
try {
connection = getConnection();
} catch (SQLException e) {
@@ -1729,17 +1727,11 @@ public class DatabaseUtil {
}
if (Debug.verboseOn()) Debug.logVerbose("[createTable] sql=" + sqlBuf.toString(), module);
try {
stmt = connection.createStatement();
try (Statement stmt = connection.createStatement()) {
stmt.executeUpdate(sqlBuf.toString());
} catch (SQLException e) {
return "SQL Exception while executing the following:\n" + sqlBuf.toString() + "\nError was: " + e.toString();
} finally {
try {
if (stmt != null) stmt.close();
} catch (SQLException e) {
Debug.logError(e, module);
}
try {
if (connection != null) {
connection.close();
@@ -1766,7 +1758,6 @@ public class DatabaseUtil {
}
Connection connection = getConnectionLogged(messages);
Statement stmt = null;
if (connection == null) {
return;
}
@@ -1778,19 +1769,13 @@ public class DatabaseUtil {
StringBuilder sqlBuf = new StringBuilder("DROP TABLE ");
sqlBuf.append(entity.getTableName(datasourceInfo));
if (Debug.verboseOn()) Debug.logVerbose("[deleteTable] sql=" + sqlBuf.toString(), module);
try {
stmt = connection.createStatement();
try (Statement stmt = connection.createStatement()) {
stmt.executeUpdate(sqlBuf.toString());
} catch (SQLException e) {
String errMsg = "SQL Exception while executing the following:\n" + sqlBuf.toString() + "\nError was: " + e.toString();
Debug.logError(errMsg, module);
if (messages != null) messages.add(errMsg);
} finally {
try {
if (stmt != null) stmt.close();
} catch (SQLException e) {
Debug.logError(e, module);
}
try {
if (connection != null) {
connection.close();
@@ -1904,7 +1889,6 @@ public class DatabaseUtil {
}
Connection connection = null;
Statement stmt = null;
try {
connection = getConnection();
@@ -1933,19 +1917,11 @@ public class DatabaseUtil {
String sql = sqlBuf.toString();
if (Debug.infoOn()) Debug.logInfo("[renameColumn] sql=" + sql, module);
try {
stmt = connection.createStatement();
try (Statement stmt = connection.createStatement()) {
stmt.executeUpdate(sql);
} catch (SQLException e) {
return "SQL Exception while executing the following:\n" + sql + "\nError was: " + e.toString();
} finally {
try {
if (stmt != null) {
stmt.close();
}
} catch (SQLException e) {
Debug.logError(e, module);
}
try {
if (connection != null) {
connection.close();
@@ -1977,7 +1953,6 @@ public class DatabaseUtil {
// need connection
Connection connection = getConnectionLogged(messages);
Statement stmt = null;
if (connection == null) {
return;
}
@@ -1992,8 +1967,7 @@ public class DatabaseUtil {
String sql1 = sqlBuf1.toString();
if (Debug.infoOn()) Debug.logInfo("[moveData] sql=" + sql1, module);
try {
stmt = connection.createStatement();
try (Statement stmt = connection.createStatement()) {
int changed = stmt.executeUpdate(sql1);
if (Debug.infoOn()) Debug.logInfo("[moveData] " + changed + " records updated", module);
} catch (SQLException e) {
@@ -2003,13 +1977,6 @@ public class DatabaseUtil {
Debug.logError(thisMsg, module);
return;
} finally {
try {
if (stmt != null) {
stmt.close();
}
} catch (SQLException e) {
Debug.logError(e, module);
}
try {
if (connection != null) {
connection.close();
@@ -2033,8 +2000,7 @@ public class DatabaseUtil {
String sql2 = sqlBuf2.toString();
if (Debug.infoOn()) Debug.logInfo("[dropColumn] sql=" + sql2, module);
try {
stmt = connection.createStatement();
try (Statement stmt = connection.createStatement()) {
stmt.executeUpdate(sql2);
} catch (SQLException e) {
String thisMsg = "SQL Exception while executing the following:\n" + sql2 + "\nError was: " + e.toString();
@@ -2043,13 +2009,6 @@ public class DatabaseUtil {
Debug.logError(thisMsg, module);
return;
} finally {
try {
if (stmt != null) {
stmt.close();
}
} catch (SQLException e) {
Debug.logError(e, module);
}
try {
if (connection != null) {
connection.close();
@@ -2187,7 +2146,6 @@ public class DatabaseUtil {
public String createForeignKey(ModelEntity entity, ModelRelation modelRelation, ModelEntity relModelEntity, int constraintNameClipLength, String fkStyle, boolean useFkInitiallyDeferred) {
Connection connection = null;
Statement stmt = null;
try {
connection = getConnection();
@@ -2212,19 +2170,11 @@ public class DatabaseUtil {
sqlBuf.append(fkConstraintClause);
if (Debug.verboseOn()) Debug.logVerbose("[createForeignKey] sql=" + sqlBuf.toString(), module);
try {
stmt = connection.createStatement();
try (Statement stmt = connection.createStatement()) {
stmt.executeUpdate(sqlBuf.toString());
} catch (SQLException e) {
return "SQL Exception while executing the following:\n" + sqlBuf.toString() + "\nError was: " + e.toString();
} finally {
try {
if (stmt != null) {
stmt.close();
}
} catch (SQLException e) {
Debug.logError(e, module);
}
try {
if (connection != null) {
connection.close();
@@ -2362,7 +2312,6 @@ public class DatabaseUtil {
public String deleteForeignKey(ModelEntity entity, ModelRelation modelRelation, ModelEntity relModelEntity, int constraintNameClipLength) {
Connection connection = null;
Statement stmt = null;
try {
connection = getConnection();
@@ -2389,18 +2338,11 @@ public class DatabaseUtil {
sqlBuf.append(relConstraintName);
if (Debug.verboseOn()) Debug.logVerbose("[deleteForeignKey] sql=" + sqlBuf.toString(), module);
try {
stmt = connection.createStatement();
try (Statement stmt = connection.createStatement()) {
stmt.executeUpdate(sqlBuf.toString());
} catch (SQLException e) {
return "SQL Exception while executing the following:\n" + sqlBuf.toString() + "\nError was: " + e.toString();
} finally {
try {
if (stmt != null)
stmt.close();
} catch (SQLException e) {
Debug.logError(e, module);
}
try {
if (connection != null) {
connection.close();
@@ -2442,7 +2384,6 @@ public class DatabaseUtil {
if (entity.getPksSize() > 0) {
message = "Creating primary key for entity [" + entity.getEntityName() + "]";
Connection connection = null;
Statement stmt = null;
try {
connection = getConnection();
@@ -2468,19 +2409,11 @@ public class DatabaseUtil {
sqlBuf.append(")");
if (Debug.verboseOn()) Debug.logVerbose("[createPrimaryKey] sql=" + sqlBuf.toString(), module);
try {
stmt = connection.createStatement();
try (Statement stmt = connection.createStatement()) {
stmt.executeUpdate(sqlBuf.toString());
} catch (SQLException e) {
return "SQL Exception while executing the following:\n" + sqlBuf.toString() + "\nError was: " + e.toString();
} finally {
try {
if (stmt != null) {
stmt.close();
}
} catch (SQLException e) {
Debug.logError(e, module);
}
try {
if (connection != null) {
connection.close();
@@ -2525,7 +2458,6 @@ public class DatabaseUtil {
if (entity.getPksSize() > 0) {
message = "Deleting primary key for entity [" + entity.getEntityName() + "]";
Connection connection = null;
Statement stmt = null;
try {
connection = getConnection();
} catch (SQLException e) {
@@ -2555,20 +2487,13 @@ public class DatabaseUtil {
}
if (Debug.verboseOn()) Debug.logVerbose("[deletePrimaryKey] sql=" + sqlBuf.toString(), module);
try {
stmt = connection.createStatement();
try (Statement stmt = connection.createStatement()) {
stmt.executeUpdate(sqlBuf.toString());
} catch (SQLException e) {
String errMsg = "SQL Exception while executing the following:\n" + sqlBuf.toString() + "\nError was: " + e.toString();
Debug.logError(e, errMsg, module);
return errMsg;
} finally {
try {
if (stmt != null)
stmt.close();
} catch (SQLException e) {
Debug.logError(e, module);
}
try {
if (connection != null) {
connection.close();
@@ -2628,7 +2553,6 @@ public class DatabaseUtil {
public String createDeclaredIndex(ModelEntity entity, ModelIndex modelIndex) {
Connection connection = null;
Statement stmt = null;
try {
connection = getConnection();
@@ -2645,17 +2569,11 @@ public class DatabaseUtil {
String createIndexSql = makeIndexClause(entity, modelIndex);
if (Debug.verboseOn()) Debug.logVerbose("[createForeignKeyIndex] index sql=" + createIndexSql, module);
try {
stmt = connection.createStatement();
try (Statement stmt = connection.createStatement()) {
stmt.executeUpdate(createIndexSql);
} catch (SQLException e) {
return "SQL Exception while executing the following:\n" + createIndexSql + "\nError was: " + e.toString();
} finally {
try {
if (stmt != null) stmt.close();
} catch (SQLException e) {
Debug.logError(e, module);
}
try {
if (connection != null) {
connection.close();
@@ -2742,8 +2660,6 @@ public class DatabaseUtil {
public String deleteDeclaredIndex(ModelEntity entity, ModelIndex modelIndex) {
Connection connection = null;
Statement stmt = null;
try {
connection = getConnection();
} catch (SQLException e) {
@@ -2770,17 +2686,11 @@ public class DatabaseUtil {
String deleteIndexSql = indexSqlBuf.toString();
if (Debug.verboseOn()) Debug.logVerbose("[deleteDeclaredIndex] index sql=" + deleteIndexSql, module);
try {
stmt = connection.createStatement();
try (Statement stmt = connection.createStatement()) {
stmt.executeUpdate(deleteIndexSql);
} catch (SQLException e) {
return "SQL Exception while executing the following:\n" + deleteIndexSql + "\nError was: " + e.toString();
} finally {
try {
if (stmt != null) stmt.close();
} catch (SQLException e) {
Debug.logError(e, module);
}
try {
if (connection != null) {
connection.close();
@@ -2840,8 +2750,6 @@ public class DatabaseUtil {
public String createForeignKeyIndex(ModelEntity entity, ModelRelation modelRelation, int constraintNameClipLength) {
Connection connection = null;
Statement stmt = null;
try {
connection = getConnection();
} catch (SQLException e) {
@@ -2861,18 +2769,11 @@ public class DatabaseUtil {
if (Debug.verboseOn()) Debug.logVerbose("[createForeignKeyIndex] index sql=" + createIndexSql, module);
try {
stmt = connection.createStatement();
try (Statement stmt = connection.createStatement()) {
stmt.executeUpdate(createIndexSql);
} catch (SQLException e) {
return "SQL Exception while executing the following:\n" + createIndexSql + "\nError was: " + e.toString();
} finally {
try {
if (stmt != null)
stmt.close();
} catch (SQLException e) {
Debug.logError(e, module);
}
try {
if (connection != null) {
connection.close();
@@ -2959,8 +2860,6 @@ public class DatabaseUtil {
public String deleteForeignKeyIndex(ModelEntity entity, ModelRelation modelRelation, int constraintNameClipLength) {
Connection connection = null;
Statement stmt = null;
try {
connection = getConnection();
} catch (SQLException e) {
@@ -2990,19 +2889,11 @@ public class DatabaseUtil {
if (Debug.verboseOn()) Debug.logVerbose("[deleteForeignKeyIndex] index sql=" + deleteIndexSql, module);
try {
stmt = connection.createStatement();
try (Statement stmt = connection.createStatement()) {
stmt.executeUpdate(deleteIndexSql);
} catch (SQLException e) {
return "SQL Exception while executing the following:\n" + deleteIndexSql + "\nError was: " + e.toString();
} finally {
try {
if (stmt != null) {
stmt.close();
}
} catch (SQLException e) {
Debug.logError(e, module);
}
try {
if (connection != null) {
connection.close();
@@ -3045,8 +2936,6 @@ public class DatabaseUtil {
Connection connection = null;
try {
Statement stmt = null;
connection = getConnectionLogged(messages);
if (connection == null) {
return;
@@ -3069,20 +2958,12 @@ public class DatabaseUtil {
if (Debug.verboseOn()) Debug.logVerbose("[updateCharacterSetAndCollation] character-set and collate sql=" + sqlTableBuf, module);
try {
stmt = connection.createStatement();
try (Statement stmt = connection.createStatement()) {
stmt.executeUpdate(sqlTableBuf.toString());
} catch (SQLException e) {
String errMsg = "SQL Exception while executing the following:\n" + sqlTableBuf + "\nError was: " + e.toString();
messages.add(errMsg);
Debug.logError(errMsg, module);
} finally {
try {
if (stmt != null)
stmt.close();
} catch (SQLException e) {
Debug.logError(e, module);
}
}
Iterator<ModelField> fieldIter = entity.getFieldsIterator();
@@ -3124,20 +3005,12 @@ public class DatabaseUtil {
}
if (Debug.verboseOn()) Debug.logVerbose("[updateCharacterSetAndCollation] character-set and collate sql=" + sqlBuf, module);
try {
stmt = connection.createStatement();
try (Statement stmt = connection.createStatement()) {
stmt.executeUpdate(sqlBuf.toString());
} catch (SQLException e) {
String errMsg = "SQL Exception while executing the following:\n" + sqlBuf + "\nError was: " + e.toString();
messages.add(errMsg);
Debug.logError(errMsg, module);
} finally {
try {
if (stmt != null)
stmt.close();
} catch (SQLException e) {
Debug.logError(e, module);
}
}
}
} finally {