2011-09-15 24 views
0

上下文:使用MysqldResource java類(Connector/MXJ lib)部署的MySQL服務器。他們使用mysql服務器的應用程序可能因某種原因崩潰,並且可能在不關閉MySQL的情況下完成。確保MySQL關閉或使用MysqldResource將其關閉

的MySQL做如下的設置和運行:

mr = new MysqldResource(runtimeDir, dataDir); 
mr.start("My MySQL", options); 

目標:編寫代碼,以確保MySQL是沒有運行(MysqldResource.pid不存在,或PID不存在) 。如果MySQL我想殺了它。

注意:我不打算啓動mysqld!

最後,

如何archieve使用MysqldResource接口,它甚至有可能在目標?或者我應該用.pid文件來手動鍛鍊?

特別是要爲我下面的代碼工作正常:

mr = new MysqldResource(runtimeDir, dataDir); 
mr.shutdown(); 

後假設其運行在另一個會話程序崩潰和不首先啓動MySQL後?換句話說,這段代碼是否會破壞現有的MySQL實例運行?

回答

0

下面的代碼將工作:

mr = new MysqldResource(runtimeDir, dataDir); 
mr.shutdown(); 

它的工作原理,即使它在另一個虛擬機或與其他先生實例中運行。也可以用下面的方法來檢查,如果MySQL的部署正在運行:

mr.isRunning(); 

爲了證明關機也有效,即使叫上新建先生實例檢查停機碼 - 它使用PIDFILE()來檢查是否存在的工藝流程:

/** 
    * 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"); 
    }