0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Db2 Spring LibraryでDb2 on Cloudに接続する

Last updated at Posted at 2022-10-18

目的

SpringのアプリケーションでDb2のデータソースを利用するには、アプリケーションサーバのJNDI
リソースを利用するか、自力でDataSourceオブジェクトを組み立てる必要がありますが、Db2 Spring Libraryなるものの存在を知ったので、それを使ってDb2 on Cloudに接続できるか検証してみました。

なお、今回はSpring Bootではなく通常のSpring Frameworkを使用しました。

サンプル

POM

<dependency>
    <groupId>com.ibm.db2.jcc</groupId>
    <artifactId>db2-spring-framework</artifactId>
    <version>0.0.1</version>
    <scope>test</scope>
</dependency>

テストクラス

実際に動作したテストクラスのサンプルです。肝は2点あります。1点目は@Configuration@EnableDb2を付与することと、接続情報をシステムプロパティで設定することです。今回はテストクラス内で直接System.setProperty()しています。

Db2SpringTests.java
import static org.junit.jupiter.api.Assertions.*;
import java.sql.SQLException;
import javax.sql.DataSource;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import com.ibm.db2.spring.framework.EnableDb2;

@SpringJUnitConfig(Db2SpringTests.Config.class)
public class Db2SpringTests {

    @Configuration
    @EnableDb2
    static class Config {
    }

    @BeforeAll
    static void beforeAll() {
        System.setProperty("db2.serverName", "********.********.databases.appdomain.cloud");
        System.setProperty("db2.portNumber", "*****");
        System.setProperty("db2.user", "********");
        System.setProperty("db2.password", "********");
        System.setProperty("db2.databaseName", "bludb");
    }

    @Autowired
    private DataSource dataSource;

    @Test
    void testConnect() throws SQLException {
        assertNotNull(dataSource);
        try (var connection = dataSource.getConnection()) {
            assertNotNull(connection);
            assertNotNull(!connection.isClosed());
        }
    }

}

しかし、このコードでは通常のDb2では問題ないですが、Db2 on Cloudには接続できません。次のようなエラーになります。

com.ibm.db2.jcc.am.DisconnectNonTransientException: [jcc][t4][2034][11148][4.31.10] 会話の割り振り解除の原因となる分散プロトコル・エラーのため、実行が失敗しました。
DRDA データ・ストリーム構文エラーが検出されました。  理由: 0x3。 ERRORCODE=-4499, SQLSTATE=58009

原因ですが、Db2 on CloudはSSL接続が必須になっているためです。Db2ドライバでSSL接続を有効にする必要があります。

Db2プロパティファイル

Db2 Spring Libraryでは残念ながら直接SSL接続を設定するプロパティがありません。正確にはdb2.globalPropertyFileプロパティでDb2ドライバ本来のプロパティを直接設定できそうに見えるのですが、これも残念なことに正しく動作しません。

今回はDb2標準のプロパティファイルを作成し、SSL接続を有効化してみました。Db2のJDBCドライバはクラスパスのルートにDb2JccConfiguration.propertiesがあると自動的にロードしてくれます。

Db2JccConfiguration.properties
db2.jcc.sslConnection = true

検証結果

プロパティsslConnection = trueを設定することで、正常に動作するようになりました。

参考

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?