加入收藏 | 设为首页 | 会员中心 | 我要投稿 | RSS
您当前的位置:首页 > 阅读中心 > 软件学习

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

时间:2014-05-04 10:34:20  来源:JSP天空网  作者:未知
读者:开发人员
产品: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();
}
}
}


参考资料
WebSphere
Application Server Development Best Practices for Performance
and Scalability

作者
Harvey W. Gunther 是 IBM 在北卡罗来纳州罗利市(Raleigh)的 WebSphere 产品开发组的一名高级性能分析师。可以通过 hgunther@us.ibm.com 与他联系。
来顶一下
返回首页
返回首页
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表
推荐资讯
滨海近地生产厂房1500平出租
滨海近地生产厂房1500
科比专为大场面而生
科比专为大场面而生
“最美清洁工”原是《赤壁》宫女
“最美清洁工”原是《
尹馨大胆亮相《男人装》 嫩肤美腿勾人魂
尹馨大胆亮相《男人装
相关文章
    无相关信息
栏目更新
栏目热门