zoukankan      html  css  js  c++  java
  • Best Practice: Avoiding or minimizing synchronization in servlets

    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
                 }
         }
    }
    

      

      

  • 相关阅读:
    AngularJS开发指南6:AngularJS表单详解
    AngularJS开发指南5:AngularJS表达式详解
    AngularJS开发指南4:指令的详解
    grunt入门讲解7:项目脚手架grunt-init
    grunt入门讲解6:grunt使用步骤和总结
    grunt入门讲解5:创建插件,安装Grunt以及常见问题
    grunt入门讲解4:如何创建task(任务)
    grunt入门讲解3:实例讲解使用 Gruntfile 配置任务
    grunt入门讲解2:如何使用 Gruntfile 配置任务
    grunt入门讲解1:grunt的基本概念和使用
  • 原文地址:https://www.cnblogs.com/malaikuangren/p/3256198.html
Copyright © 2011-2022 走看看