H2 Database Corruption

After patching several of our Kubernetes servers, I noticed that Lenses won’t start up any longer and the container continually crash-loops. When I look at the logs I noticed the following error message just before the pod is killed:

Caused by: org.h2.jdbc.JdbcSQLNonTransientConnectionException: File corrupted while reading record: null. Possible solution: use the recovery tool [90030-199]

I’m not sure how to use a recovery tool on the db file(s) or if there’s another way to restore it so Lenses will be able to successfully start up.

You can recover the database following the next steps

  1. Bash into the Lenses pod
  2. Stop Lenses process
supervisorctl stop lenses
  1. Run Recover command
java -cp /opt/lenses/lib/h2-1.4.199.jar org.h2.tools.Recover -dir /data/lenses -db lensesdb
  1. The previous command creates some files with the data to restore
java -cp /opt/lenses/lib/h2-1.4.199.jar org.h2.tools.RunScript -url jdbc:h2:/data/lenses/lenses_new -script /data/lenses/lensesdb.h2.sql
  1. Replace the old file
rm /data/lenses/lenses.mv.db mv /data/lenses/lenses_new.mv.db /data/lenses/lensesdb.mv.db
  1. Restart lenses
supervisorctl start lenses

If you still see the error such as

root@lenses / $ java -cp /opt/lenses/lib/h2-1.4.199.jar org.h2.tools.Recover  -dir /data/storage -db lensesdb
Exception in thread "main" java.lang.IllegalStateException: File corrupted in chunk 10847521, expected page length 4..3072, got 942813497 [1.4.199/6]
        at org.h2.mvstore.DataUtils.newIllegalStateException(DataUtils.java:883)
        at org.h2.mvstore.MVStore.readBufferForPage(MVStore.java:1055)
        at org.h2.mvstore.MVStore.readPage(MVStore.java:2186)
        at org.h2.mvstore.MVMap.readPage(MVMap.java:554)
        at org.h2.mvstore.Page$NonLeaf.getChildPage(Page.java:1086)
        at org.h2.mvstore.Cursor.hasNext(Cursor.java:53)
        at org.h2.mvstore.MVStore.readStoreHeader(MVStore.java:793)
        at org.h2.mvstore.MVStore.<init>(MVStore.java:401)
        at org.h2.mvstore.MVStore$Builder.open(MVStore.java:3343)
        at org.h2.mvstore.MVStoreTool.info(MVStoreTool.java:347)
        at org.h2.tools.Recover.process(Recover.java:344)
        at org.h2.tools.Recover.runTool(Recover.java:195)
        at org.h2.tools.Recover.main(Recover.java:158)
root@lenses / $ 

Try to download h2-1.4.200.jar from Maven and used it instead to perform the same Recover command with that version.
Afterwards you can reuse ** h2-1.4.199** to perform RunScript and to create new database file.