摘要
最小化 servlet 中同步的使用。因為 servlet 是多線程的,主要代碼路徑的同步會嚴重地且極為有害地影響效能。
建議
servlet 是多線程的。基於 servlet 的應用程式必須認識並適當地處理這一點。如果應用程式有很多大段的代碼是同步的,那麼這個應用程式實際上就變成單線程的,而且輸送量會顯著下降。
在 servlet 中不出現同步是最佳選擇,然而,如果應用程式設計無法避免同步,那麼請使用“鎖對象(lock Object)”並且鎖定可用性最小的代碼路徑。請不要同步 servlet 的 service 方法或 doGet 以及 doPost 方法。這些方法是主要代碼路徑。同步這些方法或任何這些 servlet 方法之一將鎖定整個 servlet 執行個體。下列代碼顯示了一個使用“鎖對象”來保護 servlet 執行個體變數 numberOfRows 的樣本。
最小同步代碼路徑
public 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.prepareStatement
("select * from db2admin.employee");
rs = pStmt.executeQuery();
}
catch (Exception es)
{
// Error handling code here
}
}
}