Transaction Demarcation

One of the first AOP services ever implemented for JBoss AOP was transaction demarcation. Modelled directly after EJB, transaction demarcation allows you to delineate at the method level when a transactions should start or stop.. You can define transaction demarcation using the standard J2EE keywords on a plain java class: Required , RequiresNew, Supports, NotSupported, Never. There are two different ways to define it. One is through XML metadata, the other is through annotations. The transaction demarcation aspect is trigger by applying XML to a class or by annotating a method or constructor.

NOTE. Any exception that propagates through a transaction boundary will cause the transaction to become rolledback.

XML metadata

If want to totally isolate your Java class from knowing that it delineates transactional boundaries, then you can use XML metadtaa to define JBoss AOP transactions. You can do this in any JBoss AOP XML file.

<aop>
...
   <annotation tag="transaction" class="com.acme.POJO">
      <default>
         <trans-attribute>Required</trans-attribute>
      </default>
      <method name="get.*">
         <trans-attribute>RequiresNew</trans-attribute>
      </method>
      <method name="someMethod3">
         <trans-attribute>Supports</trans-attribute>
      </method>
      <method name="stuff">
         <trans-attribute>Never</trans-attribute>
      </field>
   </annotation>
</aop>

The default <trans-attribute> defines the default transaction attribute for the entire class. All get methods will use RequiresNew. The method someMethod3 will use Supports. The method stuff will Never start a transaction. So you see, its the same as J2EE EJB transactions except you're apply transaction demarcation to plain old Java Classes.

Annotations

If you prefer to define transactions directly in your classfiles you can annotate your methods or constructors instead. JBoss AOP has an Annotation Compiler for JDK 1.4 if you're stuck using that version of Java. The annotation is org.jboss.aspects.tx.Tx and the enumeration type used within this annotation is org.jboss.aspects.tx.TxType. For JDK 1.4, TxType is an enum abstraction provided by JBoss AOP. For JDK 5.0, TxType is an actual enum. The @Tx annotation can be declared on a method or constructor.

Tx Annotations with JDK 1.4

public class POJO
{
   /**
    * @@org.jboss.aspects.tx.Tx (org.jboss.aspects.tx.TxType.REQUIRED)
    */
    public POJO() {}

   /**
    * @@org.jboss.aspects.tx.Tx (org.jboss.aspects.tx.TxType.NEVER)
    */
    public static void staticMethod() {}
}

Tx Annotations with JDK 5.0

import org.jboss.aspects.tx.*;

public class POJO
{
    @Tx(TxType.REQUIRED)
    public POJO() {}

    @Tx(TxType.NEVER)
    public static void staticMethod() {}
}