谢 谢 收 藏 http://www.qqread.com/j2ee/r211704.html
读者:开发人员
产品: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 与他联系。进入讨论组讨论。
读者:开发人员
产品: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 与他联系。进入讨论组讨论。
相关图文阅读
频道图文推荐
健 康 咨 询
时 尚 咨 询
相关专题
- 提高J2EE层与数据库层交互操作能力优势 (0次浏览)
- 怎样部署基于WebSphere的J2EE应用 (0次浏览)
- 微软冲击移动平台市场J2ME何去何从 (0次浏览)
- 开发工具王者比拼 .NET单挑J2EE (0次浏览)
- 教您怎样部署基于WebSphere的J2EE应用 (0次浏览)
- 华山论剑 J2EE单挑.NET之框架组件篇 (0次浏览)
- 对J2EE项目实际应用的一点体会 (0次浏览)
- J2EE综合:介绍编写XML文档的工作经验 (0次浏览)
- J2ME手机游戏:获取RecordStore相关信息 (0次浏览)
- J2EE综合——Struts常见错误的全面汇总 (0次浏览)



