关键字:servlet,jsp
摘要
请不要实现 singlethreadmodel 接口。这种实践将导致 web 容器创建多个 servlet 实例;即为每个用户创建一个实例。对于任何大小的应用程序,这种实践都将导致严重的性能问题。
建议
singlethreadmodel 是一种标记接口,servlet 可以通过实现它来将自己的重入(re-entrancy)问题传送给 servlet 引擎。javax.servlet.singlethreadmodel 本身就是 j2ee 规范的一部分。websphere servlet 引擎通过为每个用户创建单独的 servlet 实例来处理 servlet 的重入问题。因为这种方法导致极大的系统开销,所以,应该避免实现 singlethreadmodel。
通常,开发人员在多线程环境下使用 javax.servlet.singlethreadmodel 来保护可更新的 servlet 实例变量。
最佳方法 ? 不使用 singlethreadmodel
public class bpallbadthingsservletsv1c extends httpservlet
{
private int numberofrows = 0;
private javax.sql.datasource ds = null;
public void doget(httpservletrequest request,
httpservletresponse response)
throws servletexception, ioexception
{
connection conn = null;
resultset rs = null;
preparedstatement pstmt = null;
int startingrows = numberofrows;
try
{
string employeeinformation = null;
conn = ds.getconnection ("db2admin","db2admin");
pstmt = conn.preparestatement
("select * from db2admin.employee");
rs = pstmt.executequery();
}
catch (exception es)
{
// error handling code here.
}
}
}
应被取代的方法
通常,开发人员在多线程环境下使用 javax.servlet.singlethreadmodel 来保护可更新的 servlet 实例。下列代码片断举例说明了什么是要避免的。
避免这种情况!!!? javax.servlet.singlethreadmodel
public class bpallbadthingsservletsv1c extends httpservlet
implements singlethreadmodel
{
private int numberofrows = 0;
private javax.sql.datasource ds = null;
public void doget(httpservletrequest request,
httpservletresponse response)
throws servletexception, ioexception
{
connection conn = null;
resultset rs = null;
preparedstatement pstmt = null;
int startingrows = numberofrows;
try
{
string employeeinformation = null;
conn = ds.getconnection ("db2admin","db2admin");
pstmt = conn.preparestatement
("select * from db2admin.employee");
rs = pstmt.executequery();
}
catch (exception es)
{
// error handling code here.
}
}
}
参考资料
websphere application server development best practices for performance and scalability
作者
harvey w. gunther 是 ibm 在北卡罗莱纳州 raleigh 的 websphere 产品开发小组中的高级性能分析师。可以通过 hgunther@us.ibm.com 与 harvey gunther 联系
闽公网安备 35060202000074号