0

I have been having problems with my EE application and I believe I have discovered the root cause- static objects are available across all sessions of a tomcat webapp and do not die. I therefore need to adapt my code so that each session has unique object.

I have several classes which extend a Search. Search currently has

public static Parser parse;

as a field, and I refer to it through out my code.

Many of my other classes that extend search are created 50 times or so and I simply call super.getParse() whenever I need to use the Parser object. I want to avoid making a new one as it is a slow process.

What is the correct way to create a single Parser object and pass it around my code without it being static?

Really appreciate any advice or guidance.

2
  • Code please? And what are static objects? Commented Feb 9, 2015 at 9:56
  • In my above question you can see Parser is static. I was hoping for an answer more in the form of a design pattern than a direct correction to my code. I am sure lots of people have had to solve the same problem as me and am looking to find out how :) Commented Feb 9, 2015 at 9:58

3 Answers 3

2

here what to do:

  • remove the static from your objects

  • put the object in the user session

like this every user have his objects in his session

see this may help HttpSession - how to get the session.setAttribute?

Sign up to request clarification or add additional context in comments.

3 Comments

Please could you give some extra information how to achieve this?
i have edited my answer you can thanks by accepting it as the right answer :)
I'm still not sure how I would use this to create a Parser for each session sorry. If you could help me out with a more concrete answer I'd be happy to accept this answer (as it sounds lik it is on the right lines.
1

Either use a synchronized accessor if you really need a unique object, or use a thread-local variable.

Note that using a synchronized accessor may decrease the thoughput of your application if all queries need to access that unique object. In that case, a thread-local variable (i.e. not-so unique, but unique session-wide) would be a better solution.

See http://docs.oracle.com/javase/7/docs/api/java/lang/ThreadLocal.html

2 Comments

This reads like it could achieve exactly what I want, but the example is abstracted quite far from my use case.Could you possibly link me to any examples where it is used to create objects for each thread? I'd really appreciate it.
After some playing, I got this to work. What a great tool. Thanks!
0

You are looking for singleton pattern, you can find reference here

3 Comments

Would I not run into the same issue with a singleton variable? As you maintain a static reference to the instance and tomcat webapps never "die", would I not run into the exact same issue?
but if you never want to change your parser object, then this approach is not a problem, singleton classes never change in lifetime (for e.g printer,spooler)
I need to change it with each new session as per my post.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.