作成 2004/3/4
データソースの設定とか、度々忘れちゃうのでメモしときます。あと、組み込みHSQL DBについて。なお、設定はJBoss自体のバージョンが変わると変わることしばしばなので、今書いてるバージョンでの設定です。
HSQL DB(http://hsqldb.sourceforge.net/)は、サイズが小さく簡単に利用できるRDBです。単一のJARをクラスパスに通すだけで利用でき、WARの中に組み込んで利用しちゃうというのもありです。実運用システム向きではありませんが、デモやちょっとしたテストなんかには最適です。JBossではHSQL DBがデフォルトのDBとして組み込まれています。サービス定義は、deployディレクトリのhsqldb-ds.xmlに定義されています。
HSQLにはGUIによるクライアントアプリケーションも用意されています。若干しょぼいですが、スキーマを確認したり、INSERTしたデータを確認するのに利用できます。
HSQLにはインメモリ、スタンドアロン、サーバーといった起動モードの種類があります。hsqldb-ds.xmlでは、デフォルトでスタンドアロンモードで起動します。スタンドアロンモードの場合、DBへのアクセスはプロセスで排他的になるため、JBossを起動していると、HSQLのGUIクライアントで接続できません(JBossを停止すると接続できますが。。。)。通常JBossを起動したまま、GUIクライアントも接続にいきたいと思うので、hsqldb-ds.xmlの設定を変更し、サーバーモード(TCP接続)で起動するようにします。hsqldb-ds.xmlにはその設定が既に記述されています(コメントアウトされている)ので、スタンドアロン設定をコメントアウトし、サーバーモードの設定のコメントをはずします。以下の黄色文字がコメントをはずすところで、赤字部分がコメントアウトするところです。何箇所かあるので、全部をのせておきます。
リスト hsql-db.xml(編集前)
<?xml version="1.0" encoding="UTF-8"?>
<!-- The Hypersonic embedded database JCA connection factory config
$Id: hsqldb-ds.xml,v 1.1.2.11 2003/09/28 12:31:36 starksm Exp $ -->
<datasources>
<local-tx-datasource>
<!-- The jndi name of the DataSource, it is prefixed with java:/ -->
<!-- Datasources are not available outside the virtual machine -->
<jndi-name>DefaultDS</jndi-name>
<!-- for tcp connection, allowing other processes to use the hsqldb
database. This requires the org.jboss.jdbc.HypersonicDatabase mbean.
<connection-url>jdbc:hsqldb:hsql://localhost:1701</connection-url>
-->
<!-- for totally in-memory db, not saved when jboss stops.
The org.jboss.jdbc.HypersonicDatabase mbean is unnecessary
<connection-url>jdbc:hsqldb:.</connection-url>
-->
<!-- for in-process db with file store, saved when jboss stops. The
org.jboss.jdbc.HypersonicDatabase is unnecessary
-->
<connection-url>jdbc:hsqldb:${jboss.server.data.dir}/hypersonic/localDB
</connection-url>
<!-- The driver class -->
<driver-class>org.hsqldb.jdbcDriver</driver-class>
<!-- The login and password -->
<user-name>sa</user-name>
<password></password>
<!--example of how to specify class that determines if exception means connection should be destroyed-->
<!--exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.DummyExceptionSorter</exception-sorter-class-name-->
<!-- this will be run before a managed connection is removed from the pool for use by a client-->
<!--<check-valid-connection-sql>select * from something</check-valid-connection-sql> -->
<!-- The minimum connections in a pool/sub-pool. Pools are lazily constructed on first use -->
<min-pool-size>5</min-pool-size>
<!-- The maximum connections in a pool/sub-pool -->
<max-pool-size>20</max-pool-size>
<!-- The time before an unused connection is destroyed -->
<!-- NOTE: This is the check period. It will be destroyed somewhere between 1x and 2x this timeout after last use -->
<!-- TEMPORARY FIX! - Disable idle connection removal, HSQLDB has a problem with not reaping threads on closed connections -->
<idle-timeout-minutes>0</idle-timeout-minutes>
<!-- sql to call when connection is created
<new-connection-sql>some arbitrary sql</new-connection-sql>
-->
<!-- sql to call on an existing pooled connection when it is obtained from pool
<check-valid-connection-sql>some arbitrary sql</check-valid-connection-sql>
-->
<!-- example of how to specify a class that determines a connection is valid before it is handed out from the pool
<valid-connection-checker-class-name>org.jboss.resource.adapter.jdbc.vendor.DummyValidConnectionChecker</valid-connection-checker-class-name>
-->
<!-- Whether to check all statements are closed when the connection is returned to the pool,
this is a debugging feature that should be turned off in production -->
<track-statements>true</track-statements>
<!-- Use the getConnection(user, pw) for logins
<application-managed-security/>
-->
<!-- Use the security domain defined in conf/login-config.xml -->
<security-domain>HsqlDbRealm</security-domain>
<!-- Use the security domain defined in conf/login-config.xml or the
getConnection(user, pw) for logins. The security domain takes precedence.
<security-domain-and-application>HsqlDbRealm</security-domain-and-application>
-->
<!-- Add this depends tag if you are using the tcp connection url
<depends>jboss:service=Hypersonic</depends>
-->
</local-tx-datasource>
<!-- This mbean should be used only when using tcp connections. Uncomment
when the tcp based connection-url is used.
<mbean code="org.jboss.jdbc.HypersonicDatabase"
name="jboss:service=Hypersonic">
<attribute name="Port">1701</attribute>
<attribute name="Silent">true</attribute>
<attribute name="Database">default</attribute>
<attribute name="Trace">false</attribute>
<attribute name="No_system_exit">true</attribute>
</mbean>
-->
</datasources>
でもってJBossを起動すると、サーバーモードでHSQL DBも起動します。次にHSQL DBのGUIクライアントを起動します。hsqldb.jarにクラスパスを通して、org.hsqldb.util.DatabaseManagerクラスを実行します。
java -cp C:\jboss-3.2.3\server\default\lib\hsqldb.jar org.hsqldb.util.DatabaseManager -url jdbc:hsqldb:hsql://localhost:1701
-urlオプションはJDBCのURL設定です。オプションを指定せずに起動し、起動後に接続するDBを指定することもできます。
起動すると左のツリーにスキーマが、右のテキストボックスにSQLを入力しEXECUTEするとSQLを発行できます。SELECT文の場合は結果がテーブルで表示されます。
画面 HSQL DBのGUI
なおサーバーモードで起動した場合は、他のJDBCを利用するDBクライアントツールも利用できます。
画面 他のツールから接続(EclipseのMNPZ SQL Environmentの例)
と、まあ、他のDBやクライアントツールを用意しなくてもJBossだけで、これぐらいはできるのが、微妙に便利で感心です。
データソースは、deployディレクトリにxxxx-ds.xmlという名前の設定ファイルを置くことで登録します。デフォルトのhsqldb-ds.xmlも一つのデータソース設定です。ここでは、新たにMYSQLのデータソースを追加してみます。この設定ファイルを一から自分で書くのは間違いやすいので、アリもののサンプルをちょこっと変更して登録するのがよいでしょう。サンプルは、JBossのdocs/examples/jcaディレクトリにあります。MySQLの設定例は mysql-ds.xmlです(mssql-ds.xmlと名前が似てるので注意)。URLやユーザー、パスワードを自分のMySQLのDBにあわせます。
リスト mysql-ds.xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- ===================================================================== -->
<!-- -->
<!-- JBoss Server Configuration -->
<!-- -->
<!-- ===================================================================== -->
<!-- $Id: mysql-ds.xml,v 1.1 2002/07/22 22:57:24 d_jencks Exp $ -->
<!-- ==================================================================== -->
<!-- Datasource config for MySQL using 2.0.11 driver -->
<!-- ==================================================================== -->
<datasources>
<local-tx-datasource>
<jndi-name>MySqlDS</jndi-name>
<connection-url>jdbc:mysql://localhost:3306/jbossdb</connection-url>
<driver-class>com.mysql.jdbc.Driver</driver-class>
<user-name>hoge</user-name>
<password>hoge</password>
</local-tx-datasource>
</datasources>
なお、指定する値に&などが含まれる場合はJBoss起動時にエラーになる。この場合、XMLの実態参照にエスケープするとよいようです。
<connection-url>jdbc:mysql://localhost:3306/jbossdb?useUnicode=true&characterEncoding=Shift_JIS</connection-url>↓
<connection-url>jdbc:mysql://localhost:3306/jbossdb?useUnicode=true&characterEncoding=Shift_JIS</connection-url>
編集した mysql-ds.xmlをdeployディレクトリにコピーします。また、JDBCドライバをlibディレクトリ(例 server/(どれか)/lib)以下にコピーします。これで、JBossを起動すれば、MySQLのデータソースも利用できます。
J2EEからアプリケーションからこのデータソースにアクセスするには、該当JNDI(やマッピングしたリソース参照)でアクセスすればよいです。上の例では、"java:/MySqlDS"になります。最初に"java:"が付くことに注意してください(JBossの決まりごと?)。
InitialContext ctx = new InitialContext();
DataSource ds = (DataSource)ctx.lookup("java:/MySqlDS");
CMPからアクセスするには、jbosscmp-jdbc.xmlにその設定を記述します。必要があれば、create-table(デプロイ時にDBテーブルを生成)などを指定してもよいでしょう。
リスト jbosscmp-jdbc.xml(抜粋)
<jbosscmp-jdbc>
<defaults>
<datasource>java:/MySqlDS</datasource>
<datasource-mapping>mySQL</datasource-mapping>
<create-table>true</create-table>
<remove-table>true</remove-table>
</defaults>
...
このjbosscmp-jdbc.xmlをXDocletから作るにはjboss要素でdatasource、datasourceMappingを指定します。
リスト XDocletの場合
<jboss Version="3.2" datasource="java:/MySqlDS" datasourceMapping="mySQL" createTable="true" removeTable="true" destDir="src/META-INF" >
XDocletのビルドファイルをJBoss-IDE(Eclipseプラグイン)を使って作る場合、上のようなjbosscmp-jdbc.xmlを作るには、jboss要素を追加した後、datasource、datasourceMappingなどに値を指定します。
なお、jbosscmp-jdbc.xmlのdefaults以下の要素の設定は、データソースを追加、でなくデフォルトと置き換える場合には不要です。