H2 in Servermode Url,File,Port,Parameters Question

4,100 views
Skip to first unread message

Christian123456789

unread,
Dec 14, 2011, 11:08:51 AM12/14/11
to H2 Database
Hi

I am very new to H2 but I realy like the <<create alias xy for
"a.java.class.Method">>. Since I am havily using Postgres and pljava
(and having a lot of troubles with them), I am thinking about
switching to H2.

But there is my first Question ... I was not able to figure out how I
can start a H2 server by defining the following parameters:
host,port,data-file,DEFAULT_CONNECTION=true.

I have tried several versions of:
java -jar h2-1.3.161.jar -url "jdbc:h2:tcp://localhost/~/
production;DEFAULT_CONNECTION=TRUE"
Exception in thread "main" org.h2.jdbc.JdbcSQLException: Verbindung
ist unterbrochen: "java.net.ConnectException: Connection refused:
connect: localhost"
Connection is broken: "java.net.ConnectException: Connection refused:
connect: localhost" [90067-161]
at
org.h2.message.DbException.getJdbcSQLException(DbException.java:329)
at org.h2.message.DbException.get(DbException.java:158)
at
org.h2.engine.SessionRemote.connectServer(SessionRemote.java:377)
at
org.h2.engine.SessionRemote.connectEmbeddedOrServer(SessionRemote.java:
267)
at org.h2.jdbc.JdbcConnection.<init>(JdbcConnection.java:110)
at org.h2.jdbc.JdbcConnection.<init>(JdbcConnection.java:94)
at org.h2.Driver.connect(Driver.java:72)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at org.h2.util.JdbcUtils.getConnection(JdbcUtils.java:168)
at org.h2.util.JdbcUtils.getConnection(JdbcUtils.java:118)
at org.h2.tools.Console.runTool(Console.java:222)
at org.h2.tools.Console.main(Console.java:100)
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(Unknown Source)
at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at org.h2.util.NetUtils.createSocket(NetUtils.java:110)
at org.h2.util.NetUtils.createSocket(NetUtils.java:91)
at org.h2.engine.SessionRemote.initTransfer(SessionRemote.java:
91)
at
org.h2.engine.SessionRemote.connectServer(SessionRemote.java:373)
... 10 more

I want to build a stand alone server (one big jar holding my pljava
classes and a java Main class which will start the tcp server, the
webserver and queing deamon, which is part of my application).

Can you help me to set up a H2 tcp server with enabled default
connection, listen on a port other than 9092 and using a database
other then test? This can be in java code or a command line command.

Thanks
Chris

LRichard

unread,
Dec 14, 2011, 2:09:47 PM12/14/11
to h2-da...@googlegroups.com
Have a look at http://www.h2database.com/html/tutorial.html#using_server.
The org.h2.tools.Server class is in charge of exposing the server.

Christian123456789

unread,
Dec 14, 2011, 2:51:32 PM12/14/11
to H2 Database

Thanks, of course I have read this - but this is not as self explanory
as you may think :-)

As you can see there is no switch for "Database" and no switch for
connection options like "DEFAULT_CONNECTION=TRUE" ... the only swich
accepting an url is "shutdown" ... so there must be a way even in java
to set properties. I can not beleve that all things are hard coded :-)

Options are case sensitive. Supported options are:
[-help] or [-?] Print the list of options
[-web] Start the web server with the H2 Console
[-webAllowOthers] Allow other computers to connect - see below
[-webDaemon] Use a daemon thread
[-webPort <port>] The port (default: 8082)
[-webSSL] Use encrypted (HTTPS) connections
[-browser] Start a browser connecting to the web server
[-tcp] Start the TCP server
[-tcpAllowOthers] Allow other computers to connect - see below
[-tcpDaemon] Use a daemon thread
[-tcpPort <port>] The port (default: 9092)
[-tcpSSL] Use encrypted (SSL) connections
[-tcpPassword <pwd>] The password for shutting down a TCP server
[-tcpShutdown "<url>"] Stop the TCP server; example: tcp://localhost
[-tcpShutdownForce] Do not wait until all connections are closed
[-pg] Start the PG server
[-pgAllowOthers] Allow other computers to connect - see below
[-pgDaemon] Use a daemon thread
[-pgPort <port>] The port (default: 5435)
[-properties "<dir>"] Server properties (default: ~, disable: null)
[-baseDir <dir>] The base directory for H2 databases (all
servers)
[-ifExists] Only existing databases may be opened (all
servers)
[-trace] Print additional trace information (all
servers)
The options -xAllowOthers are potentially risky.

Ok, help me out. I want a server running a database named "prod"
running on port 9999 which uses DEFAULT_CONNECTION. The File
containing the data should be in C:\H2\Databases\

For now I can start a server like this:
java -cp h2-1.3.162.jar org.h2.tools.Server -web -tcp -tcpPort 9999 -
baseDir C:/H2/Databases

But there are 2 things still missing. Use a Database other than "test"
and enable the default_connection.

Thanks
Chris

LRichard

unread,
Dec 14, 2011, 5:34:55 PM12/14/11
to h2-da...@googlegroups.com


Ok, help me out. I want a server running a database named "prod"
running on port 9999 which uses DEFAULT_CONNECTION. The File
containing the data should be in C:\H2\Databases\

For now I can start a server like this:
java -cp h2-1.3.162.jar org.h2.tools.Server -web -tcp -tcpPort 9999 -
baseDir C:/H2/Databases

But there are 2 things still missing. Use a Database other than "test"
and enable the default_connection.

OK. Your server is up.
Now, you can use an URL like jdbc:h2:tcp://<server_ip>:9999/prod to connect to your database.
Sorry for the DEFAULT_CONNECTION, I don't really get what this property may be useful for...

Christian123456789

unread,
Dec 14, 2011, 5:45:29 PM12/14/11
to H2 Database
Aha, I must have missed this: "Starting the server doesn't open a
database - databases are opened as soon as a client connects.", this
makes absolutly no sense to me - why should the client define the
database and the db properties? - but, ok its working that way and it
works fine. Since I have a lot of code in pljava DEFAULT_CONNECTION is
a compatibility trick. In oralce or postgres stored java funcions you
have do access your database with a jdbc connection string like
jdbc:default:connection. To get such a connection in H2 you have to
enable this feature.

Thanks
Chris

Peter Yuill

unread,
Dec 15, 2011, 3:43:52 PM12/15/11
to h2-da...@googlegroups.com
Hi Chris,

> Since I have a lot of code in pljava DEFAULT_CONNECTION is
> a compatibility trick. In oralce or postgres stored java funcions you
> have do access your database with a jdbc connection string like
> jdbc:default:connection. To get such a connection in H2 you have to
> enable this feature.

To make your Java functions work in H2 you will need to modify your code
a little. Let's say you have a function foo:

public static String foo(String bar) {
Connection conn =
DriverManager.getConnection("jdbc:default:connection");
/* do some stuff with connection */
return someResult;
}

For H2 foo needs to look like this:

public static String foo(Connection conn, String bar) {
/* do some stuff with connection */
return someResult;
}

If the first parameter of the function is a Connection, then H2 will
supply the equivalent of the "jdbc:default:connection" connection
automatically (no need to set properties).

Regards,
Peter

Thomas Mueller

unread,
Dec 18, 2011, 12:56:01 PM12/18/11
to h2-da...@googlegroups.com
Hi,

> I want to build a stand alone server (one big jar holding my pljava classes and a java Main class which will start the tcp server, the webserver and queing deamon, which is part of my application).

Do you actually need the server mode? Do you have a client application
that connects to the database using the JDBC driver, and this client
application doesn't run in the same process as the server? If not,
using the embedded mode would be much simpler and much faster.

> Starting the server doesn't open a database - databases are opened as soon as a client connects.", this
makes absolutly no sense to me

It's a bit similar to staring a web server doesn't read any *.html
files. The files are read if and when there is a client requesting
such a file. The H2 TCP server works in a similar way (even thought
there is a difference in how long the files stay open). A server can
concurrently open multiple (any number of) databases.

Regards,
Thomas

Reply all
Reply to author
Forward
0 new messages