2012-06-28 37 views
0

我已經編寫了一個OSS插件來在Maven構建過程中啓動/停止Derby。 該插件在普通的舊單個模塊中工作正常。但是,如果我有幾個模塊的聚合器,並且其中多個模塊有數據庫相關的測試,我似乎遇到了一些奇怪的問題。無法通過編程方式正確關閉Derby

我(在process-resourcestest階段分別)調用插件的startstop目標,如下圖所示:

 <plugin> 
      <groupId>org.carlspring.maven</groupId> 
      <artifactId>derby-maven-plugin</artifactId> 
      <version>1.4</version> 

      <configuration> 
       <failIfAlreadyRunning>false</failIfAlreadyRunning> 
      </configuration> 

      <executions> 
       <execution> 
        <id>start-derby</id> 
        <phase>process-resources</phase> 
        <goals> 
         <goal>start</goal> 
        </goals> 
       </execution> 
       <execution> 
        <id>stop-derby</id> 
        <phase>test</phase> 
        <goals> 
         <goal>stop</goal> 
        </goals> 
       </execution> 
      </executions> 
     </plugin> 

的問題在於一個事實,即在第二次嘗試啓動Derby(內存中)服務器,Derby似乎仍在運行,或者已經從第一個模塊加載數據庫內容。我可以這樣說,因爲聚合器中的第一個模塊創建並填充表中的一些數據。我的期望是,一旦我關閉Derby並從另一個模塊重新開始,它將成爲一個沒有任何現有內容的全新數據庫。

這裏是我的代碼插件裏面,隨着關停德比優惠:

try 
{ 
    try 
    { 
     server.ping(); 
    } 
    catch (Exception e) 
    { 
     if (failIfNotRunning) 
     { 
      throw new MojoExecutionException("Failed to stop the Derby server, no server running!", e); 
     } 

     getLog().error("Derby server was already stopped."); 
     return; 
    } 

    server.shutdown(); 

    while (true) 
    { 
     Thread.sleep(1000); 
     try 
     { 
      server.ping(); 
     } 
     catch (Exception e) 
     { 
      getLog().info("Derby has stopped!"); 
      return; 
     } 
    } 
} 
catch (Exception e) 
{ 
    throw new MojoExecutionException(e.getMessage(), e); 
} 

這個簡單的插件的完整的源代碼可以簽出或在GitHub上here觀察。

您的提示和意見將不勝感激! 在此先感謝!

回答

0

最後我做了以下內容:

try 
{ 
    try 
    { 
     server.ping(); 
    } 
    catch (Exception e) 
    { 
     if (failIfNotRunning) 
     { 
      throw new MojoExecutionException("Failed to stop the Derby server, no server running!", e); 
     } 

     getLog().error("Derby server was already stopped."); 
     return; 
    } 

    try 
    { 
     DriverManager.getConnection(getConnectionURL()); 
     DriverManager.getConnection("jdbc:derby:;shutdown=true"); 
    } 
    catch (SQLException e) 
    { 
     // Apparently this prints out a bunch of stuff we're not currently interested in, 
     // we just want it to shutdown properly. 
     // Perhaps further handling might be required at a future point in time. 
    } 

    server.shutdown(); 

    while (true) 
    { 
     Thread.sleep(1000); 
     try 
     { 
      server.ping(); 
     } 
     catch (Exception e) 
     { 
      getLog().info("Derby has stopped!"); 
      break; 
     } 
    } 

    System.getProperties().remove("derby.system.home"); 
} 
catch (Exception e) 
{ 
    throw new MojoExecutionException(e.getMessage(), e); 
} 

這不是我期待它必須做的方式。

+1

如果有人碰巧擔心,現在已經在derby-maven-plugin 1.5版中修復了。 – carlspring