The samples here are partial, and assume a good understanding of EJB and
Servlets/JSP. :)

1) Despite what some older ejb texts say you should never ever store a
reference to an EJBs remote interface in a session variable(See EJB2.0 Spec.
Sections 5 & 6). Instead you should store a handle to the EJBObject like so:

Handle h = myEjbRemote.getHandle();

... stash this in you session

then when you need a reference to the remote object you simply do this:

MyEjbRemote myEjbRemote =
(MyEjbRemote)javax.rmi.PortableRemoteObject.narrow(handle.getEJBObject,
MyEjbRemote.class);

2) The trick the removing a stateful session bean instance when a session
times out is to bind an object that implements HttpSessionBindingListener
that does the following:

public class FooBar implements HttpSessionBindingListener{
        Handle handle;

        public FooBar(Handle handle) {
                this.handle = handle;
        }
        public void valueUnbound(HttpSessionBindingEvent event) {
                MyEjbHome home = ctx.lookup("myEjb");
                home.remove(handle);
        }
}

3) I do not like using stateful session beans in this way. There is usually
a more elegant solution. But, I know this method works.

P.S. I know you can get away with storing the EjbRemote reference, but it is
a big no no in the new spec.

Later,
Russ

-----Original Message-----
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED]]On Behalf Of Dean Hutchins
Sent: Thursday, February 01, 2001 8:43 AM
To: Orion-Interest
Subject: Removing Stateful Session Beans


Hello,

I am using Orion 1.3.8.  I have created a stateful session bean reference in
a JSP and saved the reference in the session.  My problem is that the
session bean doesn't timeout and I can't determine a method for removing the
bean.  I have tried to create a class implementing the
HttpSessionBindingListener in the hopes that I could call remove() on the
reference at this point, but the unbound function isn't called when the
session is invalidated.  I noticed that a bug has been reported concerning
the fact that the timeout doesn't seem to be working starting in version
1.3.8.  I've included some sample code snippets below for reference.  Any
help on a method for removing these beans when the session invalidates would
be much appreciated.

Thanks,
Dean

Sample JSP code:

<html>
<head><title>Test Session Page</title></head>
<body>
Session = <%=session.getId() %>
<ejb:useHome id="home"
             type="test.ejb.XYZHome"
             location="java:comp/env/ejb/XYZ" />
<%
    test.ejb.XYZ myXYZ = (test.ejb.XYZ) session.getAttribute("SESSION_XYZ");
    if (myXYZ == null) {
       System.out.println("creating and saving session bean");
       myXYZ = home.create();
       session.setAttribute("SESSION_XYZ", myXYZ);
    } else {
       myXYZ = (test.ejb.XYZ) session.getAttribute("SESSION_XYZ");
    }
%>
</body>
</html>

Bean Segment (note I've also tried a separate class than only implemented
HttpSessionBindingListener and it doesn't get called):

public class XYZBean extends Object
implements SessionBean, HttpSessionBindingListener {
    private SessionContext ivContext;           // CLient specific context
    private Hashtable ivXYZs = null;                // Client Specific State


    public void valueBound(HttpSessionBindingEvent event) {
    }
    public void valueUnbound(HttpSessionBindingEvent event) {
      System.out.println("In the unbound function");
      try {
          ivContext.getEJBObject().remove();
      }
      catch (Exception e) {
          e.printStackTrace();
      }
    }
        etc...



Reply via email to