0

Mavenビルドプロセス中にDerbyを開始/停止するOSSプラグインを作成しました。プラグインは、単純な古い単一モジュールで正常に機能します。ただし、複数のモジュールのアグリゲーターがあり、それらの複数にデータベース関連のテストがある場合、奇妙な問題が発生しているようです。

以下に示すように、プラグインstartstop目標を(それぞれprocess-resourcestestフェーズで)呼び出しています。

        <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(メモリ内)サーバーを2回起動しようとしたときに、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でチェックアウトまたは表示できます。

4

1 に答える 1

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);
}

これは私がそれをしなければならないと思っていた方法ではありませんでした。

于 2012-06-29T12:52:57.687 に答える