EJB—从EJB会话bean访问EJB实体bean

<script type="text/javascript"> google_ad_client = "pub-8800625213955058"; /* 336x280, 创建于 07-11-21 */ google_ad_slot = "0989131976"; google_ad_width = 336; google_ad_height = 280; // </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>

读者:开发人员

产品:WebSphere Application Server

版本:3.0.2.x、3.5.x 和 4.0

平台:所有

关键字:Servlet、JSP 和 EJB

摘要

使用 EJB 会话 bean 访问 EJB 实体 bean。通过用会话 bean 包装实体 bean,您能获得更佳性能。这加强了通过活动的进程对象包装被动的数据实体对象这一有效的对象模型概念。然而,可以编写 EJB 客户机来直接访问实体 bean,但是这样以性能为代价。通过用会话 bean 包装实体 bean 可以获得最佳性能。

建议

避免从客户机或 servlet 代码访问 EJB 实体 bean。这个最佳实践满足了两个性能方面的问题:

  • 减少远程方法调用的数目。当客户机应用程序直接访问实体 bean 时,每个读方法就是一个远程调用。包装会话 bean 能在本地访问实体 bean,将数据收集在一个结构中,接着返回一个值。


  • 为 EJB 实体 bean 提供外部事务上下文。在每个事务完成时,实体 bean 使其状态与其底层数据存储同步。当客户机应用程序直接访问实体 bean 时,每个读方法成为一个完整的事务。存储和载入跟随在每个方法后面。当会话 bean 包装实体 bean 来提供外部事务上下文时,实体 bean 在外部会话 bean 到达事务边界时使其状态同步。

一种更好的方法是从 EJB 会话 bean 访问 EJB 实体 bean。以下是 EJB 会话 bean 包装 EJB 实体 bean 的一个示例:

EJB 会话 bean 包装 EJB 实体 bean


 import java.rmi.RemoteException;

 import java.security.Identity;

 import java.util.Properties;

 import javax.ejb.*;

 import com.ibm.uxo.bestpractices.datamodels.*;



 public class EmployeeRosterBean implements SessionBean {

    private EmployeeHome employeeHome;

    private javax.ejb.SessionContext mySessionCtx = null;

    final static long serialVersionUID = 3206093459760846163L;



    public void ejbCreate() throws javax.ejb.CreateException,

	    java.rmi.RemoteException {

        employeeHome = EmployeeEjbHomeCacheHelper.getEmployeeHome()}



    public EmployeeStruct getEmployeeInfoFor(String empno) {

        Employee theEmployee = null;

        EmployeeStructure returnValue = new EmployeeStructure();



        try {

            theEmployee = employeeFindByPrimaryKey(new EmployeeKey(empno));



            returnValue.setSex(theEmployee.getSex());

            returnValue.setSalary(theEmployee.getSalary());

            returnValue.setPhoneno(theEmployee.getPhoneno());

            returnValue.setMidinit(theEmployee.getMidinit());

            returnValue.setLastname(theEmployee.getLastname());

            returnValue.setJob(theEmployee.getJob());

            returnValue.setHiredate(theEmployee.getHiredate());

            returnValue.setFirstnme(theEmployee.getFirstnme());

            returnValue.setEmpno(empno);

            returnValue.setEdlevel(theEmployee.getEdlevel());

            returnValue.setComm(theEmployee.getComm());

            returnValue.setBonus(theEmployee.getBonus());

            returnValue.setBirthdate(theEmployee.getBirthdate());

            returnValue.setWorkDept(theEmployee.getWorkdept());

        } catch (Exception e) {

            e.printStackTrace();

        }

        return returnValue;

    }



    public void ejbActivate() throws java.rmi.RemoteException {}

    public void ejbPassivate() throws java.rmi.RemoteException {}

    public voide ejbRemove() throws java.rmi.RemoteException {}

 } 	 

备选方案

下列代码段说明了使用 servlet 从客户机代码直接访问 EJB 实体 bean。图 1显示了直接访问相对于使用 EJB 会话 bean 来包装 EJB 实体 bean 的性能影响。

EJB 实体 bean 的直接客户机访问


public class BpEmploymentServletV0 extends HttpServlet {



public void service(HttpServletRequest request, HttpServletResponse response) {



    EmployeeHome employeeHome = null;

    Employe employee = null;



    try {

        ServletOutputStream out = response.getOutputStream();

        employee = employeeHome.findByPrimaryKey(new EmployeeKey(empno));

        out.println("<html><body>");

        out.println("<BR><B>" employee.getFirstnme() "</B>");

        out.println("<BR><B>" employee.getLastname() "</B>");

        out.println("<BR><B>" employee.getSex() "</B>");

        out.println("<BR><B>" employee.getBirthdate().toString()   </B>");

        out.println("<BR><B>" employee.getEdlevel() "</B>");

        out.println("<BR><B>" employee.getJob() "</B>");

        out.println("<BR><B>" employee.getHiredate() "</B>");

        out.println("<BR><B>" employee.getWorkdept() "</B>");

        out.println("<BR><B>" employee.getPhoneno() "</B>");

        out.println("<BR><B>" employee.getSalary.toString() "</B>");

        out.println("<BR><B>" employee.getComm().toString() "</B>");

        out.println("<BR><B>" employee.getBonus().toString() "</B>");

        out.println("</body></html>");



    } catch (Exception e) {



       e.printStackTrace();

    }

  }

} 


图 1. 将 EJB 实体 bean 包装在 EJB 会话 bean 内的性能影响

参考资料

作者

Harvey W. Gunther 是 IBM 在北卡罗来纳州罗利市(Raleigh)的 WebSphere 产品开发组的一名高级性能分析师。可以通过 hgunther@us.ibm.com 与他联系。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值