在项目中遇到的一个并发访问时session不一致的问题

出现sessionID不一致的Action类:

public class MaincontrgenAction extends AbstractAction{

private Logger log = Logger.getLogger(MaincontrgenAction.class);

private MaincontrgenVO maincontrgenVO;

private GetMaincontrgenUCB ucb;

protected ActionForward eventDispatch(String event) {
maincontrgenVO = new MaincontrgenVO();
event = "maincontrgenfirst";
maincontrgenVO.setEvent(event);

if (event.equals("maincontrgenfirst"))
return doMainContractgenfirst();
else
return mapping.findForward("default");
}

private ActionForward doMainContractgenfirst() {
String contract_num = (String) request.getParameter("contract_num");
String credit_product_name = (String) request.getParameter("credit_product_name");
String credit_product_cd = (String) request.getParameter ("credit_product_cd");

log.info("----at the beginning----"+request.getSession().getId());

if (contract_num == null)
{
contract_num = (String)request.getSession().getAttribute("contract_num");
credit_product_name = (String)request.getSession().getAttribute("credit_product_name");
}

ucb = new GetMaincontrgenUCB(request);

maincontrgenVO.setContract_num (contract_num);
maincontrgenVO.setCredit_product_name(credit_product_name);


ucb.setBaseVO(maincontrgenVO);

//执行业务Bean
maincontrgenVO = (MaincontrgenVO) executeUCB(ucb);

//这里打印出的id和上面的id是不一样的
log.info("----after execute ucb----"+request.getSession().getId());
ErrorMessage em = actionContext.getErrorMessage();

String iscredit="false";
log.info ("TypeFlag = "+maincontrgenVO.getTypeflag());
if("3".equals(maincontrgenVO.getTypeflag())){
iscredit="true";
}

request.getSession().setAttribute("credit_product_cd",credit_product_cd);
request.getSession().setAttribute("credit_product_name",credit_product_name);
request.getSession().setAttribute("iscredit",iscredit);
request.getSession().setAttribute("contract_num",contract_num);
request.setAttribute("bal",maincontrgenVO.getBal());
request.getSession().setAttribute("maincontractgenfirst", maincontrgenVO.getFirstList());
request.getSession().setAttribute("maincontractgensecond", maincontrgenVO.getSecondList());
request.setAttribute("disb_org_cd",maincontrgenVO.getDisb_org_cd());
request.setAttribute("org_name",maincontrgenVO.getOrg_name());

return mapping.findForward ("contractmaingenfirst");
}
}
 
问题原因:
struts里action是只实例化一个
所以多线程里action是共享的。因此不能出现有有状态的成员变量如
protected ActionMapping mapping;
protected ActionForm form;
protected HttpServletRequest request;
protected HttpServletResponse response;
如a请求启动一个线程把request设成A的HttpServletRequest
这时b请求启动一个线程,因为action里共享的,所以会设置同一个request,则现在request会是b的HttpServletRequest

那你a的线程再去用this.request,得到的便是b的HttpServletRequest .

这跟servlet里不能定义有状态的成员变量是一样的
问题解决:
我在AbstractAction类里加了一段代码,实例化action本身,这样每次请求就都会产生一个action实例,其他的代码就不用改了。
 

你可能感兴趣的:(在项目中遇到的一个并发访问时session不一致的问题)