帝国软件
  设为首页 加入收藏 关于我们
 
解密帝国网站管理系统
栏 目:
 
您的位置:首页 > 技术文档 > JAVA编程 >
EJB?从EJB会话bean访问EJB实体bean
作者:未知 发布时间:2005-03-12 来源: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 与他联系。
  
评论】【加入收藏夹】【 】【打印】【关闭
※ 相关链接
无相关信息

   栏目导行
  PHP编程
  ASP编程
  ASP.NET编程
  JAVA编程
   站点最新
·致合作伙伴的欢迎信
·媒体报道
·帝国软件合作伙伴计划协议
·放眼未来 帝国近期将有重大举措!
·PHPWind6.3.2版通行证发布
·帝国备份王2008版正式发布
·帝国备份王2008版发布
·phpcms2007转帝国CMS5.0程序发布
·dedecms5.1转帝国CMS5.0程序发布
·帝国网站管理系统V5.0商业购买说明
   类别最新
·谈谈JDBC
·JDBC专题介绍
·JDBC接口技术
·利用weblogic的POOL(连接池)连接
·Jsp中调用Oracle存储过程的小例子
·JSP数据库操作例程
·JSP数据库连接大全
·用连接池提高Servlet访问数据库的效
·一种简单JDBC连接池的实现
·数据库连接池Java实现小结
 
关于帝国 | 广告服务 | 联系我们 | 程序开发 | 网站地图 | 留言板 帝国网站管理系统