EJB Life Cycle
Jump to navigation
Jump to search
EJB 3.0
Stateless
- Can be created and destroyed with each call - unlikely
- Only one client per running bean
Create
- Must have no arg constructor (Class.newInstance() calls this)
- Resources marked with annotations injected
- Method called annotated with @javax.annotation.PostConstruct or specified in the deployment descriptor
- contract: void, no args, no checked exceptions
<post-construct> <lifecycle-callback-method>postCreate</lifecycle-callback-method> </post-construct> <pre-destroy> <lifecycle-callback-method>tidyUp</lifecycle-callback-method> </pre-destroy>
Destroy
- methods marked with @PreDestroy
- must be only one void, no args, no checked exceptions
Exceptions
- System exception => Bean is destroyed and @PreDestroy is not called
- System exception is any unchecked exception (which isn't annotated with @ApplicationException)
MDB
Essentially the same as stateless
EJB 2.0
Stateless
- same lifecycle as EJB 3
clients can call remove - this invalidates the bean for this client and returns it to the pool
Stateful
- same lifecycle as EJB 3 different interface
java serialisation equivalent technology used to passivate and store bean state.
- represents client state - cached but not (may be passivated - stored temporarily)
- passivate stores state to filesystem
Create EJB
- newInstance()
- setSessionContext(sc)
- ejbCreate()
Start Transaction
- afterBegin()
Commit Transaction
- beforecompletion()
- afterCompletion(true)
Rollback Transaction
- afterCompletion(false)
Passivate
- ejbPassivate()
- Transaction state, session context etc are maintained though passivation
Activate
- ejbActivate()
- transients are undefined when reactivated - unlike serialisation
Client finished - calls remove()
- ejbRemove()
Timeout
Container may call @PreDestroy but this isn't required by spec