!!!Transaction Injections

If you are a framework developer, you may have the need to gain access easily to the current transaction, or to the TransactionManager.  Since J2EE does not define a standard way of accessing either of the interfaces at runtime, the JBoss AOP framework provides some very simple aspects that you can use that abstract out how these interfaces are acquired.


!TransactionManager Injection
If you want to get access to the JTA TransactionManager, you can have it injected into a variable via a annotation or you can call a setter method depending on your preferences. Using this with JDK 1.4 requires our AnnotationCompiler.

__JDK 1.4__
{{{
import javax.transaction.*;

public class POJO
{
   /**
    * @@org.jboss.aspects.Injected
    */
   TransactionManager injectedTm;

   TransactionManager tm;

   /**
    * @@org.jboss.aspects.Injected
    */
   public void setTransactionManager(TransactionManager tm)
   {
      this.tm = tm;
   }
}
}}}


__JDK 5.0__
{{{
import javax.transaction.*;
import org.jboss.aspects.Injected;

public class POJO
{
   @Injected TransactionManager injectedTm;

   TransactionManager tm;

   @Injected
   public void setTransactionManager(TransactionManager tm)
   {
      this.tm = tm;
   }
}
}}}


!Transaction Injection
If you want to get the current active transaction, then you can annotate a fieldand accessing that field will give you a reference to the current {{javax.transaction.Transaction}}.  Using this with JDK 1.4 requires our AnnotationCompiler.

__JDK 1.4__
{{{
import javax.transaction.Transaction;

public class POJO
{
   /**
    * @@org.jboss.aspects.Current 
    */
   static Transaction currentTx;

   public static void someMethod()
   {
      currentTx.setRollbackOnly();
   }
}
}}}

__JDK 5.0__
{{{
import javax.transaction.Transaction;
import org.jboss.aspects.Current;

public class POJO
{
   @Current static Transaction currentTx;

   public static void someMethod()
   {
      currentTx.setRollbackOnly();
   }
}
}}}
