When the EJB container creates a new entity bean instance for the pool, how does the bean gain access to container services and environment information?
setEntityContext() is called by the container when a new entity bean instance is created. It provides the bean with its EntityContext, enabling access to container services like JNDI lookups and security information.
- Container instantiates a new entity bean class
- Container calls
setEntityContext(EntityContext ctx)on the instance - Bean stores the context in a member variable for later use
- Bean can now query environment info via JNDI (DataSource, etc.)
- Bean enters the pool—no specific data associated yet
- Called exactly once when the bean instance is created
- Bean can retrieve resources and environment data here
- Context should be stored for use in lifecycle callbacks
- Bean is now in the pool but has no specific data
- Built from: Entity Bean, EJB Container
- Builds into: Entity Context, unsetEntityContext()
- Related: Instance Pooling
- Do NOT perform database operations here
- Do NOT assume the bean is associated with any data