Discussion:
EJB controls and page flows: lifetime of bean reference
(too old to reply)
bebop o'saurus
2005-02-08 07:53:10 UTC
Permalink
hello,

i'm new to ejbs; new to weblogic; using weblogic 8.1; playing around with
workshop. got a page flow that uses two ejb controls:

i) one ejb control for a stateful session bean

ii) one ejb control for an entity bean.

i read the e-docs section titled, "Using an EJB Control" and it tells me:

"...If the target EJB is a stateful session bean you must first invoke (one
of) its create method(s) to obtain a reference..."

"...After a reference is obtained, it is cached in the EJB control and is
used for any subsequent calls to the EJB within the method invocation in
which the initial call was made..."

"...For page flows and both stateful and stateless session EJBs, if the
session EJB is defined in the controller class, the lifetime of the
reference is the lifetime of the page flow..."

here are some snippets of my code:

// MyPageFlow.jpf

1. // get a reference to an entity bean from the control

2. EntityBean entity = entityBeanCtrl.create("username", "password");


3. // create new Stateful Session Bean Control

4. sessionBeanCtrl.create(entity, resources);

5. sessionBeanCtrl.setEntity(entity);

6. sessionBeanCtrl.setResources(resources);

7. sessionBeanCtrl.setUpAccount(signUpInfo);

.

.

.

the implementation of the ejbCreate() method for line 4 looks like this:

// MyStateFulSessionBean.ejb

8. // an ejbCreate() method of the MyStateFulSessionBean

9. public void ejbCreate ( EntityBean entity, ResourceBundle resources ){

10. // an instance method setting an instance var

11. setEntity(entity);

.

.

.

12. }



the implementation of the setUpAccount() method at line 7 looks like this:

13. // a business method of the MyStateFulSessionBean

14. public void setUpAccount(List accountInfo) {

15. // entity is the same instance variable set at line 9

16. if (entity == null ) {

17. throw new EJBException("entity cannot be null");

18. }

.

.

.

19. }

given that i have explicitly set the MyStateFulSessionBean's entity instance
variable from ejbCreate() with the setEntity(entity) call at line 11 from
the session bean, how come the entity instance variable at line 16 (which is
the same instance variable that was set at line 11) evaluates to null when
setUpAccount() is called from line 7 in the page flow?

likewise, even though i call setEntity(entity) and setResources(resources)
from lines 5 & 6 respectively, the instance variables that were set in those
two calls have been somehow reset back to null by the time
setUpAccount(signUpInfo) at line 7 is called from the page flow. what's
going on here?

contrary to what the e-docs say, the control is not caching the stateful
session bean's reference. i'm expecting the conversational state to still be
intact from method-call-to-method-call. but it seems like i'm getting a
brand new session bean reference with nulled-out instance variables with
each different method call from the page flow. does anyone know what i'm
overlooking? thanks in advance for your help.
bebop o'saurus
2005-02-09 04:20:16 UTC
Permalink
i've found an answer to my question. in a nutshell, i need to move the calls
from out of the page flow (at lines 2-7 in my original post) and into a
method implemented on a separate custom control which should wrap the
session ejb control. that's because, a page flow is a web-tier component
(not an ejb-tier component). and an ejb control _is_ an ejb-tier component;
and as such must be executed within the context of an ejb.

so, when an ejb control is called from the web tier (the page flow),
weblogic generates a stateless session bean to act as the ejb context that
the ejb control needs. this weblogic-generated stateless session bean (like
all stateless session beans) is pooled.

the ejb control's onAcquire() lifecycle method is called immediately before
each ejb control method is called; and it's onRelease() method is called
immediately after each ejb-control business method completes. these two ejb
control lifecycle methods result in a different ejb instance being acquired
from and released back to the pool FOR EACH EJB CONTROL BUSINESS METHOD! so
even though i was setting the ejb instance's state in one method, by the
time the next method is called, the page flow is effectively working with a
totally new ejb instance from the pool (whose state has not yet been
touched).

this dev2dev article explains in more detail:

http://dev2dev.bea.com/products/wlworkshop81/articles/jones.jsp
Post by bebop o'saurus
hello,
i'm new to ejbs; new to weblogic; using weblogic 8.1; playing around with
i) one ejb control for a stateful session bean
ii) one ejb control for an entity bean.
"...If the target EJB is a stateful session bean you must first invoke
(one of) its create method(s) to obtain a reference..."
"...After a reference is obtained, it is cached in the EJB control and is
used for any subsequent calls to the EJB within the method invocation in
which the initial call was made..."
"...For page flows and both stateful and stateless session EJBs, if the
session EJB is defined in the controller class, the lifetime of the
reference is the lifetime of the page flow..."
// MyPageFlow.jpf
1. // get a reference to an entity bean from the control
2. EntityBean entity = entityBeanCtrl.create("username", "password");
3. // create new Stateful Session Bean Control
4. sessionBeanCtrl.create(entity, resources);
5. sessionBeanCtrl.setEntity(entity);
6. sessionBeanCtrl.setResources(resources);
7. sessionBeanCtrl.setUpAccount(signUpInfo);
.
.
.
// MyStateFulSessionBean.ejb
8. // an ejbCreate() method of the MyStateFulSessionBean
9. public void ejbCreate ( EntityBean entity, ResourceBundle resources ){
10. // an instance method setting an instance var
11. setEntity(entity);
.
.
.
12. }
13. // a business method of the MyStateFulSessionBean
14. public void setUpAccount(List accountInfo) {
15. // entity is the same instance variable set at line 9
16. if (entity == null ) {
17. throw new EJBException("entity cannot be null");
18. }
.
.
.
19. }
given that i have explicitly set the MyStateFulSessionBean's entity
instance variable from ejbCreate() with the setEntity(entity) call at line
11 from the session bean, how come the entity instance variable at line 16
(which is the same instance variable that was set at line 11) evaluates to
null when setUpAccount() is called from line 7 in the page flow?
likewise, even though i call setEntity(entity) and setResources(resources)
from lines 5 & 6 respectively, the instance variables that were set in
those two calls have been somehow reset back to null by the time
setUpAccount(signUpInfo) at line 7 is called from the page flow. what's
going on here?
contrary to what the e-docs say, the control is not caching the stateful
session bean's reference. i'm expecting the conversational state to still
be intact from method-call-to-method-call. but it seems like i'm getting a
brand new session bean reference with nulled-out instance variables with
each different method call from the page flow. does anyone know what i'm
overlooking? thanks in advance for your help.
Loading...