0

I am using struts 1 (maintenance tasks on a legacy project). The application is tiered in ActionClasses calling Manager classes and Manager classes instantiating and using DAO layer classes/

I have to code conditional logic, where based on a session variable, I want DAO layer to switch between different DB2 schemas.

How do I read session in a DAO java class? One obvious way is to refactor manager/dao class constructors to pass along session variables all through call-chain

in struts 2, we use ActionContext.getContext().getSession();

is there something like that which I can do in struts 1 ?

---EDIT For all the purists (including me)-------------

I am working on this code-base, for prototyping updated business logic. This is a code that's inherited , wrote way back in 2003-2004. After prototyping, this code will not be used at-all, going straight to recycle-bin.

I understand that DAO doesn't need to access HTTP session, and that's what I follow when i write my code.

But my question is, is there a way to access session in DAO in struts 1 framework (filters/hacks?)

Satish
  • 6,457
  • 8
  • 43
  • 63
  • 1
    Why do not you just pass a variable with the value that you need? – Paul Vargas May 16 '12 at 20:38
  • 1
    DAO layer shouldn't do access to context information such HTTP session, as Paul commented it should be a good alternative to pass this value as parameter to your dao method – Francisco Spaeth May 16 '12 at 20:55
  • I am passing the variable now, but there are 100+ classes that need the refactoring. That is why I posted the question, if there is a way to get session in DAO, that way I can limit changes to DAO, instead of refactoring all classes in the chain. – Satish May 16 '12 at 20:57
  • How would such a design ever be considered "layered"? If you aren't going to keep boundaries between layers, why not just dump everything into one monster servlet and get it over with? A good refactoring IDE will make short work of it. Do you really have 100+ classes in your persistence tier, all relying on session? Wait - you said Struts 1. Now I understand. – duffymo May 16 '12 at 20:57
  • Working on inherited code, last touched in 2004, with a tight deadline. I understand the concept of better layered architecture, but in real world, its not common to come across inherited code, where fixes are needed. – Satish May 16 '12 at 20:59
  • This is not a real world situation, if you give in by providing a whack solution to the already bad situation, then in 2017, the next person who comes in would say "guy in 2012 already gave access to session in DAO, its a real world situation". :) Need some integrity into this. – Oh Chin Boon May 16 '12 at 22:30

3 Answers3

1

The easiest is to have a thread local, maybe set in a filter or trivially-customized request processor, accessed via a static method. I feel dirty.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
0

You don't do this. You'll be setting yourself for massive headaches in the future. Instead, pass the value through the call chain.

Jim Barrows
  • 3,634
  • 1
  • 25
  • 36
  • edited question above, after my prototyping, there is no future, the code's going straight to recycle bin. There is a better "Spring" based version already in development. – Satish May 16 '12 at 21:41
  • Okay. I've heard this so many times, I always do it the right way anyway. Saves me heartache. ymmv – Jim Barrows May 16 '12 at 21:44
  • +1 "Okay. I've heard this so many times, I always do it the right way anyway" – Oh Chin Boon May 16 '12 at 22:32
0

If you, like me, try ThreadLocal only to learn that the request can spawn child threads, you can use a combination of ServletRequestListener (Java EE) and RequestContextHolder (Spring-web) to achieve this:

    @Override
    public void requestInitialized(ServletRequestEvent sre) {
        ServletRequest sr = sre.getServletRequest();
        if(sr instanceof HttpServletRequest)
            // second parameter will let child threads inherit this object
            RequestContextHolder.setRequestAttributes(new ServletRequestAttributes((HttpServletRequest) sr), true);
    }
    
    @Override
    public void requestDestroyed(ServletRequestEvent sre) {
        ServletRequest sr = sre.getServletRequest();
        if(sr instanceof HttpServletRequest) {
            RequestContextHolder.resetRequestAttributes();
        }
    }

And then in any place you need the HttpSession:

            ServletRequestAttributes attr = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
            HttpSession httpSession = attr.getRequest().getSession(false); // true == allow create
Zaleth
  • 1
  • 2