Why isn't it possible to AUTO_SERVER an in-memory database

1,022 views
Skip to first unread message

Xavier

unread,
Apr 28, 2011, 5:26:51 AM4/28/11
to H2 Database
Hi,

for my own understanding, could someone briefly explain why it is not
possible to automatically start a server for an in-memory database
(jdbc:h2:mem:test;DB_CLOSE_DELAY=-1&AUTO_SERVER=TRUE) ?

Is it for technical / security / performance / other reasons ?

I'm just curious because it does not seem to be a technical limitation
as it is still possible to create an in-memory database and then start
a TcpServer in the same process allowing remote connection to that
database.

Thanks,

Xavier

Maaartin G

unread,
Apr 28, 2011, 10:27:48 AM4/28/11
to h2-da...@googlegroups.com

1. What you propose would prevent the first process from termination, which is bad.

2. Actually, the server gets started as demon thread, so it doesn't prevent the app from terminating. When the app finishes, the server is gone. This way all the data is gone, too.

3. There's a ".lock.db" file, where would you put it to?

Xavier

unread,
Apr 29, 2011, 2:18:34 AM4/29/11
to H2 Database
I don't understand your answer, I would have expected a connection to
jdbc:h2:mem:test;DB_CLOSE_DELAY=-1&AUTO_SERVER=TRUE to behave like
this code (which does not prevent any process to terminate):

public static void main(String[] args) throws Exception {

Server.main("-tcp", "-tcpAllowOthers", "-tcpPort", "8043", "-
tcpDaemon");

Connection connection =
DriverManager.getConnection("jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1",
"sa", "");
connection.createStatement().execute("create table TEST_TABLE()");
connection.close();

System.out.println("I'm still accessible @ jdbc:h2:tcp://localhost:
8043/mem:testdb");
System.in.read(); // press enter to quit
System.out.println("I won't be accessible anymore as my JVM will
shut down after this message");
}

Server would be started on first connection and will stop when the JVM
shuts down.

I don't understand your 3rd point: what do lock files have to do with
in-memory databases?

Xavier

Maaartin G

unread,
Apr 29, 2011, 4:50:05 AM4/29/11
to h2-da...@googlegroups.com
On Friday, April 29, 2011 8:18:34 AM UTC+2, Xavier wrote:
I don't understand your answer, I would have expected a connection to
jdbc:h2:mem:test;DB_CLOSE_DELAY=-1&AUTO_SERVER=TRUE to behave like
this code (which does not prevent any process to terminate):
Server would be started on first connection and will stop when the JVM 
shuts down. 

OK, and then all the data is gone. I didn't think you wanted this.
 
I don't understand your 3rd point: what do lock files have to do with
in-memory databases? 

From the link I mentioned:

This server allows remote connections, however only to this database (to ensure that, the client reads .lock.db file and sends the the random key that is stored there to the server). When the first connection is closed, the server stops.

Xavier

unread,
Apr 29, 2011, 8:50:52 AM4/29/11
to H2 Database
ok, the lock file contains a random key to be read by clients to be
sure they only access that particular DB through the server... But, in
my case, I don't really care. To be more precise, I don't care if
*any* in-memory database can be accessed remotely.

I would just like to be able to open a connection to an in-memory
database and starts a server (on a non-random port) if none has been
started (and all that, specified in the connection URL). Then I would
like to access that in-memory from another remote process.

I'm fully aware that my data will be gone when shutting down the jvm
that started the DB and the server, but that's exactly what I want.
So, if it is explained like that in the documentation, could we hope
to see that feature implemented in H2 so I don't have to start the
server manually?

Thanks,

Xavier

Noel Grandin

unread,
Apr 29, 2011, 10:16:05 AM4/29/11
to h2-da...@googlegroups.com, Xavier

The docs aren't clear about this use-case, I think we'll need to wait for Thomas to weigh in.

See here:
http://www.h2database.com/html/features.html#in_memory_databases

This seems to indicate that the first process should start the in-memory database using a URL like this
jdbc:h2:mem:db1
And then the second process can connect using a URL like this
jdbc:h2:tcp://localhost/mem:db1

But the docs also talk about "starting a TCP server", which I'm not sure where that fits in.

-- Noel.

Thomas Mueller

unread,
Apr 29, 2011, 10:49:43 AM4/29/11
to h2-da...@googlegroups.com, Xavier
Hi,

Currently, the auto-server mode works as follows:
- the first application that opens the database writes its ip address
and port into the .lock.db file
- the second application will detect this and connect to this ip
address and port

The auto-server mode is useful if multiple processes use the same
database, even if the database file is on a networked file system.
That means the auto-server mode also supports that multiple computers
access the same database.


An in-memory database doesn't write any files, so the auto-server mode
doesn't currently work with in-memory databases.

As far as I understand your use case, you want multiple processes
*within the same computer* access the same in-memory database. Right?

The only way this is supported currently is starting a TCP server
manually (as documented), and then access the in-memory database
either remotely (using jdbc:h2:tcp://localhost/mem:test) or within the
same process (using either the remote URL, which is slow, or using
jdbc:h2:mem:test).

Regards,
Thomas

Noel Grandin

unread,
Apr 29, 2011, 11:13:51 AM4/29/11
to h2-da...@googlegroups.com, Thomas Mueller, Xavier

Thomas, I checked the documentation and I can't find a place where we explain that.
I can see how to start a TCP server that uses a disk-based database, but I can't see how to start a TCP server that uses
a memory database.

-- Noel.

Thomas Mueller

unread,
May 6, 2011, 9:37:10 AM5/6/11
to h2-database
Hi,

> I checked the documentation and I can't find a place where we explain that.

I'm not sure if you talk about the auto-server mode or the in-memory database.

Auto-server mode:
http://h2database.com/html/features.html#auto_mixed_mode (I will try
to extend the documentation to be more clear, it looks like it's not
easy to do that).

In-memory databases:
http://h2database.com/html/features.html#in_memory_databases "To
access an in-memory database from another process or from another
computer, you need to start a TCP server in the same process as the
in-memory database was created. The other processes then need to
access the database over TCP/IP or SSL/TLS, using a database URL such
as: jdbc:h2:tcp://localhost/mem:db1."

Regards,
Thomas

Reply all
Reply to author
Forward
0 new messages