LoginSignup
4
8

More than 5 years have passed since last update.

クラスタ構成のDBへ接続ためのJDBC URL

Posted at

spring-bootでクラスタ構成のDBに接続するためのJDBC URLです。

Oracle RAC

String DB_URL= "jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS_LIST=" +
                     "(LOAD_BALANCE=OFF)(FAILOVER=ON)" +
                     "(ADDRESS=(PROTOCOL=TCP)(HOST=host1)(PORT=port1))" +
                     "(ADDRESS=(PROTOCOL=TCP)(HOST=host2)(PORT=port2)))" +
                     "(CONNECT_DATA=(SERVICE_NAME=service_name)))"

上記LOAD_BALANCE=ON、FAILOVER=ONの設定を組み合わせることによって「負荷分散」と「接続フェイルオーバー」の2つの機能が有効になる。

application.properties指定の場合、以下のように記載する。

spring.datasource.url=jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS_LIST=(LOAD_BALANCE=OFF)(FAILOVER=ON)(ADDRESS=(PROTOCOL=TCP)(HOST=host1)(PORT=1521))(ADDRESS=(PROTOCOL=TCP)(HOST=host2)(PORT=1521)))(CONNECT_DATA=(SERVICE_NAME=testDB)))
spring.datasource.username=user
spring.datasource.password=password

AWS Aurora DBクラスタ

MariaDB JDBC ドライバーの自動フェイルオーバー機能を使用し、フェイルオーバーの状況下でマスターとレプリカを高速に切り替えられる。

JDBC URLを指定する場合、Aurora DBのクラスタエンドポイントを直接指定するか、マスターとレプリカのエンドポイントを両方指定するかの2つ方法があります。

クラスタエンドポイント指定

String DB_URL = "jdbc:mariadb:aurora//auroradbcluster.auroradbtest.com::3306/testDB";

application.properties指定の場合、以下のように記載する。

spring.datasource.url=jdbc:mariadb:aurora//auroradbcluster.auroradbtest.com::3306/testDB
spring.datasource.username=user
spring.datasource.password=password

マスターとレプリカのエンドポイントを両方指定

String DB_URL = "jdbc:mariadb:aurora//rds.auroradbtest.com:3306,rds-ro.auroradbtest.com:3306/testDB";

application.properties指定の場合、以下のように記載する。

spring.datasource.url=jdbc:mariadb:aurora//rds.auroradbtest.com:3306,rds-ro.auroradbtest.com:3306/testDB
spring.datasource.username=user
spring.datasource.password=password
4
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
8