Kohei Nozaki's blog 

CLIでデータソースを定義する


Posted on Tuesday Jan 21, 2014 at 06:37AM in Technology


環境

  • WildFly 8.0.0CR1
  • PostgreSQL 9.2.4
  • postgresql-9.3-1100.jdbc41.jar

何をするの?

CLIからPostgreSQLのデータソースを定義してみます

JDBCドライバをデプロイ

方法1

WildFlyを展開したディレクトリ内の/standalone/deploymentsディレクトリにJDBCドライバを放り込むだけです。

方法2

[3]の下の方にPostgreSQLの例が書いてある。こっちの方が正規の手順っぽい。8.0.0.Finalだけどやってみる

資源を置く

kyle-no-MacBook:wildfly-8.0.0.Final kyle$ mkdir -p modules/org/postgresql/main
kyle-no-MacBook:wildfly-8.0.0.Final kyle$ cd modules/org/postgresql/main
kyle-no-MacBook:main kyle$ cp ~/Downloads/postgresql-9.3-1100.jdbc41.jar .
kyle-no-MacBook:main kyle$ cat > module.xml << EOF
> <?xml version="1.0" encoding="UTF-8"?>
> <module xmlns="urn:jboss:module:1.0" name="org.postgresql">
>     <resources>
>         <resource-root path="postgresql-9.3-1100.jdbc41.jar"/>
>     </resources>
>     <dependencies>
>         <module name="javax.api"/>
>         <module name="javax.transaction.api"/>
>     </dependencies>
> </module>
> EOF

(貼付け用)

<?xml version="1.0" encoding="UTF-8"?>
<module xmlns="urn:jboss:module:1.0" name="org.postgresql">
    <resources>
        <resource-root path="postgresql-9.3-1100.jdbc41.jar"/>
    </resources>
    <dependencies>
        <module name="javax.api"/>
        <module name="javax.transaction.api"/>
    </dependencies>
</module>

jboss-cliを使って登録する

kyle-no-MacBook:main kyle$ ~/apps/wildfly-8.0.0.Final/bin/jboss-cli.sh --connect
Picked up _JAVA_OPTIONS: -Dfile.encoding=UTF-8
[standalone@localhost:9990 /] /subsystem=datasources/jdbc-driver=postgresql:add(driver-name=postgresql,driver-module-name=org.postgresql,driver-xa-datasource-class-name=org.postgresql.xa.PGXADataSource) 
{"outcome" => "success"}
[standalone@localhost:9990 /] ls subsystem=datasources
data-source                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
jdbc-driver                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
xa-data-source                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
installed-drivers=[{"driver-name" => "h2","deployment-name" => undefined,"driver-module-name" => "com.h2database.h2","module-slot" => "main","driver-datasource-class-name" => "","driver-xa-datasource-class-name" => "org.h2.jdbcx.JdbcDataSource","driver-class-name" => "org.h2.Driver","driver-major-version" => 1,"driver-minor-version" => 3,"jdbc-compliant" => true},{"driver-name" => "postgresql","deployment-name" => undefined,"driver-module-name" => "org.postgresql","module-slot" => "main","driver-datasource-class-name" => "","driver-xa-datasource-class-name" => "org.postgresql.xa.PGXADataSource","driver-class-name" => "org.postgresql.Driver","driver-major-version" => 9,"driver-minor-version" => 3,"jdbc-compliant" => false}]  
[standalone@localhost:9990 /] 

データソースの定義

jboss-cliの起動と接続

$ ./jboss-cli.sh
You are disconnected at the moment. Type 'connect' to connect to the server or 'help' for the list of supported commands.
[disconnected /] connect
[standalone@localhost:9990 /] 

使用可能なJDBCドライバ一覧を表示

デプロイしたものが出てきていることを確認します

[standalone@localhost:9990 /] ls subsystem=datasources
data-source                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
jdbc-driver                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
xa-data-source                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     
installed-drivers=[{"driver-name" => "h2","deployment-name" => undefined,"driver-module-name" => "com.h2database.h2","module-slot" => "main","driver-datasource-class-name" => "","driver-xa-datasource-class-name" => "org.h2.jdbcx.JdbcDataSource","driver-class-name" => "org.h2.Driver","driver-major-version" => 1,"driver-minor-version" => 3,"jdbc-compliant" => true},{"driver-name" => "postgresql-9.3-1100.jdbc41.jar","deployment-name" => "postgresql-9.3-1100.jdbc41.jar","driver-module-name" => undefined,"module-slot" => undefined,"driver-datasource-class-name" => undefined,"driver-xa-datasource-class-name" => undefined,"driver-class-name" => "org.postgresql.Driver","driver-major-version" => 9,"driver-minor-version" => 3,"jdbc-compliant" => false}]  
[standalone@localhost:9990 /]

データソースを追加

driver-nameはJDBCドライバのデプロイ方法によって変わってくる

  • 方法1の場合、JDBCドライバのjarファイルの名前(postgresql-9.3-1100.jdbc41.jar)
  • 方法2の場合、jboss-cliで指定した名前(postgresql)
    data-source add \
     --name=TestDS \
     --driver-name=postgresql-9.3-1100.jdbc41.jar \
     --connection-url=jdbc:postgresql://localhost:5432/testdb \
     --jndi-name=java:jboss/jdbc/TestDS \
     --user-name=postgres \
     --password=***
    

接続のテスト

[standalone@localhost:9990 /] /subsystem=datasources/data-source=TestDS:test-connection-in-pool
{
    "outcome" => "success",
    "result" => [true]
}
[standalone@localhost:9990 /] 

データソースの情報を見る

[standalone@localhost:9990 /] /subsystem=datasources/data-source=TestDS:read-resource
{
    "outcome" => "success",
    "result" => {
        "allocation-retry" => undefined,
        "allocation-retry-wait-millis" => undefined,
        "allow-multiple-users" => false,
        "background-validation" => undefined,
        "background-validation-millis" => undefined,
        "blocking-timeout-wait-millis" => undefined,
        "capacity-decrementer-class" => undefined,
        "capacity-decrementer-properties" => undefined,
        "capacity-incrementer-class" => undefined,
        "capacity-incrementer-properties" => undefined,
        "check-valid-connection-sql" => undefined,
        "connection-listener-class" => undefined,
        "connection-listener-property" => undefined,
        "connection-properties" => undefined,
        "connection-url" => "jdbc:postgresql://localhost:5432/testdb",
        "datasource-class" => undefined,
        "driver-class" => undefined,
        "driver-name" => "postgresql-9.3-1100.jdbc41.jar",
        "enabled" => true,
        "exception-sorter-class-name" => undefined,
        "exception-sorter-properties" => undefined,
        "flush-strategy" => undefined,
        "idle-timeout-minutes" => undefined,
        "initial-pool-size" => undefined,
        "jndi-name" => "java:jboss/jdbc/TestDS",
        "jta" => true,
        "max-pool-size" => undefined,
        "min-pool-size" => undefined,
        "new-connection-sql" => undefined,
        "password" => "***",
        "pool-prefill" => undefined,
        "pool-use-strict-min" => undefined,
        "prepared-statements-cache-size" => undefined,
        "query-timeout" => undefined,
        "reauth-plugin-class-name" => undefined,
        "reauth-plugin-properties" => undefined,
        "security-domain" => undefined,
        "set-tx-query-timeout" => false,
        "share-prepared-statements" => false,
        "spy" => false,
        "stale-connection-checker-class-name" => undefined,
        "stale-connection-checker-properties" => undefined,
        "track-statements" => "NOWARN",
        "transaction-isolation" => undefined,
        "url-delimiter" => undefined,
        "url-selector-strategy-class-name" => undefined,
        "use-ccm" => true,
        "use-fast-fail" => false,
        "use-java-context" => true,
        "use-try-lock" => undefined,
        "user-name" => "postgres",
        "valid-connection-checker-class-name" => undefined,
        "valid-connection-checker-properties" => undefined,
        "validate-on-match" => false,
        "statistics" => {
            "jdbc" => undefined,
            "pool" => undefined
        }
    }
}
[standalone@localhost:9990 /] 

データソースの設定を変更する

connection-urlを変更してみる

[standalone@localhost:9990 /] /subsystem=datasources/data-source=TestDS:write-attribute(name=connection-url, value=jdbc:postgresql:\/\/localhost:5432\/testdb2)
{
    "outcome" => "success",
    "response-headers" => {
        "operation-requires-reload" => true,
        "process-state" => "reload-required"
    }
}
[standalone@localhost:9990 /] 

データソースを削除する

[standalone@localhost:9990 /] /subsystem=datasources/data-source=TestDS:remove
{
    "outcome" => "success",
    "response-headers" => {"process-state" => "reload-required"}
}
[standalone@localhost:9990 /] 

データソース一覧を表示する

[standalone@localhost:9990 /] ls /subsystem=datasources/data-source
ArticlesDS    ExampleDS     PostgresqlDS  TestDS        
[standalone@localhost:9990 /] 

XAデータソースの定義

WildFly8.0.0.Finalでやってみた

普通のデータソースと異なりconnection-url表記は受け付けられないようだ。[5][6]を参考に

XAデータソースの追加

batch
xa-data-source add \
      --name=JBatchXADS \
      --driver-name=postgresql \
      --jndi-name=java:jboss/jdbc/JBatchXADS \
      --user-name=postgres \
      --password=***
/subsystem=datasources/xa-data-source="JBatchXADS"/xa-datasource-properties=ServerName:add(value="localhost")
/subsystem=datasources/xa-data-source="JBatchXADS"/xa-datasource-properties=PortNumber:add(value="5432")
/subsystem=datasources/xa-data-source="JBatchXADS"/xa-datasource-properties=DatabaseName:add(value="jbatch")
run-batch

接続テスト

[standalone@localhost:9990 /] /:reload
{
    "outcome" => "success",
    "result" => undefined
}
[standalone@localhost:9990 /] /subsystem=datasources/xa-data-source=JBatchXADS:test-connection-in-pool
{
    "outcome" => "success",
    "result" => [true]
}

read-resourceで情報を見てみると、何故かxa-data-source-propertiesで設定した値がundefinedとかになっているのだが、XMLファイルを見てみると値が入っているし、接続テストもOKなので気にしない事にする

接続の障害検知

check-valid-connection-sqlを設定

[8]を見ると「試験以降は valid-connection-checker-class-name と check-valid-connection-sql のどちらかは絶対指定するように」とあるので、設定してみる。例はxa-data-source

/subsystem=datasources/xa-data-source=TestDS:write-attribute(name=check-valid-connection-sql, value="SELECT 1")

background-validation-millisを設定

[9]を見ると接続チェックを定期的に行わせることもできるみたい。設定はこんな感じか

/subsystem=datasources/xa-data-source=TestDS:write-attribute(name=background-validation-millis, value=60000)

validate-on-matchを設定

DataSource#getConnection()実行時に接続チェックをするか否か。AS7/EAP6からはデフォルトfalseらしい

/subsystem=datasources/xa-data-source=TestDS:write-attribute(name=validate-on-match, value=true)

idle-timeout-minutesを設定

障害検知ではないけど。プールの中で使われてない接続を破棄する設定。デフォルト30分。60分に延ばしてみる

/subsystem=datasources/xa-data-source=TestDS:write-attribute(name=idle-timeout-minutes, value=60)

備考

“non-JDBC-compliant”

デプロイするとこういう文言が出力されるのがすごい気になる

18:06:10,463 INFO  [org.jboss.as.connector.subsystems.datasources] (management-handler-thread - 3) JBAS010404: Deploying non-JDBC-compliant driver class org.postgresql.Driver (version 9.3)
18:06:10,464 INFO  [org.jboss.as.connector.deployers.jdbc] (MSC service thread 1-2) JBAS010417: Started Driver service with driver-name = postgresql

参考文献

  1. CLI Recipes - JBoss AS 7.1 - Project Documentation Editor
  2. java - How to check datasource in JBoss? - Stack Overflow
  3. wildfly/quickstart · GitHub
  4. “non-JDBC-compliant”? - Community - JBoss
  5. WildFlyにXA DataSourceを登録する
  6. Creating XA Datasource from the CLI fails | Community
  7. JDBC XAデータソース - nekopの日記
  8. JBoss ASのデータソース設定 - nekopの日記
  9. JBoss ASのデータソース障害検知 - nekopの日記



No one has commented yet.

Leave a Comment

HTML Syntax: NOT allowed