Newbie question about H2 database creation with Tomcat

779 views
Skip to first unread message

UseTheFork

unread,
Jul 23, 2010, 3:21:45 PM7/23/10
to H2 Database
Hi,

I am starting to explore the usage of H2 in web applications.

I have read http://www.h2database.com/html/tutorial.html#creating_new_databases
which says "By default, if the database specified in the URL does not
yet exist, a new (empty) database is created automatically. The user
that created the database automatically becomes the administrator of
this database."

If I create a web application and create an H2 database from the code,
as mentioned above, will this work within Tomcat? Can a web
application do that (i.e. enough priviledges) or should the database
be created on the Tomcat server and then accessed with a connection
from the application? What is the best practice for deployment?

Is there an administration tools I could load on Tomcat to administer
that database? If yes, which?

I am looking for pointers.

Thanks !!!


Erin Drummond

unread,
Jul 23, 2010, 6:32:07 PM7/23/10
to h2-da...@googlegroups.com
I would recommend running H2 in Server mode if you're using it in a
web application running in Tomcat. As with most servlet containers,
everytime you deploy your application, the old files are deleted and
replaced with the new files. This would mean that your embedded
database is replaced everytime you deploy your app.

Of course you can try and load your database in embedded mode from
outside your webapp directory, but personally I think that violates
the concept of a webapp as self-contained single .war file.

Also, server mode allows you to put the database on another server in
future if needed (to decrease load etc)

> --
> You received this message because you are subscribed to the Google Groups "H2 Database" group.
> To post to this group, send email to h2-da...@googlegroups.com.
> To unsubscribe from this group, send email to h2-database...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/h2-database?hl=en.
>
>

Thomas Mueller

unread,
Jul 26, 2010, 2:26:22 PM7/26/10
to h2-da...@googlegroups.com
Hi,

> If I create a web application and create an H2 database from the code,
> as mentioned above, will this work within Tomcat?

Yes, it should work. It works for me.

The problem is the database URL.

What you should *avoid* is use a path relative to the current working
directory such as: jdbc:h2:./test (which is the same as jdbc:h2:test)
then it means "store the database in the current working". This is a
very common error cause, because it depends on how and where you have
started the application (or Tomcat). This is also a problem when using
the server mode, for example: jdbc:h2:tcp://localhost/test.

It's much better to use an absolute path, for example:
jdbc:h2:/data/test (or jdbc:h2:file:/data/test or
jdbc:h2:tcp://localhost//data/test - please note the two "/" after
localhost). Or, if you want, use a path relative to the current user
working directory (jdbc:h2:~/data/test) but then the problem is that
you need to ensure it's always the same user.

> What is the best practice for deployment?

An absolute path is a good idea.

Regards,
Thomas

Reply all
Reply to author
Forward
0 new messages