Top / Torque / 複数のDBで同時に利用する

アプリケーションを作成していると、一つのデータベースだけではなく、複数のデータベースに接続することがあります*1。 Torqueで複数のデータベースに接続する方法を調べていましたのですが、どうもうまくいきません。以下、いろいろと試行錯誤した作業メモです。

環境

複数のデータベースとして

DBTable
firstdbperson1
seconddbperson2

を作成して、Torque経由で二つのテーブルからデータを取得します。

その他の環境は

DBサーバRedhat Linux 8.0(2.4.20-28.8)
IP:192.168.10.3
DBPostgreSQL 7.2.4
クライアント(Torque実行環境)WindowsXP
JDK1.4.2_03-b02
Torque-gen3.1
Torque3.1
JDBCpostgresql-jdbc-7.2.4-5.80のRPMに付属していたもの
jdbc7.2dev-1.2.jar
Build ToolApache Ant version 1.6.0 compiled on December 18 2003

てな感じです。

準備

firstdb作成

[root@kino root]# su - postgres
-bash-2.05b$ createdb firstdb
CREATE DATABASE
-bash-2.05b$ psql firstdb
Welcome to psql, the PostgreSQL interactive terminal.

Type:  \copyright for distribution terms
       \h for help with SQL commands
       \? for help on internal slash commands
       \g or terminate with semicolon to execute query
       \q to quit

firstdb=# CREATE TABLE person1 (
firstdb(#   id INTEGER NOT NULL,
firstdb(#   name VARCHAR(255),
firstdb(#   PRIMARY KEY(id)
firstdb(# );

NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index 'person1_pkey' for table 'person1'
CREATE
firstdb=# INSERT INTO person1 (id, name) VALUES (1, 'John');
INSERT 18937 1

firstdb=# select * from person1
firstdb-# ;
 id | name
----+------
  1 | John
(1 row)

firstdb=#

seconddb作成

-bash-2.05b$ createdb seconddb
CREATE DATABASE
-bash-2.05b$ psql seconddb
Welcome to psql, the PostgreSQL interactive terminal.

Type:  \copyright for distribution terms
       \h for help with SQL commands
       \? for help on internal slash commands
       \g or terminate with semicolon to execute query
       \q to quit

seconddb=# CREATE TABLE person2 (
seconddb(#   id INTEGER NOT NULL,
seconddb(#   name VARCHAR(255),
seconddb(#   PRIMARY KEY(id)
seconddb(# );
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index 'person2_pkey' for table 'person2'
CREATE
seconddb=# INSERT INTO person2 (id, name) VALUES (1, 'Bob');
INSERT 18942 1
seconddb=# select * from person2;
 id | name
----+------
  1 | Bob
(1 row)

seconddb=#

Torque-gen

firstdb用のソース作成

build.propertiesを以下のように修正しました。

torque.project = firstdb
torque.targetPackage = kino.torque.firstdb

torque.database.createUrl = jdbc:postgresql://192.168.10.3:5432/firstdb
torque.database.buildUrl = jdbc:postgresql://192.168.10.3:5432/firstdb
torque.database.url = jdbc:postgresql://192.168.10.3:5432/firstdb
torque.database.driver = org.postgresql.Driver
torque.database.user = postgres
torque.database.password =
torque.database.host = 192.168.10.3

そして、antを実行しました。

ant -f build-torque.xml jdbc

生成されたschema.xmlをfirstdb-schema.xmlにリネームして、

ant -f build-torque.xml

を実行しました。

以上でfirstdbのソースコード生成は完了です。

seconddb用のソース作成

build.propertiesを以下のように修正しました。

#ref(): File not found: "build.properties.seconddb" at page "Torque/複数のDBで同時に利用する"

torque.project = seconddb
torque.targetPackage = kino.torque.seconddb

torque.database.createUrl = jdbc:postgresql://192.168.10.3:5432/seconddb
torque.database.buildUrl = jdbc:postgresql://192.168.10.3:5432/seconddb
torque.database.url = jdbc:postgresql://192.168.10.3:5432/seconddb
torque.database.driver = org.postgresql.Driver
torque.database.user = postgres
torque.database.password =
torque.database.host = 192.168.10.3

そして、antを実行しました。

ant -f build-torque.xml jdbc

生成されたschema.xmlをseconddb-schema.xmlにリネームして、

ant -f build-torque.xml

を実行しました。

以上でseconddbのソースコード生成は完了です。

最終的にTorque用のソースコードは以下になりました。

Torque

いよいよ先に生成したソースコードを用いてデータベースにアクセスします。

Torque.propertiesの修正

Torque.propertiesは以下のように修正しました。基本的にほとんどの行をコメントアウトして、以下の内容(firstdbとseconddbの情報)を追加しました。

torque.dsfactory.firstdb.factory=org.apache.torque.dsfactory.SharedPoolDataSourceFactory
torque.dsfactory.firstdb.connection.url = jdbc:postgresql://192.168.10.3:5432/firstdb
torque.dsfactory.firstdb.connection.user = postgres
torque.dsfactory.firstdb.connection.password = 

torque.dsfactory.seconddb.factory=org.apache.torque.dsfactory.SharedPoolDataSourceFactory
torque.dsfactory.seconddb.connection.driver = org.postgresql.Driver
torque.dsfactory.seconddb.connection.url = jdbc:postgresql://192.168.10.3:5432/seconddb
torque.dsfactory.seconddb.connection.user = postgres
torque.dsfactory.seconddb.connection.password = 

build.xmlの作成

Javaのクラスをコンパイルしてテストプログラムを実行するためのbuild.xmlを作成しました。また、テストプログラムとしてSampleMain?.javaを作成しました。

いよいよ実行

いよいよ実行です。

ant java

としましたが、エラーになってしまいました。

エラー内容ですが、みてみると

java.lang.NullPointerException: There was no DataSourceFactory configured for the connection default

となっていて、デフォルト値の設定がないよっておこってるみたいです。

試しに

試しに

torque.database.default=firstdb

を追記したところ、firstdbにはつながるけどもseconddbにはつながらない java.sql.SQLException: ERROR: Relation "person2" does not exist となってしまう

逆に

torque.database.default=seconddb

を追記したところ、seconddbにはつながるけどもfirstdbにはつながらない java.sql.SQLException: ERROR: Relation "person1" does not exist となってしまう

という状況でした。デフォルト値の指定をすると、データの取得はできているみたいです。

解決!

解決しました。
xxxx-schema.xmlのdatabase要素にはname属性を記述可能なのですが、どうもそれがデータソースを識別しているようです。*2

ant -f build-torque.xml jdbc

で生成されたschema.xmlには、なぜかname属性が省略されていて、そうすると

ant -f build-torque.xml

で生成されるソースコード(たとえばBasePerson1Peer.java)に

public static final String DATABASE_NAME = "default";

と"default"と書かれてしまうみたいです。ソースコードにdefaultと書かれてしまうために、Torque.properties内でtorque.database.defaultで指定したデータベースにしかつながらないのかなと判断しました。*3

とするとxxxx-schema.xmlのdatabase要素のname属性で指定した値で、Torque.propertiesを記述しなくてはいけないということでしょうか?何となく釈然としないのは私だけ?

追加手順

何はともあれ、解決策が見つかりましたので、追加の手順です。先ほどのTorque.propertiesには

torque.dsfactory.firstdb.factory
torque.dsfactory.seconddb.factory

などと書いたので、xxxx-schema.xmlの<database>をそれぞれ

<database name="firstdb">
<database name="seconddb">

に変更し、antでソースコードを再作成しました。*4

再度実行

antでもう一度サンプルプログラムを実行したところ、ようやくうまくいきました。両方のテーブルからデータ取得ができました!!


この記事は

選択肢 投票
おもしろかった 25  
そうでもない 0  
Top / Torque / 複数のDBで同時に利用する

現在のアクセス:17104


*1 管理用DBと参照DBなど
*2 ここでいっているデータソースってのは、巡り巡って、Torque.propertiesのtorque.dsfactory.firstdb.factory=org.apache.torque.dsfactory.SharedPoolDataSourceFactory?などの「firstdb」のこと
*3 torque.database.default=の右辺をみてtorque.dsfactory.右辺の値.factoryを参照するってながれ?
*4 確かにpublic static final String DATABASE_NAME = "firstdb";などとなっている

添付ファイル: file実行結果.txt 1697件 [詳細] fileerror.log 20件 [詳細] fileTorque.properties 1822件 [詳細] fileSampleMain.java 1563件 [詳細] filebuild.xml 1732件 [詳細] filesource.tar.gz 1296件 [詳細] fileseconddb-schema.xml 1761件 [詳細] filebuild.properties.second 726件 [詳細] filefirstdb-schema.xml 1729件 [詳細] filebuild.properties.firstdb 1676件 [詳細]

トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2019-01-30 (水) 15:38:35 (1906d)