Sunday, June 3, 2012

misconception about h2 in memory db

Seems sometimes I have seen h2 url connection as "jdbc:h2:mem", which is different from "jdbc:h2:mem:".  The former will create a persistent db in the current path db called mem, the later will create a in memory db.  The official docs explains in in more detail h2 docs.

A good way to know if its in memory or not is just check your current path.  If you have mem.h2.db and  mem.trace.db on the file system, then you are not using the in memory db.

Most of the time though more than 1 connection is needed.  The first reaction is to remove the extra ":", rather than looking at the docs.  The correct fix will be to name the memory db such as "jdbc:h2:mem:foo".  Or if you use JPA sometimes the abstraction is just to thick, and most developers forget how connections are managed.  A connection is closed per test, in general this is the last connection which leads to new db across a test suite.  So to keep the db alive through out the lifetime of the JVM just add ";DB_CLOSE_DELAY=-1"

3 comments:

Unknown said...

Thank you for this clear explanation, it makes clear the difference between the two confusing jdbc:h2:mem and jdbc:h2:mem: URLs. I came through this situation before as you until I discovered your article.

Unknown said...

Thank you for this clear explanation, it makes clear the difference between the two confusing jdbc:h2:mem and jdbc:h2:mem: URLs. I came through this situation before as you until I discovered your article.

Unknown said...

Thank you for this clear explanation, it makes clear the difference between the two confusing jdbc:h2:mem and jdbc:h2:mem: URLs. I came through this situation before as you until I discovered your article.