EJB Life Cycle

From sheep
Jump to navigation Jump to search

EJB 3.0

Stateless 3
Stateful 3

Stateless

  • Can be created and destroyed with each call - unlikely
  • Only one client per running bean

Create

  1. Must have no arg constructor (Class.newInstance() calls this)
  2. Resources marked with annotations injected
  3. Method called annotated with @javax.annotation.PostConstruct or specified in the deployment descriptor
    1. 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

Stateful 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

  1. newInstance()
  2. setSessionContext(sc)
  3. ejbCreate()


Start Transaction

  1. afterBegin()

Commit Transaction

  1. beforecompletion()
  2. afterCompletion(true)

Rollback Transaction

  1. afterCompletion(false)

Passivate

  1. ejbPassivate()
  • Transaction state, session context etc are maintained though passivation

Activate

  1. ejbActivate()
  • transients are undefined when reactivated - unlike serialisation

Client finished - calls remove()

  1. ejbRemove()

Timeout

Container may call @PreDestroy but this isn't required by spec