add try finally for closing db resources

This commit is contained in:
John Woodlock
2012-07-11 17:01:17 +10:00
parent e4d5325f31
commit 40f7627eb2
2 changed files with 64 additions and 69 deletions
@@ -59,15 +59,16 @@ public class ReadExtraDataAndReportingServiceImpl implements
ClassicEngineBoot.getInstance().start(); ClassicEngineBoot.getInstance().start();
noPentaho = false; noPentaho = false;
Connection db_connection = null;
try { try {
this.dataSource = dataSource; this.dataSource = dataSource;
Connection db_connection = dataSource.getConnection(); db_connection = dataSource.getConnection();
this.reportingMetaDataDB = db_connection.getCatalog(); this.reportingMetaDataDB = db_connection.getCatalog();
db_connection.close();
db_connection = null;
} catch (SQLException e) { } catch (SQLException e) {
throw new PlatformDataIntegrityException("error.msg.sql.error", throw new PlatformDataIntegrityException("error.msg.sql.error",
e.getMessage(), "DataSource: " + dataSource); e.getMessage(), "DataSource: " + dataSource);
} finally {
dbClose(null, db_connection);
} }
} }
@@ -197,9 +198,11 @@ public class ReadExtraDataAndReportingServiceImpl implements
GenericResultset result = new GenericResultset(); GenericResultset result = new GenericResultset();
Connection db_connection = null;
Statement db_statement = null;
try { try {
Connection db_connection = dataSource.getConnection(); db_connection = dataSource.getConnection();
Statement db_statement = db_connection.createStatement(); db_statement = db_connection.createStatement();
ResultSet rs = db_statement.executeQuery(sql); ResultSet rs = db_statement.executeQuery(sql);
ResultSetMetaData rsmd = rs.getMetaData(); ResultSetMetaData rsmd = rs.getMetaData();
@@ -229,14 +232,11 @@ public class ReadExtraDataAndReportingServiceImpl implements
} }
result.setData(resultsetDataRows); result.setData(resultsetDataRows);
db_statement.close();
db_statement = null;
db_connection.close();
db_connection = null;
} catch (SQLException e) { } catch (SQLException e) {
throw new PlatformDataIntegrityException("error.msg.sql.error", throw new PlatformDataIntegrityException("error.msg.sql.error",
e.getMessage(), "Sql: " + sql); e.getMessage(), "Sql: " + sql);
} finally {
dbClose(db_statement, db_connection);
} }
return result; return result;
@@ -315,9 +315,11 @@ public class ReadExtraDataAndReportingServiceImpl implements
private String getSql(String inputSql) { private String getSql(String inputSql) {
String sql = null; String sql = null;
Connection db_connection = null;
Statement db_statement = null;
try { try {
Connection db_connection = dataSource.getConnection(); db_connection = dataSource.getConnection();
Statement db_statement = db_connection.createStatement(); db_statement = db_connection.createStatement();
ResultSet rs = db_statement.executeQuery(inputSql); ResultSet rs = db_statement.executeQuery(inputSql);
if (rs.next()) { if (rs.next()) {
@@ -326,13 +328,11 @@ public class ReadExtraDataAndReportingServiceImpl implements
throw new ReportNotFoundException(inputSql); throw new ReportNotFoundException(inputSql);
} }
db_statement.close();
db_statement = null;
db_connection.close();
db_connection = null;
} catch (SQLException e) { } catch (SQLException e) {
throw new PlatformDataIntegrityException("error.msg.sql.error", throw new PlatformDataIntegrityException("error.msg.sql.error",
e.getMessage(), "Input Sql: " + inputSql); e.getMessage(), "Input Sql: " + inputSql);
} finally {
dbClose(db_statement, db_connection);
} }
return sql; return sql;
@@ -343,9 +343,11 @@ public class ReadExtraDataAndReportingServiceImpl implements
List<AdditionalFieldsSet> additionalFieldsSets = new ArrayList<AdditionalFieldsSet>(); List<AdditionalFieldsSet> additionalFieldsSets = new ArrayList<AdditionalFieldsSet>();
Connection db_connection = null;
Statement db_statement = null;
try { try {
Connection db_connection = dataSource.getConnection(); db_connection = dataSource.getConnection();
Statement db_statement = db_connection.createStatement(); db_statement = db_connection.createStatement();
String whereClause; String whereClause;
if (type == null) { if (type == null) {
@@ -364,13 +366,11 @@ public class ReadExtraDataAndReportingServiceImpl implements
.getString("type"))); .getString("type")));
} }
db_statement.close();
db_statement = null;
db_connection.close();
db_connection = null;
} catch (SQLException e) { } catch (SQLException e) {
throw new PlatformDataIntegrityException("error.msg.sql.error", throw new PlatformDataIntegrityException("error.msg.sql.error",
e.getMessage(), "Additional Fields Type: " + type); e.getMessage(), "Additional Fields Type: " + type);
} finally {
dbClose(db_statement, db_connection);
} }
return additionalFieldsSets; return additionalFieldsSets;
@@ -391,9 +391,13 @@ public class ReadExtraDataAndReportingServiceImpl implements
GenericResultset result = new GenericResultset(); GenericResultset result = new GenericResultset();
Connection db_connection = null;
Statement db_statement1 = null;
Statement db_statement2 = null;
Statement db_statement3 = null;
try { try {
Connection db_connection = dataSource.getConnection(); db_connection = dataSource.getConnection();
Statement db_statement1 = db_connection.createStatement(); db_statement1 = db_connection.createStatement();
String sql = "select f.`name`, f.data_type, f.data_length, f.display_type, f.allowed_list_id from stretchydata_datasettype t join stretchydata_dataset d on d.datasettype_id = t.id join stretchydata_dataset_fields f on f.dataset_id = d.id where d.`name` = '" String sql = "select f.`name`, f.data_type, f.data_length, f.display_type, f.allowed_list_id from stretchydata_datasettype t join stretchydata_dataset d on d.datasettype_id = t.id join stretchydata_dataset_fields f on f.dataset_id = d.id where d.`name` = '"
+ set + "' and t.`name` = '" + type + "' order by f.id"; + set + "' and t.`name` = '" + type + "' order by f.id";
@@ -402,7 +406,7 @@ public class ReadExtraDataAndReportingServiceImpl implements
if (rsmd.next()) { if (rsmd.next()) {
String fullDatasetName = getFullDatasetName(type, set); String fullDatasetName = getFullDatasetName(type, set);
Statement db_statement2 = db_connection.createStatement(); db_statement2 = db_connection.createStatement();
List<ResultsetColumnHeader> columnHeaders = new ArrayList<ResultsetColumnHeader>(); List<ResultsetColumnHeader> columnHeaders = new ArrayList<ResultsetColumnHeader>();
Boolean firstColumn = true; Boolean firstColumn = true;
Integer allowedListId; Integer allowedListId;
@@ -441,14 +445,12 @@ public class ReadExtraDataAndReportingServiceImpl implements
columnHeaders.add(rsch); columnHeaders.add(rsch);
} while (rsmd.next()); } while (rsmd.next());
result.setColumnHeaders(columnHeaders); result.setColumnHeaders(columnHeaders);
db_statement2.close();
db_statement2 = null;
sql = "select " + selectFieldList + " from `" + type sql = "select " + selectFieldList + " from `" + type
+ "` t left join `" + fullDatasetName + "` t left join `" + fullDatasetName
+ "` s on s.id = t.id " + " where t.id = " + id; + "` s on s.id = t.id " + " where t.id = " + id;
Statement db_statement3 = db_connection.createStatement(); db_statement3 = db_connection.createStatement();
ResultSet rs = db_statement3.executeQuery(sql); ResultSet rs = db_statement3.executeQuery(sql);
if (rs.next()) { if (rs.next()) {
@@ -472,20 +474,17 @@ public class ReadExtraDataAndReportingServiceImpl implements
} else { } else {
throw new AdditionalFieldsNotFoundException(type, id); throw new AdditionalFieldsNotFoundException(type, id);
} }
db_statement3.close();
db_statement3 = null;
} else { } else {
throw new AdditionalFieldsNotFoundException(type, set); throw new AdditionalFieldsNotFoundException(type, set);
} }
db_statement1.close();
db_statement1 = null;
db_connection.close();
db_connection = null;
} catch (SQLException e) { } catch (SQLException e) {
throw new PlatformDataIntegrityException("error.msg.sql.error", throw new PlatformDataIntegrityException("error.msg.sql.error",
e.getMessage(), "Additional Fields Type: " + type e.getMessage(), "Additional Fields Type: " + type
+ " Set: " + set + " Id: " + id); + " Set: " + set + " Id: " + id);
} finally {
dbClose(db_statement2, null);
dbClose(db_statement3, null);
dbClose(db_statement1, db_connection);
} }
return result; return result;
@@ -522,19 +521,19 @@ public class ReadExtraDataAndReportingServiceImpl implements
String saveSql = getSaveSql(fullDatasetName, id, transType, queryParams); String saveSql = getSaveSql(fullDatasetName, id, transType, queryParams);
logger.info("saveSQL: " + saveSql); logger.info("saveSQL: " + saveSql);
Connection db_connection = null;
Statement db_statement = null;
try { try {
Connection db_connection = dataSource.getConnection(); db_connection = dataSource.getConnection();
Statement db_statement = db_connection.createStatement(); db_statement = db_connection.createStatement();
db_statement.executeUpdate(saveSql); db_statement.executeUpdate(saveSql);
db_statement.close();
db_statement = null;
db_connection.close();
db_connection = null;
} catch (SQLException e) { } catch (SQLException e) {
throw new PlatformDataIntegrityException("error.msg.sql.error", throw new PlatformDataIntegrityException("error.msg.sql.error",
e.getMessage(), "Additional Fields Type: " + type e.getMessage(), "Additional Fields Type: " + type
+ " Set: " + set + " Id: " + id); + " Set: " + set + " Id: " + id);
} finally {
dbClose(db_statement, db_connection);
} }
} }
@@ -542,28 +541,24 @@ public class ReadExtraDataAndReportingServiceImpl implements
private void checkResourceTypeThere(String type, String set) { private void checkResourceTypeThere(String type, String set) {
String sql = "select 'f' from stretchydata_datasettype t join stretchydata_dataset d on d.datasettype_id = t.id where d.`name` = '" String sql = "select 'f' from stretchydata_datasettype t join stretchydata_dataset d on d.datasettype_id = t.id where d.`name` = '"
+ set + "' and t.`name` = '" + type + "'"; + set + "' and t.`name` = '" + type + "'";
try {
Connection db_connection = dataSource.getConnection();
Statement db_statement = db_connection.createStatement();
Connection db_connection = null;
Statement db_statement = null;
try {
db_connection = dataSource.getConnection();
db_statement = db_connection.createStatement();
ResultSet rs = db_statement.executeQuery(sql); ResultSet rs = db_statement.executeQuery(sql);
if (!(rs.next())) { if (!(rs.next())) {
db_statement.close();
db_statement = null;
db_connection.close();
db_connection = null;
throw new AdditionalFieldsNotFoundException(type, set); throw new AdditionalFieldsNotFoundException(type, set);
} }
db_statement.close();
db_statement = null;
db_connection.close();
db_connection = null;
} catch (SQLException e) { } catch (SQLException e) {
throw new PlatformDataIntegrityException("error.msg.sql.error", throw new PlatformDataIntegrityException("error.msg.sql.error",
e.getMessage(), "Additional Fields Type: " + type e.getMessage(), "Additional Fields Type: " + type
+ " Set: " + set + " Sql: " + sql); + " Set: " + set + " Sql: " + sql);
} finally {
dbClose(db_statement, db_connection);
} }
} }
@@ -574,9 +569,11 @@ public class ReadExtraDataAndReportingServiceImpl implements
String sql = "select s.id from `" + type + "` t left join `" String sql = "select s.id from `" + type + "` t left join `"
+ fullDatasetName + "` s on s.id = t.id where t.id = " + id; + fullDatasetName + "` s on s.id = t.id where t.id = " + id;
Connection db_connection = null;
Statement db_statement = null;
try { try {
Connection db_connection = dataSource.getConnection(); db_connection = dataSource.getConnection();
Statement db_statement = db_connection.createStatement(); db_statement = db_connection.createStatement();
ResultSet rs = db_statement.executeQuery(sql); ResultSet rs = db_statement.executeQuery(sql);
if (rs.next()) { if (rs.next()) {
@@ -587,21 +584,15 @@ public class ReadExtraDataAndReportingServiceImpl implements
transType = "A"; transType = "A";
} }
} else { } else {
db_statement.close();
db_statement = null;
db_connection.close();
db_connection = null;
throw new AdditionalFieldsNotFoundException(type, id); throw new AdditionalFieldsNotFoundException(type, id);
} }
db_statement.close();
db_statement = null;
db_connection.close();
db_connection = null;
} catch (SQLException e) { } catch (SQLException e) {
throw new PlatformDataIntegrityException("error.msg.sql.error", throw new PlatformDataIntegrityException("error.msg.sql.error",
e.getMessage(), "Additional Fields Type: " + type e.getMessage(), "Additional Fields Type: " + type
+ " Full Set Name: " + fullDatasetName + " Full Set Name: " + fullDatasetName
+ " Sql: " + sql); + " Sql: " + sql);
} finally {
dbClose(db_statement, db_connection);
} }
return transType; return transType;
@@ -680,28 +671,32 @@ public class ReadExtraDataAndReportingServiceImpl implements
String reportType = ""; String reportType = "";
logger.info("get reportType: " + sql); logger.info("get reportType: " + sql);
Connection db_connection = null;
Statement db_statement = null;
ResultSet rs = null;
try { try {
Connection db_connection = dataSource.getConnection(); db_connection = dataSource.getConnection();
Statement db_statement = db_connection.createStatement(); db_statement = db_connection.createStatement();
ResultSet rs = db_statement.executeQuery(sql); rs = db_statement.executeQuery(sql);
if (rs.next()) { if (rs.next()) {
reportType = rs.getString("report_type"); reportType = rs.getString("report_type");
} else { } else {
dbClose(db_statement, db_connection);
throw new ReportNotFoundException(sql); throw new ReportNotFoundException(sql);
} }
dbClose(db_statement, db_connection);
} catch (SQLException e) { } catch (SQLException e) {
throw new PlatformDataIntegrityException("error.msg.sql.error", throw new PlatformDataIntegrityException("error.msg.sql.error",
e.getMessage(), "Report Name: " + reportName + " Sql: " e.getMessage(), "Report Name: " + reportName + " Sql: "
+ sql); + sql);
} finally {
dbClose(db_statement, db_connection);
} }
return reportType; return reportType;
} }
private void dbClose(Statement db_statement, Connection db_connection) { private void dbClose(Statement db_statement, Connection db_connection) {
logger.info("dbClose");
try { try {
if (db_statement != null) { if (db_statement != null) {
db_statement.close(); db_statement.close();
@@ -100,10 +100,10 @@ public class ClientReadPlatformServiceImpl implements ClientReadPlatformService
ClientMapper rm = new ClientMapper(offices); ClientMapper rm = new ClientMapper(offices);
String sql = "select " + rm.clientSchema() String sql = "select " + rm.clientSchema()
+ " where c.id = ? and c.org_id = ?"; + " where c.id = ? ";
return this.jdbcTemplate.queryForObject(sql, rm, new Object[] { return this.jdbcTemplate.queryForObject(sql, rm, new Object[] {
clientId, currentUser.getOrganisation().getId() }); clientId });
} catch (EmptyResultDataAccessException e) { } catch (EmptyResultDataAccessException e) {
throw new ClientNotFoundException(clientId); throw new ClientNotFoundException(clientId);
} }