How to use "baseDir" ??

1,590 views
Skip to first unread message

milly

unread,
Sep 29, 2008, 1:09:39 PM9/29/08
to H2 Database
Bringing up an old topic...

Was there ever any resolution for using the baseDir parameter with
embedded databases?

String baseDir = "C:/mydocuments/h2db";
String[] args = new String[]{"-baseDir", baseDir};
server = new Server();
int exitCode = server.run(args, System.out);1

This code seems to drop the baseDir argument on the floor as none of
the data files ever make it into the C:/mydocuments/h2db directory.

thanks,

milly

Hi,
Sorry, I think that -baseDir simply does not work for the H2 Console
(when using embedded databases, that is database URL of the form
jdbc:h2:test) , it only works for the server mode (database URL
jdbc:h2:tcp://localhost/test). I will think about a way to make the
-baseDir parameter work with embedded databases as well. As a
workaround (not a good one I know) you could use the Console to
connect to a database in server mode (you would need to start the
server as well of course, using the -baseDir parameter).
Thomas

On 4/26/07, 簡建勝 <ajax.ch...@gmail.com> wrote:

- Hide quoted text -
- Show quoted text -
> import java.io.File;
> import javax.servlet.*;
> import java.sql.*;
> import org.h2.tools.Server;

> public class DBStart implements ServletContextListener {
> private Server server;

> public void contextInitialized(ServletContextEvent servletContextEvent)
> {
> try {
> File file = new File("DB");
> if(!file.exists()){
> file.mkdir();
> }
> System.out.println("DB
> Starting..........................................................................................................");
> String[] DBArgs = new
> String[]{"-webPort","8082","-webAllowOthers","true","-baseDir",
> file.getAbsolutePath()};
> for(int i = 0;i<DBArgs.length;i++){
> System.out.println(DBArgs[i]);
> }
> server = Server.createWebServer(DBArgs);
> server.start();
> } catch (Exception e) {
> e.printStackTrace();
> }
> }

> public void contextDestroyed(ServletContextEvent servletContextEvent) {
> try {
> server.stop();
> System.out.println("DB Shutdown
> ..........................................................................................................");
> } catch (Exception e) {
> e.printStackTrace();
> }
> }
> }

> Every time,When I start the tomcat service,the database(if db not exists)
> always create in tomcat 'root' folder just like 'C:\Program Files\Apache
> Software Foundation\Tomcat 5.5' !!!!
> but my '-baseDir' is assign 'C:\Program Files\Apache Software
> Foundation\Tomcat 5.5\DB', How to use '-baseDir'??

milly

unread,
Sep 29, 2008, 1:35:25 PM9/29/08
to H2 Database
Thomas replied... and this does work. The problem was that I was
using a JDBC url of the form:

jdbc:h2:~/DB_NAME_HERE

instead of

jdbc:h2:tcp://localhost/DB_NAME_HERE

I don't understand why this works the way it does, but if you want to
connect to a DB with a base dir you need to use the full URL; One
question I have is... does this hurt performance for embedded H2
database servers? It seems unnecessary to make communication with the
DB travel up and down the TCP stack just to be able to use the baseDir
option.

Thanks for the help Thomas,

milly

Thomas Mueller

unread,
Sep 29, 2008, 2:43:52 PM9/29/08
to h2-da...@googlegroups.com
Hi,

> Thomas replied... and this does work. The problem was that I was
> using a JDBC url of the form:
>
> jdbc:h2:~/DB_NAME_HERE

The ~ stands for the home directory. You can also use it in the server
mode: jdbc:h2:tcp://localhost/~/DB_NAME_HERE

What you probably want is:

jdbc:h2:DB_NAME_HERE
or
jdbc:h2:file:DB_NAME_HERE

> does this hurt performance for embedded H2
> database servers?

Yes, see the performance comparison on the home page (www.h2database.com).

Regards,
Thomas

milly

unread,
Oct 1, 2008, 3:27:39 PM10/1/08
to H2 Database
Thanks Thomas... that really helps!! I obviously never really
understood what that "~" meant in the URL!!


On Sep 29, 1:43 pm, "Thomas Mueller" <thomas.tom.muel...@gmail.com>
wrote:
Reply all
Reply to author
Forward
0 new messages