Kill Derby DB Network Server in the background
I am looking for a good way to run Apache Derby server in network mode. I am using NetworkServerControl to start the server and it works great.
I start the server like this:
/**
* starts the network server
* @return true if sucessfull
*/
public boolean start()
{
try
{
// just to be sure that we don't start two servers
this.stop();
server = new NetworkServerControl();
server.start( null );
return true;
}
catch ( Exception ex )
{
this.logLogger.debug( ex );
return false;
}
}
And stop it like this:
/**
* stops the server
*
* @return true if there were no problems stopping the server
*/
public boolean stop()
{
try
{
if ( server == null )
{
server = new NetworkServerControl();
}
server.shutdown();
return true;
}
catch ( Exception ex )
{
this.logLogger.debug( ex );
return false;
}
}
On main () I have this so the process doesn't die while the server is running
(...)
clsDB.start();
while( clsDB.testForConnection() )
{
Thread.sleep( 60000 );
}
testForConnection () looks like this:
/**
* Try to test for a connection
*
* @return true if the server is alive
*/
public boolean testForConnection()
{
try
{
server.ping();
return true;
}
catch ( Exception ex )
{
this.logLogger.debug( ex );
return false;
}
}
My problem is that when the new instance of my JAR is called old, it will still work (unless I really have any luck and the test runs before starting the new server).
I know I can just check if the server is running and then I won't start it again, but I would like it to start working as a restart if the server already exists.
source to share
I was doing a derby project a few years ago (including running online) and I seem to remember that there is some kind of SQL on the server that disables derby.
So, assuming the case (no time for Google for it, sorry), you could look for your network instance at startup. If it exists, run this SQL. Once you have determined that it will finally shut down (the poll that it exists, I suppose), you can start it up again.
Edit: Oh it wasn't SQL, it connects to the DB with shutdown = true. See here :
DriverManager.getConnection(
"jdbc:derby:sample;shutdown=true");
source to share
The other solution I raised does not interact with your JAR, which you might not like.
The alternative is that your JAR that starts the derby instance opens a TCP port that it controls when it starts the derby. You can then send your own shutdown commands from your new JAR instance (obviously before opening your own TCP port).
Running an instance of your Derby JAR:
- See if the negotiated TCP port was open.
- If so, send the shutdown command.
- The JAR you sent to the command will then turn off derby using the method
stop()
. - After it finished derby down, it will send
success
any of them and close the connection.
- The JAR you sent to the command will then turn off derby using the method
- We are listening on a negotiated TCP port.
- Let's name
start()
and run Derby.
This is more than my other solution, but theoretically better, as as part of your restart, the JAR may want to do other things (clean / restart other resources, log the fact that it shuts down like this).
source to share