views:

4159

answers:

3

At the moment we use HSQLDB as an embedded database, but we search for a database with less memory footprint as the data volume grows.

Derby / JavaDB is not an option at the moment because it stores properties globally in the system properties. So we thought of h2.

While we used HSQLDB we created a Server-object, set the parameters and started it. This is described here (and given as example in the class org.hsqldb.test.TestBase).

The question is: Can this be done analogous with the h2 database, too? Do you have any code samples for that? Scanning the h2-page, I did not find an example.

+7  A: 

From the download, I see that the file tutorial.html has this

import org.h2.tools.Server;
...
// start the TCP Server
Server server = Server.createTcpServer(args).start();
...
// stop the TCP Server
server.stop();
anjanb
+8  A: 

Yes, you can run H2 in embedded mode. You just use the JDBC driver and connect to an embedded url like this (their example):

This database can be used in embedded mode, or in server mode. To use it in embedded mode, you need to:

* Add h2.jar to the classpath
* Use the JDBC driver class: org.h2.Driver
* The database URL jdbc:h2:~/test opens the database 'test' in your user home directory

Example of connecting with JDBC to an embedded H2 database (adapted from http://www.h2database.com/javadoc/org/h2/jdbcx/JdbcDataSource.html ):

import org.h2.jdbcx.JdbcDataSource;
// ...
JdbcDataSource ds = new JdbcDataSource();
ds.setURL("jdbc:h2:˜/test");
ds.setUser("sa");
ds.setPassword("sa");
Connection conn = ds.getConnection();

If you're looking to use H2 in a purely in-memory / embedded mode, you can do that too. See this link for more:

You just need to use a special URL in normal JDBC code like "jdbc:h2:mem:db1".

Alex Miller
I need to set the "properties" right, to say: I have to - at least - set the directory the database resides in. This is the reason why we cannot use Derby/JavaDB because it uses system properties, as stated in the question.
Georgi
Running on port 8082, which defaults for h2 is no option, either.
Georgi
I don't understand what you're saying. Nothing here uses system properties. You specify a place to store files. It's up to your app to decide where to do that. You can use java.io.tmpdir for that if you want. It doesn't run on a port in embedded mode.
Alex Miller
I'm modding this back up - Alex's answer is dead on accurate. Is the OP unclear on how to establish a JDBC connection using a driver class and URL? Let's get some clarification, and maybe Alex would be willing to provide some actual code that uses these parameters.
Kevin Day
I added the JDBC connection code. Just vanilla JDBC usage though. @Georgi, are you looking for a pure *in-memory only* database? Maybe there is confusion here over what "embedded" means. To me that means "in process engine" not "in memory data".
Alex Miller
I needed a soulution that gives you the possibility to start the h2 server in an other application, and I needed to get a hand on all the parameters too (both "programmatically"). The solution of @anjanb is best: Create a org.h2.tools.Server-object and put params into - just what I exactly asked for
Georgi
A: 

If for some reason you need an embedded H2 database in server mode you can do it either manually using the API at http://www.h2database.com/javadoc/org/h2/tools/Server.html - or by appending ;AUTO_SERVER=TRUE to the database URL.

javydreamercsw