Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

開発環境において、複数アプリケーションが同じH2DBを参照する方法をガイドへ追加する #1604

Open
kenjiyoshid-a opened this issue Nov 22, 2024 · 2 comments · May be fixed by #1960
Assignees
Labels
target: ガイド/AP開発手順 ドキュメントのガイド/アプリケーション開発手順に関係がある
Milestone

Comments

@kenjiyoshid-a
Copy link
Contributor

概要

開発環境において、複数アプリケーションがH2DBを共有して利用する方法がアプリケーション開発手順に明記されていない追加する。

詳細 / 機能詳細(オプション)

具体的な変更としては以下の通りである。

  • H2DBをサーバーモードで立ち上げるクラスを作成する。
  • 上記のクラスをConfigurationアノテーションのついているクラス内でAutowiredする。(他にもっと良い方法があるかもしれないので要検討)
  • プロパティファイルでDBのアクセス先の変更等を行う。

H2ServerLauncher.java(H2DBをサーバーモードで立ち上げるクラス)

@Component
@Profile("local")
public class H2ServerLauncher {

  private Server tcpServer;

  /**
   * h2サーバをサーバーモードで起動します。
   */
  @PostConstruct
  public void start() {
    try {
      this.tcpServer = Server.createTcpServer("-tcpPort", "9092", "-tcpAllowOthers", "-ifNotExists").start();
    } catch (SQLException e) {
      System.out.println("H2 server is already started.");
    }
  }

  /**
   * h2サーバを停止します。
   */
  @PreDestroy
  public void stop() {
    if (this.tcpServer != null) {
      this.tcpServer.stop();
    }
  }
}

Configurationアノテーションのついているクラス

@Configuration
public class DresscaWebConfig {

  @Autowired(required = false)
  public H2ServerLauncher h2ServerLauncher;
}

プロパティファイル

spring.datasource.hikari.jdbc-url=jdbc:h2:tcp://localhost:9092/mem:./data;
spring.sql.init.mode=always

完了条件

アプリケーション開発手順のJava編にて、開発環境において複数アプリケーションが同じH2DBを参照する方法が追加されている。

@kenjiyoshid-a kenjiyoshid-a added the target: ガイド/AP開発手順 ドキュメントのガイド/アプリケーション開発手順に関係がある label Nov 22, 2024
@tsuna-can-se tsuna-can-se added this to the v1.1.0 milestone Dec 20, 2024
@kenjiyoshid-a kenjiyoshid-a self-assigned this Feb 4, 2025
@kenjiyoshid-a
Copy link
Contributor Author

H2ServerLauncher の読み込みについて

@Component を付与し、コンストラクタ内にサーバーを起動する処理を含めることで、DIコンテナ内にインスタンス化される際に、サーバーモードで起動される。
そのため、H2ServerLauncherを@Configurationアノテーションのついているクラス内でAutowiredする必要はない。

@kenjiyoshid-a
Copy link
Contributor Author

修正後のH2ServerLauncherクラス

package com.dressca.web.admin.config;

import org.h2.tools.Server;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;
import com.dressca.systemcommon.constant.SystemPropertyConstants;
import jakarta.annotation.PreDestroy;
import java.sql.SQLException;

/**
 * 開発環境で H2 Database をサーバーモードで立ち上げるためのクラスです。
 */
@Component
@Profile("local")
public class H2ServerLauncher {

  private Server tcpServer;
  private final Logger apLog = LoggerFactory.getLogger(SystemPropertyConstants.APPLICATION_LOG_LOGGER);

  /**
   * {@link H2ServerLauncher} クラスのインスタンスを初期化し、 H2 Database をサーバーモードで起動します。
   */
  public H2ServerLauncher() {
    try {
      this.tcpServer = Server.createTcpServer("-tcpPort", "9092", "-tcpAllowOthers", "-ifNotExists").start();
    } catch (SQLException e) {
      apLog.info("H2 Database は既にサーバーモードで起動しています。");
    }
  }

  /**
   * インスタンスを破棄する際に H2 Database を停止します。
   */
  @PreDestroy
  public void stop() {
    if (this.tcpServer != null) {
      this.tcpServer.stop();
    }
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
target: ガイド/AP開発手順 ドキュメントのガイド/アプリケーション開発手順に関係がある
Projects
None yet
2 participants