This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
With 'true' it creates a session if it does not exist, with 'false' it returns active session if exists otherwise null. It would be more efficient to use 'false', so you don't unnecessarily create sessions, and create sessions only when needed.
session = req.getSesion(false)//retrieves already existing session or null //if there is no current existing session
if(session == null) session=req.getSession(true)
Now I understand that there are better ways of doing this; I could have done something like session = req.getSession() or req.getSession(true) in which case it would have returned an already existing session or created a new one where none exists; my initial response was in line with the discussion that has to do with creating a session using req.getSession(false); I do not know if I made this clear enough; and please let me know if my thought was wrong. Thanks.
Originally posted by Anselm Paulinus: I do not know if I made this clear enough
Someone said that passing "false" as the argument was better, as it avoided creating extra sessions, and only returned an existing one.
Bear said efficiency isn't the question; if you need a session, you need it.
You said it would be better to pass false, test for null, then pass true. I don't understand why you said this, as this is functionally equivalent to just passing true the first time, except that it does two lookups, so is slower!
Then Bear said "why the test?" meaning "why not just pass true?" since, as I've explained, what you recommended doesn't make any sense.
And then your last response above doesn't make any sense to me at all.
Now I got it; actually, I was trying to tell the guy that said pass false that it was a bad idea, that was my comment of checking for null then passing true which shoud have been done initially; I guess I convoluted the whole thing.
Joined: May 24, 2005
One cannot say any one of those is better than the other. It depends on the context. In the context of "invalidate" example above, or simply your code is simply trying to check if an object exists in session (if getSession returns null, you know the answer, I don't think you want to session to be created for this purpose....), it makes sense to use false. There are ohter examples given above in which case passing true makes sense.
In a nut shell, not any one of those be considered as a standard for all contexts (or all applications).
subject: difference between false and true sessions