• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

getting a property via the session: slow?

 
Trailboss
Posts: 23778
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We're having a lively talk at work today centering around how long it might take to get a property from tomcat.

We have a layer that has a method where the request is passed in. At this point, everything is stateless.

The current implementation is: request.getSession().getServletContext().getInitParameter("myproperty")

Is this pretty quick?

There is some speculation that touching the session even once makes all our stuff no longer stateless. Not so?

We've thought about getting this through the servlet init(), and passing inthe context instead, but that would add complexity to this layer that we would like to avoid.

So a lazy initialization is the route we're going for now, but with the concern that if we touch a session, tomcat might choose to activate safe session management stuff and slow everything down.

Anybody know about the innards of tomcat and can tell us there is nothing to worry about?
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well I don't know the specifics for Tomcat, but as no one else has responded yet, I'll toss in my two cents.

Answer 1: This is probably a non-issue. I'd be inclined to just do it (use the session) and check the performance later. Or whip up a simple stress test now, if this is considered a really burning issue (translation: if the team is spending time worrying about this when they could be doing somethign useful).

Answer 2: "We've thought about getting this through the servlet init(), and passing in the context instead" - that sounds a little more complicated than necessary. You're getting the request from within doPost() or doGet(), right? Can't you call getServletConfig() directly there? (Or from a JSP, use the implicit object config.) And pass the ServletConfig to your other layer? It doesn't seem that complex, offhand. It's one more parameter to pass, from an API closely associated with servlets already. It's not like we're bringing in complex third-party dependencies here.

Answer 3: I guess you can always get the source for the specific version of Tomcat you've got, and look to see what it does. Might be overly complex, or it might not. Worth a glance at least...
[ February 22, 2006: Message edited by: Jim Yingst ]
 
town drunk
( and author)
Posts: 4118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'd have to agree. The simple rule, generally speaking, is the measure, do the simple thing, and measure again. Then, if you have a problem, consider a more efficient solution.

Point being: make sure there's an problem before you try to solve it.

M
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A quick persusal of the source shows that Sessions are lazy-loaded. I don't know if your avoiding touching the Session means it will never be created, but it's certainly possible. One experiment would be to use the getSession(boolean) method. Pass false, and you'll get back "null" if no session has yet been created. If you get back true, well, then calling getSession() is going to be fairly cheap.
 
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I may be being dumb here, especially as you don't give any hints about where this line of code lives, but have you tried just using:



If you code is in a method of a servlet (or, I guess, in a JSP scriptlet), then there is probably already a "getServletContext()" method provided by GenericServlet (the parent of HttpServlet).

I've certainly never needed to ask the request for a session just to get to the context
 
paul wheaton
Trailboss
Posts: 23778
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is a sort of API that resides in a jar file. The API is used within a servlet. The current interface requires that the request is passed in at certain points. Our code does not have access to any servlet stuff other than what is passed in.
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmmm, in that context (library code in a jar) it sounds like you'd want the API to have just enough hooks for your component to be able to get the info it needs, while being as simple to use as possible otherwise. Performance may or may not become an issue, but public APIs can be hard to change later.

What about something like this?

This would still be fairly easy for clients to use, but by forcing them to give you a reference to the servlet, you can get at whatever you need, either upon construction, or lazily instantiated later. Lazy instantiation may also help guard against the possibility that the ServletHelper is instantiated somewhere before the servlet has been fully initialized. You could ask them for the ServletContext or ServletConfig instead, but that would make it a little more complex for your clients to use - requiring just one extra method call in the course of instantiating this. I'd try to maximize the ease-of-use outside this class. Putting a little more complexity inside (like lazy instantiation) is not a big deal.
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Paul Wheaton:
This is a sort of API that resides in a jar file. The API is used within a servlet. The current interface requires that the request is passed in at certain points. Our code does not have access to any servlet stuff other than what is passed in.



Have you considered passing a reference to the servlet itself along with the request? Then you could call getServletContext() directly and not need to get it via the session object.

I would agree with Jim.
Write it so it's simple, then profile it to see where the bottlenecks are.
The overhead of session management may turn out to be minute compared with other things (that might be easier to optimize).
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If this was my problem I would grab all initializtion parameters to a Map instance variable in the init method. ServletContext has the convenient getInitParameterNames() method which would make this very easy.
You could attach this Map to every request as an Attribute, thus avoiding session related interactions.
Bill
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic