Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Default Attributes in EJB Transactions

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

}
like image 937
arjacsoh Avatar asked Dec 31 '13 16:12

arjacsoh


People also ask

Which of the given are properties of an EJB transaction?

A transaction is a single unit of work items, which follows the ACID properties. ACID stands for Atomic, Consistent, Isolated, and Durable. Atomic − If any of the work item fails, the whole unit will be considered failed.

What are transaction attributes?

A transaction attribute controls the scope of a transaction. Figure 27–1 illustrates why controlling the scope is important. In the diagram, method-A begins a transaction and then invokes method-B of Bean-2 .

How do I rollback a container managed EJB transaction?

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.

What is Bean managed transaction in EJB?

In a bean-managed transaction, the code in the session or message-driven bean explicitly marks the boundaries of the transaction. An entity bean cannot have bean-managed transactions; it must use container-managed transactions instead.


2 Answers

  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,

like image 153
Brett Kail Avatar answered Oct 18 '22 12:10

Brett Kail


  1. Yes
  2. By default method has transaction settings REQUIRED. Therefore methodB() has REQUIRED
like image 30
michaldo Avatar answered Oct 18 '22 12:10

michaldo