Fixed lots of unclosed ResultSets in Database and DatabaseUpdates.

This commit is contained in:
Fabian Schlenz 2018-03-12 22:01:47 +01:00
parent d796cb1bf0
commit e75aa2101e
2 changed files with 35 additions and 28 deletions

View File

@ -56,7 +56,9 @@ class Database private constructor(var client: TelegramClient) {
try {
val rs = stmt!!.executeQuery("SELECT MAX(message_id) FROM messages WHERE source_type IN ('group', 'dialog')")
rs.next()
return rs.getInt(1)
val result = rs.getInt(1)
rs.close()
return result
} catch (e: SQLException) {
return 0
}
@ -87,6 +89,7 @@ class Database private constructor(var client: TelegramClient) {
missing.add(i)
}
}
rs.close()
return missing
} catch (e: SQLException) {
e.printStackTrace()
@ -111,16 +114,7 @@ class Database private constructor(var client: TelegramClient) {
}
fun getMessagesFromUserCount(): Int {
try {
val rs = stmt!!.executeQuery("SELECT COUNT(*) FROM messages WHERE sender_id=" + user_manager.user!!.getId())
rs.next()
return rs.getInt(1)
} catch (e: SQLException) {
throw RuntimeException(e)
}
}
fun getMessagesFromUserCount() = queryInt("SELECT COUNT(*) FROM messages WHERE sender_id=" + user_manager.user!!.getId())
fun getMessageTypesWithCount(): HashMap<String, Int> = getMessageTypesWithCount(GlobalChat())
@ -164,7 +158,9 @@ class Database private constructor(var client: TelegramClient) {
try {
val rs = stmt!!.executeQuery("PRAGMA encoding")
rs.next()
return rs.getString(1)
val result = rs.getString(1)
rs.close()
return result
} catch (e: SQLException) {
logger.debug("SQLException: {}", e)
return "unknown"
@ -255,9 +251,7 @@ class Database private constructor(var client: TelegramClient) {
}
fun getTopMessageIDForChannel(id: Int): Int {
return queryInt("SELECT MAX(message_id) FROM messages WHERE source_id=$id AND source_type IN('channel', 'supergroup')")
}
fun getTopMessageIDForChannel(id: Int): Int = queryInt("SELECT MAX(message_id) FROM messages WHERE source_id=$id AND source_type IN('channel', 'supergroup')")
fun logRun(start_id: Int, end_id: Int, count: Int) {
try {
@ -269,6 +263,7 @@ class Database private constructor(var client: TelegramClient) {
ps.setInt(2, end_id)
ps.setInt(3, count)
ps.execute()
ps.close()
} catch (e: SQLException) {
}
@ -278,7 +273,9 @@ class Database private constructor(var client: TelegramClient) {
try {
val rs = stmt!!.executeQuery(query)
rs.next()
return rs.getInt(1)
val result = rs.getInt(1)
rs.close()
return result
} catch (e: SQLException) {
throw RuntimeException("Could not get count of messages.")
}
@ -431,6 +428,9 @@ class Database private constructor(var client: TelegramClient) {
ps_insert_or_ignore.clearBatch()
conn!!.commit()
conn!!.setAutoCommit(true)
ps.close()
ps_insert_or_ignore.close()
} catch (e: Exception) {
e.printStackTrace()
throw RuntimeException("Exception shown above happened.")
@ -486,6 +486,9 @@ class Database private constructor(var client: TelegramClient) {
ps_insert_or_replace.clearBatch()
conn!!.commit()
conn!!.setAutoCommit(true)
ps_insert_or_ignore.close()
ps_insert_or_replace.close()
} catch (e: Exception) {
e.printStackTrace()
throw RuntimeException("Exception shown above happened.")
@ -535,6 +538,9 @@ class Database private constructor(var client: TelegramClient) {
ps_insert_or_replace.clearBatch()
conn!!.commit()
conn!!.setAutoCommit(true)
ps_insert_or_ignore.close()
ps_insert_or_replace.close()
} catch (e: Exception) {
e.printStackTrace()
throw RuntimeException("Exception shown above happened.")
@ -564,6 +570,7 @@ class Database private constructor(var client: TelegramClient) {
while (rs.next()) {
map.put("count.messages.type." + rs.getString(1), rs.getInt(2))
}
rs.close()
return map
} catch (e: Exception) {
throw RuntimeException(e)
@ -586,6 +593,7 @@ class Database private constructor(var client: TelegramClient) {
map.put("count.messages.media_type.$s", rs.getInt(2))
}
map.put("count.messages.media_type.any", count)
rs.close()
return map
} catch (e: Exception) {
throw RuntimeException(e)
@ -625,6 +633,7 @@ class Database private constructor(var client: TelegramClient) {
}
map.put("authors.count.others", count_others)
map.put("authors.all", all_data)
rs.close()
return map
} catch (e: Exception) {
throw RuntimeException(e)
@ -634,7 +643,9 @@ class Database private constructor(var client: TelegramClient) {
fun getMessageCountForExport(c: AbstractChat): Int {
val rs = stmt!!.executeQuery("SELECT COUNT(*) FROM messages WHERE " + c.query);
rs.next()
return rs.getInt(1)
val result = rs.getInt(1)
rs.close()
return result
}
fun getMessageTimesMatrix(c: AbstractChat): Array<IntArray> {
@ -647,6 +658,7 @@ class Database private constructor(var client: TelegramClient) {
while (rs.next()) {
result[if (rs.getInt(1) == 0) 6 else rs.getInt(1) - 1][rs.getInt(2)] = rs.getInt(3)
}
rs.close()
return result
} catch (e: Exception) {
throw RuntimeException(e)

View File

@ -37,25 +37,19 @@ class DatabaseUpdates(protected var conn: Connection, protected var db: Database
fun doUpdates() {
try {
val stmt = conn.createStatement()
var rs: ResultSet
logger.debug("DatabaseUpdate.doUpdates running")
logger.debug("Getting current database version")
var version: Int
logger.debug("Checking if table database_versions exists")
rs = stmt.executeQuery("SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND name='database_versions'")
rs.next()
if (rs.getInt(1) == 0) {
val table_count = db.queryInt("SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND name='database_versions'")
if (table_count == 0) {
logger.debug("Table does not exist")
version = 0
} else {
logger.debug("Table exists. Checking max version")
rs.close()
rs = stmt.executeQuery("SELECT MAX(version) FROM database_versions")
rs.next()
version = rs.getInt(1)
version = db.queryInt("SELECT MAX(version) FROM database_versions")
}
rs.close()
logger.debug("version: {}", version)
System.out.println("Database version: " + version)
logger.debug("Max available database version is {}", maxPossibleVersion)
@ -106,6 +100,9 @@ class DatabaseUpdates(protected var conn: Connection, protected var db: Database
} catch (e: SQLException) {
throw RuntimeException(e)
}
println("Cleaning up the database (this might take some time)...")
try { stmt.executeUpdate("VACUUM") } catch (t: Throwable) { logger.debug("Exception during VACUUMing: {}", t) }
} else {
logger.debug("No update necessary.")
@ -441,7 +438,5 @@ internal class DB_Update_9(conn: Connection, db: Database) : DatabaseUpdate(conn
}
println()
logger.info("Converted ${i} of ${count} messages.")
println(" Cleaning up the database (this might also take some time, sorry)...")
execute("VACUUM")
}
}