Added connection close safety

- moved connection close outside of try catch to finally to ensure that the connection is closed even on error (SqliteManager)
pull/15/head
Trivernis 5 years ago
parent 8b63643f7a
commit 3a0012ef3f

@ -79,6 +79,7 @@ class SqliteManager(private val chunkmaster: Chunkmaster) {
fun executeStatement(sql: String, values: HashMap<Int, Any>, callback: ((ResultSet) -> Unit)?) { fun executeStatement(sql: String, values: HashMap<Int, Any>, callback: ((ResultSet) -> Unit)?) {
val connection = getConnection() val connection = getConnection()
if (connection != null) { if (connection != null) {
try {
val statement = connection.prepareStatement(sql) val statement = connection.prepareStatement(sql)
for (parameterValue in values) { for (parameterValue in values) {
statement.setObject(parameterValue.key, parameterValue.value) statement.setObject(parameterValue.key, parameterValue.value)
@ -89,7 +90,12 @@ class SqliteManager(private val chunkmaster: Chunkmaster) {
callback(res) callback(res)
} }
statement.close() statement.close()
} catch (e: Exception) {
chunkmaster.logger.severe("An error occured on sql $sql. ${e.message}")
chunkmaster.logger.info(ExceptionUtils.getStackTrace(e))
} finally {
connection.close() connection.close()
}
} else { } else {
chunkmaster.logger.severe("Could not execute sql $sql. No database connection established.") chunkmaster.logger.severe("Could not execute sql $sql. No database connection established.")
} }

Loading…
Cancel
Save