I had similar requirement when running H2 as AUTO_SERVER mode. I wanted to
know which of my java applications was the current owner of the DB instance
( which was server vs client ).
I ended up modifying the H2 code to set a java system property so that a
simple property check would let me know which JVM contained the running
server. I am not sure that is exactly your requirement, however, the code
I added ( just a couple of lines ) should also serve your purpose.
I do not think it is generic enough for integration with the base code, but
building the H2 code is simple enough if you wanted to attempt it.
I only modify the main.org.h2.engine.Database.java file and add the one
line to each of the startServer() and stopServer() methods ( see example
below ).
In my case, I could possibly have more than one database server running at
a time so I append the port to the end of the system property name.
I simply check the system property of the server I am curious about; and if
the value is "TRUE" then it is running.
Good luck, I hope I have not misinterpreted your issue.
-Kent
The following represents my changes to the Database.java code ( look for
"Added the following..." )
private void startServer(String key) {
try {
server = Server.createTcpServer(
"-tcpPort", Integer.toString(autoServerPort),
"-tcpAllowOthers",
"-tcpDaemon",
"-key", key, databaseName);
server.start();
} catch (SQLException e) {
throw DbException.convert(e);
}
String localAddress = NetUtils.getLocalAddress();
String address = localAddress + ":" + server.getPort();
lock.setProperty("server", address);
String hostName = NetUtils.getHostName(localAddress);
lock.setProperty("hostName", hostName);
lock.save();
// Added the following...
System.getProperties().setProperty("H2_IS_EMBEDDED_TCP_SERVER"+
server.getPort(), "TRUE");
}
private void stopServer() {
if (server != null) {
Server s = server;
// avoid calling stop recursively
// because stopping the server will
// try to close the database as well
server = null;
s.stop();
// Added the following...
System.getProperties().setProperty("H2_IS_EMBEDDED_TCP_SERVER"+
s.getPort(), "FALSE");
}
}
On Friday, April 22, 2016 at 8:31:38 AM UTC-4, Jijo AC wrote:
>
> How we can check an H2 database TCP server started (running) from our java
> application. We tried,
> Server.createTcpServer().getService().isRunning(true); But it returns false
>
--
You received this message because you are subscribed to the Google Groups "H2
Database" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/h2-database.
For more options, visit https://groups.google.com/d/optout.