S2Txの機能を使って、POJO(普通のJavaのクラス)に対して、Aspectでトランザクションの自動管理機能を組み込むことができます。EJBコンテナが提供するようなトランザクション管理機能をPOJOに対して透明に組み込むことができるのです。組む込むことのできるトランザクション属性は次のとおりです。
コンポーネント名がAdviceの名前です
| 属性 | コンポーネント名 | 説明 |
|---|---|---|
| Required | tx/required | トランザクションが開始されていなければ、 自動的にトランザクションを開始します。 既にトランザクションが開始されていれば、 そのトランザクションを引き継ぎます。 |
| RequiresNew | tx/requiresNew | 常に新しいトランザクションを開始させます。 既存のトランザクションが開始されているなら、 既存のトランザクションを中断し、 自分自身のトランザクションの終了後、 中断したトランザクションを復帰させます。 |
| Mandatory | tx/mandatory | トランザクションが既に開始されてなければエラーにします。 |
package examples.tx;
public interface Hoge {
public void foo();
}
package examples.tx;
public class HogeImpl implements Hoge {
public void foo() {
System.out.println("foo");
}
}
<components>
<include path="j2ee-config.xml"/>
<component class="examples.tx.HogeImpl">
<aspect>tx/required</aspect>
</component>
</components>
package examples.tx;
import org.seasar.framework.container.S2Container;
import org.seasar.framework.container.factory.S2ContainerFactory;
public class HogeClient {
private static final String PATH =
"examples/tx/HogeClient-config.xml";
public static void main(String[] args) {
S2Container container = S2ContainerFactory.create(PATH);
Hoge hoge = (Hoge) container.getComponent(Hoge.class);
hoge.foo();
}
}
DEBUG 2004-03-14 18:05:18,402 [main] Transaction.begin() foo DEBUG 2004-03-14 18:05:18,432 [main] Transaction.commit()j2ee-config.xmlはS2としてあらかじめ用意(srcの直下)されています。Adviceのコンポーネント名をaspectタグのボディに指定するだけなので簡単です。 POJOに簡単にトランザクション管理機能が組み込めることがわかってもらえたと思います。