Of course you could just use the server mode.
> I believe there is a new connection mode that will create and manage
> the server for you. But I'm not sure how to do that. So I second your
> question: How do we allow multiple connections to embedded database?
Yes there is a new (experimental) mode that automatically starts a
server when opening the database. To use it, append ;AUTO_SERVER=TRUE
to the database URL. You need H2 version 1.1.100 for this feature.
Example:
jdbc:h2:/data/dbs/test;AUTO_SERVER=TRUE
The first connection will open the database in embedded mode and will
start a server. Later connections will automatically use the server
mode. If the first connection is closed the later connections should
re-connect automatically.
You could try out if that works for you. As I said it's a new feature,
please tell me if there are some problems with that.
Regards,
Thomas
> AUTO_SERVER.
> As a heads up, it needs to be added to the JDBC URL documentation. =)
Sure, it will be documented in the next release. However it is still
quite new code, so there could always be some surprises.
Regards,
Thomas
> I'm using H2 database on Glassfish.
> I've configured a connection pool with the following properties...
> <property value="jdbc:h2:./db/slenticketdb;auto_server=true"
> When the domain starts, I can't connect to the database with an
> external process like SQuirrel SQL Client ?
If you get an exception, could you post it please? It should work when
using the exact same URL as above
jdbc:h2:./db/slenticketdb;auto_server=true
however it's sometimes better to use an absolute path, or a path
relative to the user home directory (jdbc:h2:~/...). Otherwise you
could end up with two databases if the current working directory is
different in the two applications.
> How to allow other process to connect to a server started database ?
It's allowed automatically when using ;auto_server=true (even remote
access is allowed - but there is additional security using a randomly
generated key that is stored in the .lock.db file in addition to the
user password protection).
> How to set the tcp port ?
You don't need to set it, the port is picked automatically and saved
in the .lock.db file.
Regards,
Thomas
> The stacktrace is :
> org.h2.jdbc.JdbcSQLException: Connection is broken [90067-100]
> When I connect Squirrel SQL, I use the absolute path like
> "jdbc:h2:tcp://localhost:9092/data/servers/glassfish/domains/myApp/
> config/db"
If you use the server mode, you need to start a server. I'm not sure
if you did that. See:
http://www.h2database.com/html/features.html#connection_modes
http://www.h2database.com/html/features.html#database_url
Maybe it's easier to just always use auto_server=true. Then you don't
have to think about starting the server, and what port number to use.
Regards,
Thomas
> The connection pool of the Glassfish domain use the mixed mode with
> this url : jdbc:h2:./db/slenticketdb;auto_server=true
>
> This means the server listen on a tcp port choosen by itself, right ?
Yes.
> When a client like SQuirrel SQL must be connected to the database
> server,
> it should use the remote tcp url like : jdbc:h2:tcp://
> 10.37.129.2:49578/slenticketdb
No. Just use the same URL as above: jdbc:h2:./db/slenticketdb;auto_server=true
Regards,
Thomas
>> The connection pool of the Glassfish domain use the mixed mode with
>> this url : jdbc:h2:./db/slenticketdb;auto_server=true
Actually there is a problem. You are using a relative path:
./db/...
That's a bit dangerous as it depends on the current working directory,
which is probably different depending on the application.
Better is an absolute path:
jdbc:h2:/data/db/slenticketdb;auto_server=true
I already wrote that before, but it's probably better to repeat it.
Regards,
Thomas