kotlinkotlin-exposed

New rows inserted with Jetbrains Exposed are not visible until application restarts


I have this simple storage service. I can create Movies and I can retrieve Movies. However, when I create a Movie it is not returned by all until I restart the entire web app. I presume this is some kind of cache problem, but I can't figure out how. What am I doing wrong?

class SqliteStorageService : StorageService {
    init {
        TransactionManager.defaultDatabase = Database.connect("jdbc:sqlite:.movieq.db", "org.sqlite.JDBC")
        TransactionManager.manager.defaultIsolationLevel = Connection.TRANSACTION_SERIALIZABLE
        transaction {
            SchemaUtils.create(MoviesTable)
        }
    }

    override val all = transaction {
        MovieEntity.all().toList().map { it.toMovie() }
    }

    override fun save(movie: Movie) {
        transaction {
            MovieEntity.new {
                imdbId = movie.imdbId
                reason = movie.reason
                title = movie.title
                synopsis = movie.synopsis
            }
        }
    }
}

Solution

  • Because all is a property that is initialized during object-construction, it will be initialized to whatever is in your database-table before you call save.

    You need to change it to a function-call, so it actually gets reevaluated after you've called save.

    override fun all() = transaction {
        MovieEntity.all().toList().map { it.toMovie() }
    }