Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is RequestContextHolder thread-safe?

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.

like image 399
cihan adil seven Avatar asked Apr 29 '16 08:04

cihan adil seven


People also ask

How to expose the current web request in a thread-bound request?

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.

What is thread safe collection in net?

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.

Is a messageservice thread safe?

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.

How do I inherit a web request from another thread?

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.


Video Answer


1 Answers

"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?

like image 79
v.ladynev Avatar answered Sep 17 '22 12:09

v.ladynev