Search code examples
databasescalamapdb

mapdb how to persist cross restart


I use mapdb as following

val mycache = DBMaker.newFileDB(new File(("/data/tmp/cache.db")))
    .transactionDisable()
    .make().getHashSet("")

then when i do

mycache.put(k1, v1)
assertTrue(mycache.get(k1), v1) // all is fine

however if i restart my server i do see i have cache.db on disk however it will have an empty map when reading

so

mycache.get(k1) // is null after restart

how can i have it reread my map after restart from file?


Solution

  • you have to close database before JVM shutdown. Easiest is to add .closeOnJVMShutdown() option