This discussion is archived
5 Replies Latest reply: Jun 18, 2012 6:06 AM by 944271 RSS

Calling Stateful & Statless Methods from the same Entity Bean[EJB2.0]?

944271 Newbie
Currently Being Moderated
Dear All,
We using EJB2.0........
Regarding segregation of stateless and stateful methods..These methods are defined in Session bean.Now my question is

Is it a good practice to call Stateful and Stateless business methods from same single entity bean("EntityBean") defined at program level by extending MainBean..

The architecture is like
-------------------------------------------------------------------------------------------------------------------------------------------------
public class EntityBean extends MainBean
{
Public Methodxyz
{
stfobj.MakeConn();
stlobj.getXyz();
stfobj.CloseConn();
}

}
-------------------------------------------------------------------------------------------------------------------------------------------------
Public Class MainBean{lookup("stl");lookup(stf) ;stl_obj=home.create;stf_obj=home.create()............}
-------------------------------------------------------------------------------------------------------------------------------------------------
ejb-jar.xml
{ <session>
          <description>Entity Bean Employee Example</description>
          <ejb-name>stl</ejb-name>
          <local-home>com.packg.stlHome</local-home>
          <local>com.packg.stl</local>
          <ejb-class>com.packg.stlBean</ejb-class>
          <session-type>Stateless</session-type>
          <transaction-type>Container</transaction-type>
     </session>
     <session>
          <description>Entity Bean Employee Example for stateful</description>
          <ejb-name>stf</ejb-name>
          <local-home>com.packg.stfHome</local-home>
          <local>com.packg.ejbeans.stf</local>
          <ejb-class>com.packg.stfBean</ejb-class>
          <session-type>Stateful</session-type>
          <transaction-type>Container</transaction-type>
     </session>


     <session>
          <description>Session bean for System Parameters</description>
          <ejb-name>SysParam</ejb-name>
          <local-home>com.packg.Home</local-home>
          <local>com.packg.Param</local>
          <ejb-class>com.packg.Bean</ejb-class>
          <session-type>Stateless</session-type>
          <transaction-type>Bean</transaction-type>
          <ejb-local-ref>
          <ejb-ref-name>stl</ejb-ref-name>
          <ejb-ref-type>Session</ejb-ref-type>
          <local-home>com.packg.stlHome</local-home>
          <local>com.packg.ejbeans.stl</local>
          <ejb-link>stl</ejb-link>
     </ejb-local-ref>
          <ejb-local-ref>
          <ejb-ref-name>stf</ejb-ref-name>
          <ejb-ref-type>Session</ejb-ref-type>
          <local-home>com.packg.stfHome</local-home>
          <local>com.packg.stf</local>
          <ejb-link>stf</ejb-link>
     </ejb-local-ref>
     </session>
}
-------------------------------------------------------------------------------------------------------------------------------------------------

Thanks !!

Edited by: user9052593 on Jun 18, 2012 2:20 AM

Edited by: user9052593 on Jun 18, 2012 2:25 AM
  • 1. Re: Calling Stateful & Statless Methods from the same Entity Bean[EJB2.0]?
    r035198x Pro
    Currently Being Moderated
    There is nothing wrong with calling methods on different scoped beans if your current scope can access them.
    What I'm worried about is why your entity beans need to call session beans. Shouldn't your entity beans worry about data access only?
    Also, trying to use ejb 2.0 today is more pain than migrating to newer ee versions.
  • 2. Re: Calling Stateful & Statless Methods from the same Entity Bean[EJB2.0]?
    gimbal2 Guru
    Currently Being Moderated
    r035198x wrote:
    Also, trying to use ejb 2.0 today is more pain than migrating to newer ee versions.
    Agreed, although it might be somewhat of a hefty migration to switch from EntityBeans to JPA. Sometimes you just have to cope with legacy stuff and send out nice fat estimates...
  • 3. Re: Calling Stateful & Statless Methods from the same Entity Bean[EJB2.0]?
    944271 Newbie
    Currently Being Moderated
    All Business logic resides in session beans[Stateful or Stateless]..For stateful beans we are opening connections in entity bean ,calling business methods and closing connection in entity bean itself..Problem is

    I am doing lookup for stateful and stateless both since both objects are called by extending MainBean in same entity bean else will give null pointer exception..

    So whenever i call a Stateful or Stateless method from entity bean Which ultimately extends/calling MainBean and thereby calling home.create methods of stateful and stateless both..creating objects..Is this way fine or it will affect the performance?

    Do i need to segregate the Calls/invokation to Stateful / Stateless methods at entity bean level too..

    For ex:- Whenever i call Methodxyz() method ..at the same time it create 2 objects as its extending MainBean...i want to extend/create 1 object either of stateful or stateless one for stateful and stateless methods call respectively..

    See the code in entity bean is like
    ----------------------------------------------------------------------
    public class EntityBean extends MainBean

    {

    Public Methodxyz

    {

    stfobj.MakeConn(); call to stateful method defined in stateful bean

    stlobj.getXyz();// call to stateless method defined in stateless bean

    stfobj.CloseConn(); call to stateful method defined in stateful bean

    }

    }
    ------------------------------------------------------------
  • 4. Re: Calling Stateful & Statless Methods from the same Entity Bean[EJB2.0]?
    r035198x Pro
    Currently Being Moderated
    I'm not understanding the connections here.
    I would think that when the stateless bean needs to access the datastore it would invoke a method on the entity bean and the entity bean would start a connection, get the data, close the connection and return the results to the stateless EJB.
    Why does the stateless bean need to open and close the connection?
  • 5. Re: Calling Stateful & Statless Methods from the same Entity Bean[EJB2.0]?
    944271 Newbie
    Currently Being Moderated
    Because It is handled at bean level,If you check the transaction type ,It is set to Bean in xml file and we are doing Bean Managed Transactions and calling all business logic in beans by calling stf.method ot stl.method..
    in case of stateful we are opening the connection in bean and invoking business methods and then closing the connection in same block..

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points