In my spring-jdbc project i have a class called DBStuff
which i use to connect to db and make simple db operations. It's a web project and there are users, so naturally i use session mechanism. When i need to retrieve request data in DBStuff
class, i use this line of code below :
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
But, there is no explanation that if the RequestContextHolder
is thread-safe or not. Even the spring's official forum doesn't have an answer for that. Because of using servlet, i need to provide a thread-safe nature for the users.
By definition RequestContextHolder
is defined as "Holder class to expose the web request in the form of a thread-bound RequestAttributes
object." But i am not sure if "thread-bound" stands for thread-safe.
Holder class to expose the web request in the form of a thread-bound RequestAttributes object. The request will be inherited by any child threads spawned by the current thread if the inheritable flag is set to true. Use RequestContextListener or org.springframework.web.filter.RequestContextFilter to expose the current web request.
Thread-Safe Collections. The .NET Framework 4 introduces the System.Collections.Concurrent namespace, which includes several collection classes that are both thread-safe and scalable. Multiple threads can safely and efficiently add or remove items from these collections, without requiring additional synchronization in user code.
A MessageService object is effectively immutable since its state can't change after its construction. Hence, it's thread-safe. Moreover, if MessageService were actually mutable, but multiple threads only have read-only access to it, it's thread-safe as well.
The request will be inherited by any child threads spawned by the current thread if the inheritable flag is set to true. Use RequestContextListener or org.springframework.web.filter.RequestContextFilter to expose the current web request.
"thread-bound" means that every thread has own copy of the data, so it is thread-safe.
It uses ThreadLocal
for that
private static final ThreadLocal<RequestAttributes> requestAttributesHolder =
new NamedThreadLocal<RequestAttributes>("Request attributes");
public static RequestAttributes getRequestAttributes() {
RequestAttributes attributes = requestAttributesHolder.get();
if (attributes == null) {
attributes = inheritableRequestAttributesHolder.get();
}
return attributes;
}
requestAttributesHolder.get()
returns RequestAttributes
for the current thread, it is a thread that process an HTTP
request. Every request has an own thread.
Method get()
of ThreadLocal
uses a map to bound the data to the Thread.currentThread()
public T get() {
Thread t = Thread.currentThread();
ThreadLocalMap map = getMap(t);
if (map != null) {
ThreadLocalMap.Entry e = map.getEntry(this);
if (e != null)
return (T)e.value;
}
return setInitialValue();
}
When and how should I use a ThreadLocal variable?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With