Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use try-with-resources for JDBC AutoCloseable resources #233

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 4 additions & 10 deletions base-component/tools/screen/Tools/Entity/SqlRunner.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ along with this software (see the LICENSE.md file). If not, see

ExecutionContext ec = context.ec

def rs = null
outerColumnsList = new ArrayList<ArrayList<String>>()
outerRecordsList = new ArrayList<ArrayList<ArrayList>>()

Expand All @@ -39,10 +38,8 @@ along with this software (see the LICENSE.md file). If not, see
sql = ec.web.secureRequestParameters.get("sql")
if (sql && groupName) {
messageList = []
Connection con = ec.entity.getConnection(groupName)
try {
stmt = con.createStatement()

try (Connection con = ec.entity.getConnection(groupName);
stmt = con.createStatement()) {
for(int i=0; i< 100; i++) {
// try get 100 ResultSets, avoid infinite loop
boolean isResultSet = i == 0 ? stmt.execute(sql as String) : stmt.getMoreResults()
Expand All @@ -61,7 +58,7 @@ along with this software (see the LICENSE.md file). If not, see
records = new ArrayList<ArrayList>()
outerColumnsList.add(columns)
outerRecordsList.add(records)
rs = stmt.getResultSet()
try (rs = stmt.getResultSet()) {
if (rs != null) {
rsmd = rs.getMetaData()
columnCount = rsmd.getColumnCount()
Expand All @@ -77,14 +74,14 @@ along with this software (see the LICENSE.md file). If not, see
for (i = 1; i <= columnCount; i++) record.add(rs.getObject(i))
records.add(record)
}
rs.close()

if (limitReached) {
messageList.add(ec.resource.expand('Only showing first ${limit} rows.',''))
} else {
messageList.add(ec.resource.expand('Showing all ${records.size()} results.',''))
}
}
}
} else if ((rowsAffected = stmt.getUpdateCount()) != -1){
messageList.add(ec.resource.expand('Query altered ${rowsAffected} rows.',''))
} else {
Expand All @@ -94,9 +91,6 @@ along with this software (see the LICENSE.md file). If not, see
} catch (Exception e) {
messageList.add(e.toString())
ec.logger.log(200, "Error running SQL query in SqlRunner", e)
} finally {
if (stmt != null) { try { stmt.close() } catch (Exception e) { /* Ignore */ } }
if (con != null) { try { con.close() } catch (Exception e) { /* Ignore */ } }
}
}
]]></script>
Expand Down
17 changes: 3 additions & 14 deletions base-component/tools/screen/Tools/Entity/SqlScriptRunner.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,16 @@ along with this software (see the LICENSE.md file). If not, see

ExecutionContext ec = context.ec

def rs = null
int limitInt = 1

// make sure SQL comes in secure parameter (body, etc no URL)
String sqlScript = ec.web.secureRequestParameters.get("sql")
if (sqlScript && groupName) {
sqlList = sqlScript.split(';')
messageList = []
Connection con = ec.entity.getConnection(groupName)
try {
try (Connection con = ec.entity.getConnection(groupName)) {
for (sql in sqlList) {
try {
stmt = con.createStatement()

try (stmt = con.createStatement()) {
boolean isResultSet = stmt.execute(sql as String)

SQLWarning w = stmt.getWarnings()
Expand All @@ -53,11 +49,8 @@ along with this software (see the LICENSE.md file). If not, see
stmt.clearWarnings()

if (isResultSet) {
rs = stmt.getResultSet()
try {
try (rs = stmt.getResultSet()) {
// do stuff with result set (like displaying it see SqlRunner.xml)
} finally {
rs.close()
}
} else if ((rowsAffected = stmt.getUpdateCount()) != -1){
messageList.add(ec.resource.expand('Query altered ${rowsAffected} rows.',''))
Expand All @@ -66,12 +59,8 @@ along with this software (see the LICENSE.md file). If not, see
} catch (Exception e) {
messageList.add(e.toString())
ec.logger.log(200, "Error running SQL query in SqlRunner", e)
} finally {
if (stmt != null) { try { stmt.close() } catch (Exception e) { /* Ignore */ } }
}
}
} finally {
if (con != null) { try { con.close() } catch (Exception e) { /* Ignore */ } }
}

}
Expand Down