diff --git a/src/main/kotlin/mdnet/MangaDexClient.kt b/src/main/kotlin/mdnet/MangaDexClient.kt index ad40187..c6f498c 100644 --- a/src/main/kotlin/mdnet/MangaDexClient.kt +++ b/src/main/kotlin/mdnet/MangaDexClient.kt @@ -70,7 +70,6 @@ class MangaDexClient(private val settingsFile: File, databaseFolder: Path, cache val config = HikariConfig() val db = databaseFolder.resolve("metadata.db") config.jdbcUrl = "jdbc:sqlite:$db" - config.maximumPoolSize = 1 config.addDataSourceProperty("cachePrepStmts", "true") config.addDataSourceProperty("prepStmtCacheSize", "100") config.addDataSourceProperty("prepStmtCacheSqlLimit", "1000") diff --git a/src/main/kotlin/mdnet/cache/ImageStorage.kt b/src/main/kotlin/mdnet/cache/ImageStorage.kt index 7e0ba1d..69d2db6 100644 --- a/src/main/kotlin/mdnet/cache/ImageStorage.kt +++ b/src/main/kotlin/mdnet/cache/ImageStorage.kt @@ -31,7 +31,7 @@ import org.ktorm.dsl.* import org.slf4j.LoggerFactory import java.io.* import java.nio.file.* -import java.sql.SQLIntegrityConstraintViolationException +import java.sql.SQLException import java.time.Instant import java.util.UUID import java.util.concurrent.* @@ -94,12 +94,14 @@ class ImageStorage( val now = Instant.now() LOGGER.info { "Updating LRU times for ${toUpdate.size} entries" } - database.batchUpdate(DbImage) { - for (id in toUpdate) { - item { - set(DbImage.accessed, now) - where { - DbImage.id eq id + synchronized(database) { + database.batchUpdate(DbImage) { + for (id in toUpdate) { + item { + set(DbImage.accessed, now) + where { + DbImage.id eq id + } } } } @@ -218,20 +220,20 @@ class ImageStorage( private fun deleteImage(id: String) { LOGGER.trace { "Deleting image $id from cache" } - database.useTransaction { - val path = getTempPath() + val path = getTempPath() - try { - Files.move( - getPath(id), - path, - StandardCopyOption.ATOMIC_MOVE - ) + try { + Files.move( + getPath(id), + path, + StandardCopyOption.ATOMIC_MOVE + ) - Files.deleteIfExists(path) - } catch (e: IOException) { - // a failure means the image did not exist - } finally { + Files.deleteIfExists(path) + } catch (e: IOException) { + // a failure means the image did not exist + } finally { + synchronized(database) { database.delete(DbImage) { DbImage.id eq id } @@ -312,12 +314,14 @@ class ImageStorage( Files.createDirectories(getPath(id).parent) try { - database.insert(DbImage) { - set(DbImage.id, id) - set(DbImage.accessed, Instant.now()) - set(DbImage.size, metadataSize + bytes) + synchronized(database) { + database.insert(DbImage) { + set(DbImage.id, id) + set(DbImage.accessed, Instant.now()) + set(DbImage.size, metadataSize + bytes) + } } - } catch (e: SQLIntegrityConstraintViolationException) { + } catch (e: SQLException) { // someone got to us before this (TOCTOU) // there are 2 situations here // one is that the