When calling an EJB3 method the transaction attribute is being ignored.

Solution Verified - Updated -

Issue

  • When calling an EJB method from within the same EJB, the transaction attribute is being ignored.

For example:

@Stateless  
public class TransactionBean implements Transaction {  
  ...

  @TransactionAttribute(TransactionAttributeType.REQUIRED)  
  public void firstMethod() {
    // ... some action to update the database
    this.secondMethod();
  }

  // a new transaction  here
  @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
  public void secondMethod() {
    // ... some action to update the database
    Session.setRollBackOnly();// this rollsback the work done in this method AND firstMethod - WHY? it is in a new transaction
  }
}
  • REQUIRES_NEW transaction causing rollback to calling method. Method 1 calls method 2, and method 2 interrogates the database within its own transaction Why does the second call to method 2 not result in a new transaction being created?
  • If I instantiate a StatelessSessionBean and call a method the transaction are not respected, what is wrong?
  • If an interal method is called the transaction attribute does not have any effect, why?
@Stateless
@TransactionAttribute(TransactionAttributeType.REQUIRED)
@TransactionManagement(TransactionManagementType.CONTAINER)
public MyClassBean implements MyClass {

  @Override
  public doPSomething() {
     doSomethingIntern();
     doSomethingIntern2();
  }

  @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
  private void doSomethingIntern() {
    ...
  }

  private void doSomethingIntern2() {
    Aworker worker = new Aworker();  // this is a class annotated with @Stateless and REQUIRES_NEW

    worker.doSomethingInANewTransaction()
  }
}

Environment

  • Red Hat JBoss Enterprise Application Platform (EAP)
  • Java Enterprise Edition (JEE)
    • EJB

Subscriber exclusive content

A Red Hat subscription provides unlimited access to our knowledgebase, tools, and much more.

Current Customers and Partners

Log in for full access

Log In

New to Red Hat?

Learn more about Red Hat subscriptions

Using a Red Hat product through a public cloud?

How to access this content