Tuesday, October 27, 2009

How to start H2 database as a stand alone java process

In order to start H2 database server from a Java application, in a process other than the main process of your application (i.e. stand alone), you should use the java runtime API.

A code sample to do that:
-------------------------

Runtime rt = Runtime.getRuntime();
Process p = rt.exec("java -cp h2*.jar org.h2.tools.Server");


For the Spring users -
If you are using a Spring application with H2, you can configure spring to start your H2 server as described in the H2 documentation. Note that this will start the H2 server in the same process as you java application.
You can use java Runtime API in Spring as well. If you want the database to start before Spring/Hibernate are started you can use ServletContextListener to start the H2 server.


No comments:

Post a Comment