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,17 +79,23 @@ class SqliteManager(private val chunkmaster: Chunkmaster) {
fun executeStatement(sql: String, values: HashMap<Int, Any>, callback: ((ResultSet) -> Unit)?) {
val connection = getConnection()
if (connection != null) {
val statement = connection.prepareStatement(sql)
for (parameterValue in values) {
statement.setObject(parameterValue.key, parameterValue.value)
}
statement.execute()
val res = statement.resultSet
if (callback != null) {
callback(res)
try {
val statement = connection.prepareStatement(sql)
for (parameterValue in values) {
statement.setObject(parameterValue.key, parameterValue.value)
}
statement.execute()
val res = statement.resultSet
if (callback != null) {
callback(res)
}
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()
}
statement.close()
connection.close()
} else {
chunkmaster.logger.severe("Could not execute sql $sql. No database connection established.")
}

Loading…
Cancel
Save