Ensuring MySQL off or kill it with MysqldResource
Context: MySQL server deployed with MysqldResource java class (Connector/MXJ lib). They app using mysql server may crash for some reason and could be finished without shutting down the MySQL.
The setup and run of MySQL done as following:
mr = new MysqldResource(runtimeDir, dataDir);
mr.start("My MySQL", options);
Goal: Write code to ensure that MySQL is not running (MysqldResource.pid doesn't exist, or pid doesn't exist). If MySQL I would like to kill it.
Note: I am not going to start mysqld!
Finally,
How do I archieve the Goal using MysqldResource interface, is it even possible? Or I should workout with .pid file for that manually?
In particular will the following code work correctly for me:
mr = new MysqldResource(runtimeDir, dataDir);
mr.shutdown();
assuming its running after in another session after开发者_如何学JAVA program crashed and without starting mysql first? In other words, will this code kill existing MySQL instance running?
The following code will work:
mr = new MysqldResource(runtimeDir, dataDir);
mr.shutdown();
It works even if it's running on another VM or with another mr instance. Also following method can be used to check if the MySQL deployed is running:
mr.isRunning();
To proof that shutdown also works even if called on newly created mr instance check the shutdown code - it uses pidFile() to check the existence of process:
/**
* Kills the MySQL process.
*/
public synchronized void shutdown() {
boolean haveShell = (getShell() != null);
if (!pidFile().exists() && !haveShell) {
printMessage("Mysqld not running. No file: " + pidFile());
return;
}
printMessage("stopping mysqld (process: " + pid() + ")");
issueNormalKill();
if (processRunning()) {
issueForceKill();
}
if (shellRunning()) {
destroyShell();
}
setShell(null);
if (processRunning()) {
printWarning("Process " + pid + "still running; not deleting "
+ pidFile());
} else {
threads.pause(150);
System.gc();
threads.pause(150);
pidFile().deleteOnExit();
pidFile().delete();
pidFile = null;
pid = null;
}
setReadyForConnection(false);
printMessage("clearing options");
options.clear();
out.flush();
printMessage("shutdown complete");
}
精彩评论