• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Access Session variable via thread local context - Found an example in the internet

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just come across this example -  http://veerasundar.com/blog/2010/11/java-thread-local-how-to-use-and-code-sample/

Is this is the correct approach to acess session variables in any layer - presentation, dao, service layer ???

Thanks.
 
Marshal
Posts: 24594
55
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Definitely not. A session variable can persist over more than one request to the app, that's why you put things into a session. But those requests can run in different threads, so a ThreadLocal variable wouldn't persist properly.

It's also worth noting that the ThreadLocal technique described in that blog entry essentially describes storing data in request scope, which is already supported directly by JSTL (for example). So it may be less useful than you think.
 
Kathir jeyap
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So Paul what would be the right approach...Shall i do as below

1. create an interceptor which extends HandlerInterceptorAdapter
2. put the session variable in a thread local variable per request
3. clear the thread local variable in afterConcurrentHandlingStarted and afterCompletion method

import org.slf4j.MDC;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import java.util.HashSet;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class SessionHandlerInterceptor extends HandlerInterceptorAdapter {
  /**
   * set of keys added to MDC so can be removed
   */
  private ThreadLocal<Set<String>> storedKeys = ThreadLocal.withInitial(() -> new HashSet<>());

  @Override
  public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
     addKey("sessionId", request.getHeader("X-Session-Id"));
     addKey("userId", resolveUser(request));
     return true;
  }


private void addKey(String key, String value) {
     storedKeys.get().add(key);
  }

  @Override
  public void afterConcurrentHandlingStarted(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
     // request ended on current thread remove properties
     // clear the variables - key and values
removeKeys();
  }

  @Override
  public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
          throws Exception {
    // clear the variables - key and values
removeKeys();
  }

private void removeKeys() {
     storedKeys.remove();
  }
 
}



import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@Configuration
public class BeanConfiguration {

  @Bean
  public SessionHandlerInterceptor sessionHandlerInterceptor() {
     return new SessionHandlerInterceptor();
  }

  @Bean
  public WebMvcConfigurerAdapter webConfigurer() {
     return new WebMvcConfigurerAdapter() {
        @Override
        public void addInterceptors(InterceptorRegistry registry) {
           registry.addInterceptor(sessionHandlerInterceptor());
        }
     };
  }
}
 
Paul Clapham
Marshal
Posts: 24594
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm sorry, it appears you're asking a question about Spring (which I know nothing about). So let's move this thread to the Spring forum, so it can attract people who know what they're talking about.
 
Kathir jeyap
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul, i am not sure how to move it..Could you please move this to Spring forum ??

Thanks.
 
Paul Clapham
Marshal
Posts: 24594
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I moved it already.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!