1
0
mirror of https://github.com/fabianonline/telegram_backup.git synced 2024-11-23 01:06:17 +00:00

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

View File

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