The 2.2 Servlet spec says that the container must keep only one instance of a
Servlet per Web application (the exception is when a servlet implements
SingleThreadModel). However, there is a one-to-one relationship between
Sessions and jessionids (which are assumed to be a single user). For every HTTP
request there is a corresponding HttpServletRequest object which contains a
cookie (or query string) variable jsessionid. This is the variable that the
container uses to map a request to a session. Your code is locking an object
that should only be accessed by a single thread at a time (I need a stronger
argument, I know but it's friday afternoon).
Andi Setiyadi wrote:
> I have a question about database connection.
> In the code below, what happen when one request has just creating a session
> for storing the connection, and the another request is about to execute
> session.removeAttribute that will kill the session? Is the session unique
> for each request, means that when one user kill connection session will not
> affect other user's connection session? Or all request share the same
> connection session?
> Is this a good way to maintain connection for each request? Why?
>
> Thank you. I appreciated any input.
>
> Andi Setiyadi
>
> Attached code:
>
> public void doGet(HttpServletRequest req, HttpServletResponse res) throws
> IOException, ServletException {
>
> HttpSession session = req.getSession(true);
> Connection conn;
>
> synchronized(session) {
> conn = (Connection) session.getAttribute("connection");
> if (conn == null) {
> ConnectionBean cBean = new ConnectionBean();
> conn = cBean.getConnection();
> session.setAttribute("connection", conn);
> }
> }
>
> // perform some tasks
> ...
>
> try {
> if (conn != null) {
> conn.close();
> }
> }
>
> catch(Exception e) { }
>
> session.removeAttribute("connection");
> }
>
> ___________________________________________________________________________
> To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
> of the message "signoff SERVLET-INTEREST".
>
> Archives: http://archives.java.sun.com/archives/servlet-interest.html
> Resources: http://java.sun.com/products/servlet/external-resources.html
> LISTSERV Help: http://www.lsoft.com/manuals/user/user.html
--
Erik I Morton
Software Developer
------------------
CommerceHub
http://www.commercehub.com
518-886-0704
21 Corporate Drive
Clifton Park, NY 12065
___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".
Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html