You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ofbiz.apache.org by jl...@apache.org on 2016/09/03 12:49:47 UTC
svn commit: r1759088 -
/ofbiz/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/jdbc/DatabaseUtil.java
Author: jleroux
Date: Sat Sep 3 12:49:47 2016
New Revision: 1759088
URL: http://svn.apache.org/viewvc?rev=1759088&view=rev
Log:
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
Modified:
ofbiz/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/jdbc/DatabaseUtil.java
Modified: ofbiz/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/jdbc/DatabaseUtil.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/jdbc/DatabaseUtil.java?rev=1759088&r1=1759087&r2=1759088&view=diff
==============================================================================
--- ofbiz/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/jdbc/DatabaseUtil.java (original)
+++ ofbiz/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/jdbc/DatabaseUtil.java Sat Sep 3 12:49:47 2016
@@ -1613,8 +1613,6 @@ public class DatabaseUtil {
}
Connection connection = null;
- Statement stmt = null;
-
try {
connection = getConnection();
} catch (SQLException e) {
@@ -1729,18 +1727,12 @@ 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,8 +1769,7 @@ 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();
@@ -1787,11 +1777,6 @@ public class DatabaseUtil {
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,20 +1917,12 @@ 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) {
@@ -2004,13 +1978,6 @@ public class DatabaseUtil {
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();
@@ -2044,13 +2010,6 @@ public class DatabaseUtil {
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,20 +2170,12 @@ 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,19 +2338,12 @@ 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,20 +2409,12 @@ 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,8 +2487,7 @@ 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();
@@ -2564,12 +2495,6 @@ public class DatabaseUtil {
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,18 +2569,12 @@ 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,18 +2686,12 @@ 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,19 +2769,12 @@ 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,20 +2889,12 @@ 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 {