How to start and shutdown database cleanly using embedded mode, Spring and Hibernate

2,295 views
Skip to first unread message

tolyen

unread,
Aug 22, 2008, 6:21:38 AM8/22/08
to H2 Database
Hello,

I'm putting together a simple web application that has H2 in embedded
mode, Spring and Hibernate. I've got everything working now, but the
problem is that the database won't shut down gracefully when Tomcat
shuts down. I have defined the db properties in hibernate.cfg.xml and
use that to create sessionFactory in Spring config. But when the
servlet container is shut down, I have not yet found a way to shutdown
the database properly. First, there are some stacktraces in the logs
when Tomcat shuts down, like:

INFO: Illegal access: this web application instance has been stopped
already. Could not load org.h2.util.CacheObject$1. The eventual
following stack trace is caused by an error thrown for debugging
purposes as well as to attempt to terminate the thread which caused
the illegal access, and has no functional impact.
java.lang.IllegalStateException
at
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:
1273)
at
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:
1233)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:
319)
at org.h2.util.CacheObject.sort(CacheObject.java:65)
at org.h2.store.DiskFile.flush(DiskFile.java:436)
at org.h2.index.BtreeIndex.flush(BtreeIndex.java:190)
at org.h2.index.BtreeIndex.close(BtreeIndex.java:205)
at org.h2.table.TableData.close(TableData.java:83)
at org.h2.engine.Database.close(Database.java:1024)
at org.h2.engine.Database.removeSession(Database.java:963)
at org.h2.engine.Session.close(Session.java:431)
at org.h2.engine.Database.close(Database.java:996)
at org.h2.engine.DatabaseCloser.run(DatabaseCloser.java:73)

and on top of that, the lock file, test.lock.db is not even deleted,
so I have to delete it manually when I want to restart Tomcat. This
makes me a little worried that there might be issues with data
corruption etc. if I continue this kind of startup/shutdown-procedure.
So what is the best practice for stopping the database in a servlet
environment (with Hibernate) when using embedded mode?

Also, interestingly, all the auto increment counters in tables seem to
increment by 30 every time I restart Tomcat, what might be the cause
of this?

Holger Hoffstätte

unread,
Aug 22, 2008, 10:10:06 AM8/22/08
to h2-da...@googlegroups.com
tolyen wrote:
> I'm putting together a simple web application that has H2 in embedded
> mode, Spring and Hibernate. I've got everything working now, but the
> problem is that the database won't shut down gracefully when Tomcat
> shuts down. I have defined the db properties in hibernate.cfg.xml and
> use that to create sessionFactory in Spring config. But when the
> servlet container is shut down, I have not yet found a way to shutdown
> the database properly.

Check out "Using a Servlet Listener to Start and Stop a Database" in the
Tutorial. That should do the trick.

Holger

tolyen

unread,
Aug 25, 2008, 8:28:15 AM8/25/08
to H2 Database
Thanks, I saw that in the documentation, but I thought that is only
for starting a non-embedded instance?

Holger Hoffstätte

unread,
Aug 25, 2008, 8:35:38 AM8/25/08
to h2-da...@googlegroups.com
tolyen wrote:
> Thanks, I saw that in the documentation, but I thought that is only
> for starting a non-embedded instance?

The same principle applies to your application. If you don't shut down
your web app (and the embedded db) properly by installing a
ServletContextListener, you will simply kill the db.

-h

Thomas Mueller

unread,
Aug 26, 2008, 7:43:44 PM8/26/08
to h2-da...@googlegroups.com
Hi,

I will update the documentation of the DbStarter:

"
When the web application is stopped, the database connection will be
closed automatically.
If the server is started within the DbStarter, it will also be stopped
automatically when the
web application is stopped.
"

Regards,
Thomas

tolyen

unread,
Aug 28, 2008, 6:52:12 AM8/28/08
to H2 Database
Thanks! It works now.

Do you have any insight on the auto increment issue? Every start/stop-
cycle increments the auto_increment counters by 30 or so. Not that
this is a big problem but is there some kind of fix for this?

On 27 elo, 02:43, "Thomas Mueller" <thomas.tom.muel...@gmail.com>
wrote:

Thomas Mueller

unread,
Aug 28, 2008, 9:00:01 PM8/28/08
to h2-da...@googlegroups.com
Hi,

> Do you have any insight on the auto increment issue? Every start/stop-
> cycle increments the auto_increment counters by 30 or so. Not that
> this is a big problem but is there some kind of fix for this?

Yes, the fix is to close the database. See also:
http://www.h2database.com/html/grammar.html#createsequence
"The cache is the number of pre-allocated numbers. If the system
crashes without closing the
database, at most this many numbers are lost. The default cache size is 32. "

Regards,
Thomas

tolyen

unread,
Aug 29, 2008, 5:27:00 AM8/29/08
to H2 Database
I figured out I was running some tests against the database in a way
that did not stop the server - the problem was not about starting and
shutting down Tomcat after all, the contextlistener fixed that. Thanks
for the help once again!

On 29 elo, 04:00, "Thomas Mueller" <thomas.tom.muel...@gmail.com>
wrote:
Reply all
Reply to author
Forward
0 new messages