| ## Transaction Management S2Tx provides the automatic transaction management by using Aspect for POJOs. You can use the transaction management that the EJB container offers without adding the code to POJOs. Showing as follows is a transaction attribute offered with S2tx. ## Transaction Attributes The column of the component is a name of Advice.
attribute | component | description
--- | --- | ---
Required | j2ee.requiredTx | If the method's caller is already part of a transaction, it does not create a new transaction, but continues in the same transaction as its caller. If the caller is not in a transaction, a new transaction is created.
RequiresNew | j2ee.requiresNewTx | A new transaction is created always, regardless of the transactional state of the caller. If the caller was operating in a transaction, its transaction is suspended until the method completes.
Mandatory | j2ee.mandatoryTx | The method will not even start unless its caller is in a transaction. It will throw a Exception instead.
## Example #### Hoge.java
    package examples.tx;

    public interface Hoge {

        public void foo();
    }
#### HogeImpl.java
    package examples.tx;

    public class HogeImpl implements Hoge {

        public void foo() {
            System.out.println("foo");
        }
    }
#### HogeClient.dicon
    <components>
        <include path="j2ee.dicon"/>
        <component class="examples.tx.HogeImpl">
            <aspect>j2ee.requiredTx</aspect>
        </component>
    </components>
#### HogeClient.java
    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.dicon";

        public static void main(String[] args) {
            S2Container container = S2ContainerFactory.create(PATH);
            Hoge hoge = (Hoge) container.getComponent(Hoge.class);
            hoge.foo();
        }
    }
#### Execution result
    DEBUG 2004-03-14 18:05:18,402 [main] Transaction.begin()
    foo
    DEBUG 2004-03-14 18:05:18,432 [main] Transaction.commit()
J2ee.dicon file is in S2(under src directory). You only specify the component name of Advice for the body of the aspect tag. Thus, it is very easy to manage the transaction with POJO by using S2tx. ## Exception Handling The Advice defined in j2ee.dicon rolles back the transaction, when a component throws an exception. The Advice can be specified to commit or roll back the transaction depending on the exception class. Using `addCommitRule(Class)` method of Advice, you can specify the exception which invokes to commit the transaction when thrown. Using `addRollbackRule(Class)` method of Advice, you can specify the exception which invokes to roll back the transaction when thrown. When a component throws an exception, S2Tx checks if it matches the classes specified by `addCommitRule()/addRollbackRule()` method. When an exception matches the class or subclass of the class specified by `addCommitRule()/addRollbackRule()` method, the transaction is commited or rolled back as specified. When an exception does not match the class or subclass of the class specified by addCommitRule()/addRollbackRule() method, the transaction is rolled back. ### Example #### MyTx.dicon Following example shows myTx definition as a required Advice that rolls back the transaction when a RuntimeException (`java.lang.RuntimeException`) or subclass of it is thrown, and it commits when an Exception (`java.lang.Exception`) or subclass of it is thrown. Be aware that when the order of the setting of RuntimeException (`java.lang.RuntimeException`) and Exception (`java.lang.Exception`) is reversed, Advice commits the transaction when a RuntimeException (`java.lang.RuntimeException`) is thrown, because it is a subclass of Exception. (It looks like the try/catch block of Java)
    <components>
        <include path="j2ee.dicon"/>
        <component name="myTx"
            class="org.seasar.extension.tx.RequiredInterceptor">
            <initMethod name="addRollbackRule">
                <arg>@java.lang.RuntimeException@class</arg>
            </initMethod>
            <initMethod name="addCommitRule">
                <arg>@java.lang.Exception@class</arg>
            </initMethod>
        </component>
    </components>
### Advice compatible with EJB S2 provides ejbtx.dicon that has definition of Advice that rolls back the transaction when a RuntimeException (`java.lang.RuntimeException`), RemoteException (`java.rmi.RemoteException`), or subclass of them is thrown, and commits when another Exception is thrown. The column of the component is a name of Advice.
attribute | component
--- | ---
Required | `ejbtx.requiredTx`
RequiresNew | `ejbtx.requiresNewTx`
Mandatory | `ejbtx.mandatoryTx`
NotSupported | `ejbtx.notSupportedTx`
| Copyright© 2004-2005, The Seasar Foundation and the others. All rights reserved.