thread safe 관련 질문입니다.
안녕하세요.
레진, struts 환경입니다.
일단 두 코드를 보고 말씀드리겠습니다.
--------------------------------
// A
public class testAction extends Action
{
User loginUser;
public testAction(){}
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception
{
loginUser = (User)session.getAttribute("user");
}
--------------------------------------
// B
public class testAction extends Action
{
User loginUser;
public testAction()
{
loginUser = null;
}
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception
{
loginUser = (User)session.getAttribute("user");
}
-------------------------------------------
위에 A코드로 실행시에는 로그인시 내 정보가 아닌 다른사람의 정보가 나올때가
있습니다. 그리고 새로고침하면 바로 정상으로 돌아오구요
그래서 B코드처럼 생성자에 초기화를 해줬더니 그런 문제가 없어진 것 같습니다.
근데 A코드에서 명시적으로 초기화를 안해주어도 B와 같이 기본값으로 초기화 대는게아닌지 .. 두 소스의 차이가 정확히 먼지 모르겠습니다.
두 소스의 차이점을 정확히좀 설명해주시면 감사하겠습니다.
맴버변수는 명시적으로 초기화를 안해도 기본값으로 초기화가 된다고 알고 있습니다.
그럼 위의 두경우는 같은결과가 발생되야 하지않나요?
그리고 A의 경우 아래 execute에서 loginUser를 세션값으로 초기화 하고 하는데
왜 다른 사람의 정보가 보이는건지 .. 이해가안갑니다.
Thread safe에 대해서 많이 찾아봤지만 정확한 것을 몰라서 이렇게 질문드립니다.
고수님들 답변주시면 감사하겠습니다.^^
API문서를 참고하시기
API문서를 참고하시기 바랍니다.
http://struts.apache.org/1.x/apidocs/org/apache/struts/action/Action.html
Actions must be programmed in a thread-safe manner, because the controller will share the same instance for multiple simultaneous requests. This means you should design with the following items in mind:
* Instance and static variables MUST NOT be used to store information related to the state of a particular request. They MAY be used to share global resources across requests for the same action.
* Access to other resources (JavaBeans, session variables, etc.) MUST be synchronized if those resources require protection. (Generally, however, resource classes should be designed to provide their own protection where necessary.
When an Action instance is first created, the controller will call setServlet with a non-null argument to identify the servlet instance to which this Action is attached. When the servlet is to be shut down (or restarted), the setServlet method will be called with a null argument, which can be used to clean up any allocated resources in use by this Action.
서블릿과 마찬가지로 instance variable은 공유된다고 보시고 프로그래밍하셔야 합니다.
댓글 달기