"is javax.servlet.servletcontext set/getattribute thread safe?" Code Answer


you can safely assume you can call getattribute and setattribute without synchronizing on anything, but you should make the objects you store there threadsafe (the easiest way being to store things that are immutable). the question linked in the comments is about storing a mutable object in the servletcontext, in which case threads using it need to acquire its lock first (which the accepted answer explains).

there's not a spelled-out requirement. this is covered in java concurrency in practice, section 4.5.1 interpreting vague documentation:

you are going to have to guess. one way to improve the quality of your guess is to interpret the specification from the perspective of someone who will implement it (such as a container or database vendor), as opposed to someone who will merely use it. servlets are always called from a container-managed thread, and it is safe to assume that if there is more than one such thread, the container knows this. the servlet container makes available certain objects that provide service to multiple servlets, such as httpsession or servletcontext. so the servlet container should expect to have these objects accessed concurrently, since it has created multiple threads and called methods like servlet.service from them that could reasonably be expected to access the servletcontext.

since it is impossible to imagine a single-threaded context in which these objects would be useful, one has to assume that they have been made thread-safe, even though the specification does not explicitly require this. besides, if they required client-side locking, on what lock should the client code synchronize? the documentation doesn't say, and it seems absurd to guess. this “reasonable assumption” is further bolstered by the examples in the specification and official tutorials that show how to access servletcontext or httpsession and do not use any client-side synchronization.

on the other hand, the objects placed in the servletcontext or httpsession with setattribute are owned by the web application, not the servlet container. the servlet specification does not suggest any mechanism for coordinating concurrent access to shared attributes. so attributes stored by the container on behalf of the web application should be thread-safe or effectively immutable. if all the container did was store these attributes on behalf of the web application, another option would be to ensure that they are consistently guarded by a lock when accessed from servlet application code. but because the container may want to serialize objects in the httpsession for replication or passivation purposes, and the servlet container can't possibly know your locking protocol, you should make them thread-safe.

By barryleajo on July 20 2022

Answers related to “is javax.servlet.servletcontext set/getattribute thread safe?”

Only authorized users can answer the Search term. Please sign in first, or register a free account.