2008/05/27

cayenne+pgpoolではまりどころ

以前、pgpoolをcayenneで利用する為について記載した。
しかし、pgpoolやjndiで再接続機能を利用する為には更に工夫が必要だ。


以下のようにTransactionクラスを継承したクラスを作成する。


import java.sql.Connection;
import java.sql.SQLException;
import java.util.Iterator;

import org.apache.cayenne.CayenneException;
import org.apache.cayenne.access.Transaction;

public class NoTransaction extends Transaction {

@Override
public void begin() {
}

@Override
public void commit() throws IllegalStateException, SQLException, CayenneException {
close();
}

@Override
public void rollback() throws IllegalStateException, SQLException, CayenneException {
close();
}

protected void close() {

if (connections == null || connections.isEmpty()) {
return;
}

Iterator it = connections.values().iterator();
while (it.hasNext()) {
try {

((Connection) it.next()).close();
}
catch (Throwable th) {
}
}

}

}


そしてSQLを実行する部分をトランザクションしないよう以下のようにする。


Transaction tx = new NoTransaction();
try {

Transaction.bindThreadTransaction(tx);

Expression expression = ExpressionFactory.matchExp(~);
SelectQuery query = new SelectQuery(Ent.class, expression);
List entList = getDataContext().performQuery(query);
if (CollectionUtils.isEmpty(entList))
return null;

return entList;

}
finally {
try {
tx.commit();
} catch (Exception e) {

}
}