アプリケーションを作成していると、一つのデータベースだけではなく、複数のデータベースに接続することがあります*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を以下のように修正しました。

filebuild.properties.firstdb
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

を実行しました。

filefirstdb-schema.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

を実行しました。

fileseconddb-schema.xml

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

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

filesource.tar.gz

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 = 
fileTorque.properties

build.xmlの作成

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

filebuild.xml
fileSampleMain.java

いよいよ実行

いよいよ実行です。

ant java

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

fileerror.log

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

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 となってしまう

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




現在のアクセス:17112


*1 管理用DBと参照DBなど

トップ   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS