title: Cannot correctly shutdown Derby programmatically
body: <p>I have written an OSS plugin to start/stop Derby during a Maven build process.
  The plugin works fine in plain old single modules. However, if I have an aggregator of several modules and more than one of them has database-related tests, I seem to be hitting some weird problem.</p>
  
  <p>I am invoking the plugin's <code>start</code> and <code>stop</code> goals (respectively during the <code>process-resources</code> and <code>test</code> phases), as shown below:</p>
  
  <pre><code>        &lt;plugin&gt;
              &lt;groupId&gt;org.carlspring.maven&lt;/groupId&gt;
              &lt;artifactId&gt;derby-maven-plugin&lt;/artifactId&gt;
              &lt;version&gt;1.4&lt;/version&gt;
  
              &lt;configuration&gt;
                  &lt;failIfAlreadyRunning&gt;false&lt;/failIfAlreadyRunning&gt;
              &lt;/configuration&gt;
  
              &lt;executions&gt;
                  &lt;execution&gt;
                      &lt;id&gt;start-derby&lt;/id&gt;
                      &lt;phase&gt;process-resources&lt;/phase&gt;
                      &lt;goals&gt;
                          &lt;goal&gt;start&lt;/goal&gt;
                      &lt;/goals&gt;
                  &lt;/execution&gt;
                  &lt;execution&gt;
                      &lt;id&gt;stop-derby&lt;/id&gt;
                      &lt;phase&gt;test&lt;/phase&gt;
                      &lt;goals&gt;
                          &lt;goal&gt;stop&lt;/goal&gt;
                      &lt;/goals&gt;
                  &lt;/execution&gt;
              &lt;/executions&gt;
          &lt;/plugin&gt;
  </code></pre>
  
  <p>The problem consists in the fact that upon the second attempt to start the Derby (in-memory) server, Derby appears to still be running, or to have loaded with the database contents from the first module. I can tell this because the first module in the aggregator creates and populates some data in a table. My expectation is that once I've shutdown Derby and started it all over again from the other module, that it would be a fresh database without any existing contents.</p>
  
  <p>Here is my code inside the plugin that deals with shutting down Derby:</p>
  
  <pre><code>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);
  }
  </code></pre>
  
  <p>The full source of this rather simple plugin can be checked out or viewed in GitHub <a href="https://github.com/carlspring/derby-maven-plugin" rel="nofollow noreferrer">here</a>.</p>