title: Synchronized Method In Spring MVC
body: <p>I am attempting to use synchronize method in spring controller. Because our Payment gateway hits method [@RequestMapping(value="/pay",method=RequestMethod.POST)] different transactions [txn id : txn01 &amp; txn02] at a time. But these 2 different transaction processing one by one than parallel due to using synchronize block.</p>
  
  <p>Problem -> Why i am using synchronize block in controller is that say Transaction [txn01] hits [@RequestMapping(value="/pay",method=RequestMethod.POST)] twice like duplicate call from payment gateway. before finishing first call [backend processing] i get second call from payment gateway for same tran id.</p>
  
  <p>Is there any way to process two different transaction parallel with using transaction id in synchronize block other than duplicate call i mean same tran id. Please advice me.</p>
  
  <p>Please let me know if my question is unclear.</p>
  
  <pre><code>@RequestMapping(value="/pay",method=RequestMethod.POST)
  public String payAck(HttpServletRequest httpRequest,HttpServletResponse httpResponse,HttpSession session){
      synchronized (this) {
          return this.processPayAck(httpRequest, httpResponse, session);
      }
  }
  
  public synchronized String processPayAck(HttpServletRequest httpRequest,HttpServletResponse httpResponse,HttpSession session){
     // Payment Acknowledgment process here
      if (sametranIDNotExists) {
          // first call here
          callWS(); - processing business logic.
          return someURL;
      } else {
         // Gets second call here before first call completed
         return someURL;
      }
  }
  </code></pre>
  
  <p>Modified code :</p>
  
  <p>Is it correct way to use intern inside synchronize block.</p>
  
  <pre><code>@RequestMapping(value="/pay",method=RequestMethod.POST)
  public String payAck(HttpServletRequest httpRequest,HttpServletResponse httpResponse,HttpSession session){
      String tranID = httpRequest.getParameter("tranID");
      synchronized (String.valueOf(tranID).intern()) {
          return processPayAck(httpRequest, httpResponse, session);
      }
  }
  </code></pre>