2016-08-04 49 views
2

我遇到問題。 我們在我的項目中使用了Spring MVC框架,但Spring MVC的默認Controller是Singleton Model。 我改變控制器使用@Scope(「會話」),以避免競爭條件問題(每個人都有自己的控制器)。如何修復Fortify Race條件:Singleton Member現場問題

@Controller 
@Scope("session") 
public class AP0Controller extends BaseController { 

    @Autowired 
    GnRecService gnRecService; 

    Integer seq = null;//Global variable 

    @RequestMapping(value = "/agn/AP1W01A_004", method=RequestMethod.GET) 
    public ModelAndView welcomeGrid(@RequestParam("payType")String payType){ 
     seq = gnRecService.findTheLastPK(payType); 
     ModelAndView view = new ModelAndView(".agn.AP1W01A_004"); 
     return view; 
    } 

    public ModelAndView showPk() { 
     seq +=2; 
     ModelAndView view = new ModelAndView(".agn.AP1W01A_004"); 
     view.addObject("seq",seq) 
     return view; 
    } 

} 

惠普Fortify掃描後,報告指出這將導致競爭條件。 我該如何解決並解決問題?

seq +=2;//Race Condition: Singleton Member Field 

回答

0

試着重新設計你的控制器,使其不放置狀態。 另外,您可以考慮使用的AtomicInteger

AtomicInteger seq = new AtomicInteger();//Global variable 

@RequestMapping(value = "/agn/AP1W01A_004", method=RequestMethod.GET) 
    public ModelAndView welcomeGrid(@RequestParam("payType")String payType){ 
     seq.set(gnRecService.findTheLastPK(payType)); 
     ModelAndView view = new ModelAndView(".agn.AP1W01A_004"); 
     return view; 
    } 

    public ModelAndView showPk() { 
     final int localSeq = seq.addAndGet(2); 
     ModelAndView view = new ModelAndView(".agn.AP1W01A_004"); 
     view.addObject("seq",localSeq) 
     return view; 
    } 
+0

如果變量序列是java.lang.String中的類型或POJO,如何解決?謝謝 –

+0

我只專注於通過Fortify的掃描....... @ Vladimiro Corsi –

+1

@HamiltonLin:你可以閱讀更多關於java.util.concurrent.atomic.AtomicReference –