问题描述
HttpSessionListener
中的sessionCreated()
方法是否自动与request.getSession()
调用同步?特别是,我想知道在sessionCreated()
方法中设置会话属性并在servlet中使用request.getSession().getAttribute("something")
检索属性是否是线程安全的?
例如,拥有
Is sessionCreated()
method from a HttpSessionListener
automatically synchronized with request.getSession()
call? In particular, I would like to know if it is thread safe to set a session attribute in the sessionCreated()
method and retrieve the attribute with request.getSession().getAttribute("something")
in a servlet?
For example, is it thread-safe to have
@Override
public void sessionCreated(HttpSessionEvent se) {
se.getSession().setAttribute("something", new Something());
}
在HttpSessionListener
中,并且拥有
Something something = (Something) request.getSession().getAttribute("something");
synchronized(something){
// do anything with this "something" object
}
在HttpServlet?
的doGet()
方法内部的
我要关注的是,如果该sessionCreated()
方法没有自动与requested.getSession()
同步,则getAttribute("something")
返回的值可以是null
. /p>
inside of a doGet()
method of a HttpServlet?
The point of my concern is that if this sessionCreated()
method is not automatically synchronized with requested.getSession()
the value returned by getAttribute("something")
can be null
.
推荐答案
这两个方法调用至少在Tomcat中在同一线程中执行.我使用log4j通过打印线程ID对其进行了检查.因此,至少在Tomcat中,上述方法调用的组合是线程安全的.
These two method calls are executed in the same thread, at least in Tomcat. I checked it with printing thread IDs using log4j. Therefore, the combination of the method calls, described above, is thread-safe, at least in Tomcat.
这篇关于来自HttpSessionListener的sessionCreated()是否自动与request.getSession()同步?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!