Reset Embedded H2 database periodically

javydreamercsw picture javydreamercsw · Dec 15, 2011 · Viewed 58.4k times · Source

I'm setting up a new version of my application in a demo server and would love to find a way of resetting the database daily. I guess I can always have a cron job executing drop and create queries but I'm looking for a cleaner approach. I tried using a special persistence unit with drop-create approach but it doesn't work as the system connects and disconnects from the server frequently (on demand).

Is there a better approach?

Answer

Thomas Mueller picture Thomas Mueller · Dec 15, 2011

H2 supports a special SQL statement to drop all objects:

DROP ALL OBJECTS [DELETE FILES]

If you don't want to drop all tables, you might want to use truncate table:

TRUNCATE TABLE