Introduction

Minimize the use of synchronization in servlets. Because servlets are multi-threaded, synchronization of the major code path can seriously and adversely affect performance.

Recommendation

Servlets are multithreaded. Servlet-based applications have to recognize and handle this appropriately. If large sections of code are synchronized, an application effectively becomes single threaded, and throughput decreases dramatically.

No synchronization in servlets presents the best option, however, if the application design cannot avoid synchronization, then use a "Lock Object" and lock the smallest possible code path. Do not synchronize the servlet service method or the doGet and doPost methods. These methods are the major code paths. Synchronizing these methods or any of the servlet methods will lock the entire servlet instance. The following code shows an example using a "Lock Object" to protect the servlet instance variable numberOfRows.

Minimum synchronized code path

ublic class BpAllBadThingsServletsV1b extends HttpServlet
{
private int numberOfRows = 0;
private javax.sql.DataSource ds = null; private Object lockObject = new Object(); public void doGet(HttpServletRequest request, HttpServletResponse response(
throws ServletException, IOException
{
Connection conn = null;
ResultSet rs = null;
PreparedStatement pStmt = null;
int startingRows = 0; synchronize(lockObject)
{
startingRows = numberOfRows;
}
try
{
String employeeInformation = null;
conn = ds.getConnection("db2admin", "db2admin");
pStmt = conn.prepareStatemtn
("select * from db2admin.employee");
rs = pStmt.executeQuery();
}
catch (Exception es)
{
// Error handling code here
}
}
}

Alternative

The following code shows how the major code path is synchronized to protect a servlet instance variable called numberOfRows.

Using javax.servlet.SingleThreadModel is yet another way to protect servlet instance variables, but this should be avoided as well.

Figure 1 shows the performance impact of Synchronization

Locking the major code path: excessive synchronization

public class BpAllBadThingsServletsV1a 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; try
{
synchronized(this) // Locks out Most of the Servlet Processing
{
startingRows = numberOfRows;
String employeeInformation = null;
conn = ds.getConnection("db2admin", "db2admin");
pStmt = conn.prepareStatemtn
("select * from db2admin.employee");
rs = pStmt.executeQuery();
}
}
catch (Exception es)
{
// Error handling code here
}
}
}

  

  

最新文章

  1. 批量转换编码 (gbk -> utf8)
  2. c语言中gets ,getschar 和fgets 的用法及三者之间的差别,还有scanf
  3. struts的学习笔记
  4. vi 命令 用法(转)
  5. hiho 第116周,最大流最小割定理,求最小割集S,T
  6. 用完成例程(Completion Routine)实现的重叠I/O模型
  7. Node.js和mybatis分别实现mysql中like变量模糊查询
  8. Python3.4 多线程
  9. WinSock IO模型 -- WSAEventSelect模型事件触发条件说明
  10. Oracle左连接、右连接、全外连接
  11. LPC2478的硬件IIC使用
  12. Android各种Manager
  13. 如何判断NSDictionary是否包含某个键
  14. javascript数组去重的3种方法
  15. 利用postman 实现Get和Post测试
  16. ecplise 正则替换技巧
  17. js的快速搜索
  18. 20个Java练手项目,献给嗜学如狂的人
  19. java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0).
  20. 16.Set、List、Queue集合;Map.md

热门文章

  1. Django数据库设置
  2. hive数据库的一些应用
  3. 第四周 课堂Scrum站立会议
  4. hust--------The Minimum Length (最短循环节)(kmp)
  5. 198. 213. 337. House Robber -- 不取相邻值的最大值
  6. eclipse设置总结
  7. 陈朱兴-js写法【案例】:
  8. 用jquery或js实现三个div自动循环轮播
  9. oracle Redhat64 安装
  10. xampp搭建服务器环境、html5新的input类型