CMT


go to main page

(0)What type of bean used for CMT?
(1)What is the default transaction demarcation?
(2)Can a method be associated with multiple transactions?
(3)What are transaction attributes?
(4)How to rollback transaction in CMT?
What type of bean used for CMT?
You can use container-managed transactions with any type of enterprise bean: session or message-driven
TOP
What is the default transaction demarcation?
By default, if no transaction demarcation is specified, enterprise beans use container-managed transaction demarcation.
TOP
Can a method be associated with multiple transactions?
Each method can be associated with a single transaction. Nested or multiple transactions are not allowed within a method.
TOP
What are transaction attributes?
A transaction attribute can have one of the following values: Transaction attributes are specified by decorating the enterprise bean class or method with a javax.ejb.TransactionAttribute annotation and setting it to one of the javax.ejb.TransactionAttributeType constants.
TOP
How to rollback transaction in CMT?
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.

public MyClass {
@Resource
private SessionContext sessionContext;

@Transactional(propagation = Propagation.REQUIRED,
isolation = Isolation.DEFAULT,
readOnly = false)
public int saveAll(){
//do stuff;
if(oops == true) {
sessionContext.setRollbackOnly();
return;
}
}

TOP