Divided
CMT container management transactions (recommended)
@ Transactionmanagement (transactionmanagementtype. Container) (Class-level annotation)
Container indicates that the CMT container manages transactions, and bean indicates BMT Management
@ Transactionattribute (transactionattributetype. required) (method-level annotation)
|
Non-transactional context Client |
Transaction context Client |
Required |
New transactions |
Classified as a superior transaction |
Required_new |
New transactions |
Pending a superior transaction, new transaction |
Supports |
Normal |
Classified as machine transaction |
Mandatory |
Exception |
Classified as a superior transaction |
Not_supported |
Normal |
Pending upper-level transactions |
Never |
Normal |
Exception |
The essence of supports is to support any transaction environment
Not_supported this attribute is only used in non-transactional automatic validation mode that supports MDB of the JMS provider
@ Resource // inject the EJB Context
Private sessioncontext context;
Context. setrollbackonly (); // an exception occurs during rollback.
BMT bean manages transactions, manages transactions by hard coding, and controls transaction boundaries.
(When usertransaction is used to manage a transaction, the upper-level transaction will be suspended)
@ Resource
Private usertransaction; // inject usertransaction
Usertransaction. Begin (); // start the transaction Boundary
Usertransaction. Commit (); // submit
Usertransaction. rollback (); // transaction boundary ends
Note: usertransaction can also be obtained through JNDI lookup.
And from sessioncontext context (EJB context), context. getusertransaction ()
Implementation exception class
@ Applicationexception (rollback = true)
// Rollback is false by default. If it is true, exception will be rolled back before being returned to the client.
Class XXX extends exception {}
All checked exceptions (except java. RMI. RemoteException) are expected to be application exception.
Java. RMI. RemoteException and non-checked exception are regarded as system exception
System exception is not directly transmitted to the client, but is saved as ejbexception by the container.