Originally posted by Bear Bibeault:
No, it is a very bad idea. By doing so you run the risk of having separate threads compete for the same connection.
You should be using container-managed connection pooling and obtain a connection from the pool for each request, releasing the connection when the request is done with it.
If you create a Connection just for that Session, then no, you don't have that threading issue
Is that all I need to do ?
Why is only "Connection" object specially concerned
Originally posted by Bear Bibeault:
[QB]
It's not special. All session data is vulnerable to threading issues. For read-only attributes it's usually not an issue.
This is bit scary, especially for session beginner like me. My application includes multiple JSP pages that are connected by servlet and action classes(struts). Eventually it is the result display page using what user has selected in the previous pages. quite common application..
So, basically for each page I have an action class and in it I use "getParamter()" sort thing to grab what user selects, and set it into session using session.setAttribute(). I understand this object is not thread safe, but since everytime user clicks "submit" button my Action class "reset" its value, so it should be fine. Am I right ?
Secondly, bear, you said I should take care of the connection within the request scope. Do you think I should explicitly do "connection.close()" after I am done with the query ? I am not sure about that because it seems I should just "return" the open connection to the pool without closing it so it can be reused. right ? But how to "return" an open connection to pool ?
Thanks lot.
javax.naming.InitialContext ctx = new InitialContext();
DataSource ds = (DataSource)ctx.lookup(dbname);
con = ds.getConnection();
That's it. It seems it is very dummy code.
Vince Hon<br /> <br />SCJP 1.4 | SCWCD | SCBCD <br /><a href="http://vincehon.homeip.net:8000/VJW" target="_blank" rel="nofollow">http://vincehon.homeip.net:8000/VJW</a>
Do you think I should explicitly do "connection.close()" after I am done with the query ? I
42
SCJP, SCWCD, SCJWS, IBM 700,IBM 701, IBM 704, IBM 705, CA Clarity Technical<br /> <br /><a href="http://eddyleesinti.blogspot.com" target="_blank" rel="nofollow">http://eddyleesinti.blogspot.com</a>
Don't get me started about those stupid light bulbs. |