14

I ma reading the java ee docs and I would like to ask a couple of question to be sure I have understood well what is going on with EJB-Transactions.

1) The docs state that the defaalt TransactionManagement value is CONTAINER and the the default TransactionAttribute value is REQUIRED: If so, am I right that the following (Session) Bean executes all its methods in with CONTAINER managed Transactions and the attribute REQUIRED?

@Stateless
public class MyBean{

public void methodA(){
...
}

public void methodB(){
...
}

}

2) The docs state: Container-managed transactions do not require all methods to be associated with transactions. When developing a bean, you can set the transaction attributes to specify which of the bean’s methods are associated with transactions.

If I omit however the TransactionAttributeType, is it not automatically set to REQUIRED? Is the methodB in the following Bean not associated with a Transaction?

@Stateless
@TransactionManagement(CONTAINER)
public class MyBean{

@TransactionAttribute(MANDATORY)
public void methodA(){
...
}

public void methodB(){
...
}

}
arjacsoh
  • 8,932
  • 28
  • 106
  • 166

3 Answers3

10
  1. Yes, CONTAINER and REQUIRED are the default.

  2. The quote you gave seems to come from The Java EE 5 Tutorial. I agree that sentence is somewhat confusingly worded. Here's a possible rewriting that might help.

Container-managed transactions do not require all methods to use the default REQUIRED transaction semantics. When developing a bean, you can change the transaction semantics by setting the transaction attributes. For example, you can specify that a method should run without any transaction by using the NEVER transaction attribute,

Brett Kail
  • 33,593
  • 2
  • 85
  • 90
1
  1. Yes
  2. By default method has transaction settings REQUIRED. Therefore methodB() has REQUIRED
michaldo
  • 4,195
  • 1
  • 39
  • 65
0

1- Yes.

2- methodB() has REQUIRED attribute as it`s the default attribute, but you can override this default attribute with any other options like (NEVER, REQUIRED_NEW, SUPPORTED... etc).

However, the container still has the control to roll-back your transaction in case of system exception but you still have the ability to roll-back your transactions by invoking the setRollbackOnly method.

There are two ways to roll back a container-managed transaction. First, if a system exception is thrown, the container will automatically roll back the transaction. Second, by invoking the setRollbackOnly method of the EJBContext interface, the bean method instructs the container to roll back the transaction. If the bean throws an application exception, the rollback is not automatic but can be initiated by a call to setRollbackOnly.

https://docs.oracle.com/cd/E19798-01/821-1841/bnciv/index.html

  • So, if my class has @ stateless there's no need to add @ TransactionManagement because it's set by default. Is that right? – Viny Machado Mar 14 '19 at 20:35
  • 1
    Yes. Once you defined a class as an EJB using (@Statless or @Stateful) any transactions needed inside any one of its methods will be managed by container unless you change it. – Ibrahim Qandeel Mar 15 '19 at 21:16