CLOVER🍀

That was when it all began.

WildFlyにXA DataSourceを登録する

WildFly 8.0.0.Finalがリリースされましたね!

WildFly
http://www.wildfly.org/

これで、Java EE 7が始められます!やってる暇が、どれだけあるのかという疑問はありますが…。

とりあえず、使い始める前にXA DataSourceくらい登録しておこうと思います。

WildFlyのダウンロード

以下より、WildFly本体をダウンロードします。

http://wildfly.org/downloads/

とりあえず、「Java EE7 Certified Server」のZIPを選択。

展開。

$ unzip wildfly-8.0.0.Final.zip

起動。

$ wildfly-8.0.0.Final/bin/standalone.sh

起動完了ログ。

00:29:19,136 INFO  [org.jboss.as] (Controller Boot Thread) JBAS015874: WildFly 8.0.0.Final "WildFly" started in 8678ms - Started 192 of 241 services (80 services are lazy, passive or on-demand)

JDBC Driverの登録

まず、JDBC Driverを登録します。JDBC Driverの登録は、管理CLIよりコマンドで登録しました。

今回登録するJDBC Driverは、MySQLのドライバで、あらかじめダウンロード済みとします。

管理CLIに接続。

$ wildfly-8.0.0.Final/bin/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

MySQLJDBC Driverを、WildFlyのモジュールとして登録します。

[standalone@localhost:9990 /] module add --name=com.mysql --resources=/path/to/mysql-connector-java-5.1.29/mysql-connector-java-5.1.29-bin.jar --dependencies=javax.api,javax.transaction.api

DataSourceサブシステムに移動。

[standalone@localhost:9990 /] cd subsystem=datasources

続いて、JDBC Driverとして登録します。

[standalone@localhost:9990 subsystem=datasources] ./jdbc-driver=mysql-driver:add(driver-name=mysql-driver,driver-module-name=com.mysql)
{"outcome" => "success"}

確認。

[standalone@localhost:9990 subsystem=datasources] jdbc-driver-info 
NAME         SOURCE                 
h2           com.h2database.h2/main 
mysql-driver com.mysql/main

あらかじめ入っていた、H2のドライバも見えますが。

JDBC Driver自体の詳しい情報は、こちらのコマンドで。

[standalone@localhost:9990 subsystem=datasources] jdbc-driver-info mysql-driver
driver-name                     mysql-driver          
deployment-name                 n/a                   
driver-module-name              com.mysql             
module-slot                     main                  
driver-datasource-class-name                          
driver-xa-datasource-class-name                       
driver-class-name               com.mysql.jdbc.Driver 
driver-major-version            5                     
driver-minor-version            1                     
jdbc-compliant                  false

XA DataSourceの登録

で、管理CLIからXA DataSourceの登録をしようとしたのですが、どうにもうまくいかず…。

[standalone@localhost:9990 subsystem=datasources] xa-data-source add --name=mysqlXaDs --jndi-name=java:jboss/datasources/mysqlXaDs --driver-name=mysql-driver                                                         
{"JBAS014653: Composite operation failed and was rolled back. Steps that failed:" => {"Operation step-1" => "JBAS010469: At least one xa-datasource-property is required for an xa-datasource"}}

JBoss AS 7.1.1の頃は、これで登録できてたのに。

いくつか情報を見たのですが、諦めてXMLに直接書くことにしました。

とりあえず、WildFlyをシャットダウン。

以下のファイルを編集。

$ vim wildfly-8.0.0.Final/standalone/configuration/standalone.xml

先ほど登録したJDBC Driverが、以下の様に記述してあるので

                    <driver name="mysql-driver" module="com.mysql"/>

XADataSourceを指定します。

                    <driver name="mysql-driver" module="com.mysql">
                        <xa-datasource-class>com.mysql.jdbc.jdbc2.optional.MysqlXADataSource</xa-datasource-class>
                    </driver>

続いて、登録済みのH2のDataSourceの下に、MySQLのXA DataSourceの定義をします。

                <datasource jndi-name="java:jboss/datasources/ExampleDS" pool-name="ExampleDS" enabled="true" use-java-context="true">
                    <connection-url>jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE</connection-url>
                    <driver>h2</driver>
                    <security>
                        <user-name>sa</user-name>
                        <password>sa</password>
                    </security>
                </datasource>
                <xa-datasource jndi-name="java:jboss/datasources/mysqlXaDs" pool-name="mysqlXaDs" enabled="true">
                    <xa-datasource-property name="ServerName">
                        localhost
                    </xa-datasource-property>
                    <xa-datasource-property name="PortNumber">
                        3306
                    </xa-datasource-property>
                    <xa-datasource-property name="DatabaseName">
                        test
                    </xa-datasource-property>
                    <driver>mysql-driver</driver>
                    <security>
                        <user-name>username</user-name>
                        <password>password</password>
                    </security>
                </xa-datasource>

JNDI名、接続先のサーバ、ポート、データベース名、ユーザ/パスワードなどを設定しています。

ここまで設定したら、編集を確定してWildFlyを起動。

$ wildfly-8.0.0.Final/bin/standalone.sh

再び、管理CLIに接続。

$ wildfly-8.0.0.Final/bin/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 /]

XA DataSourceの接続確認。

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

できましたー。

設定されている情報を見たい場合は、こちらで。

[standalone@localhost:9990 /] xa-data-source read-resource --name=mysqlXaDs --recursive=true

とりあえず、登録だけ完了…。使い始めるのは、また今度…。

追記
こちらの情報を参考に、もう少し頑張ってみました。

WildFly - CLIでデータソースを定義する
http://www.nailedtothex.org/articles/wildfly/datasource/

まずはJDBC Driverに、XA DataSourceクラス名を含めて登録

[standalone@localhost:9990 subsystem=datasources] ./jdbc-driver=mysql-driver:add(driver-name=mysql-driver,driver-module-name=com.mysql,driver-xa-datasource-class-name=com.mysql.jdbc.jdbc2.optional.MysqlXADataSource)

XA DataSourceの登録は、batchですればいい?

[standalone@localhost:9990 subsystem=datasources] batch
[standalone@localhost:9990 subsystem=datasources #] xa-data-source add --name=mysqlXaDs --jndi-name=java:jboss/datasources/mysqlXaDs --driver-name=mysql-driver --user-name=kazuhira --password=password
[standalone@localhost:9990 subsystem=datasources #] /subsystem=datasources/xa-data-source=mysqlXaDs/xa-datasource-properties=ServerName:add(value=localhost)
[standalone@localhost:9990 subsystem=datasources #] /subsystem=datasources/xa-data-source=mysqlXaDs/xa-datasource-properties=PortNumber:add(value=3306)                                                d
[standalone@localhost:9990 subsystem=datasources #] /subsystem=datasources/xa-data-source=mysqlXaDs/xa-datasource-properties=DatabaseName:add(value=test)
[standalone@localhost:9990 subsystem=datasources #] run-batch 
The batch executed successfully

batchで始めて、run-batchで反映するみたいです。

これで、CLIで登録できました♪