Using request.getSession() as a locking object?

后端 未结 7 986
面向向阳花
面向向阳花 2020-12-11 02:18

I have some java code that gets and sets a session attribute:

Object obj = session.getAttribute(TEST_ATTR);
if (obj==null) {
  obj = new MyObject();
  sessio         


        
7条回答
  •  情深已故
    2020-12-11 02:55

    Your code won't work for at least two reasons.

    1) If the session doesn't exist, then you could easily create it twice for the same user, and have an ugly race condition.

    2) If the session is not the same object across threads, then it won't work anyway. The session will probably be equals() to the same session in another thread, but that won't work.

提交回复
热议问题