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.
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; } }
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(); } }