Andi;

Every user who executes doGet will run in his/her own thread and so one
person's session/connection will never "get mixed up" in another person's
session or connection. ..So in fact you don't have to even add the
"synchronized(session) " bit. (It is automatically synchronized.)

However, getting and relasing a connection thsi way is not (generally) a good
idea: you may want to use a connection pool (though the reasons have nothing to
do with synchronization, but with performance, etc.). (You may want to search
the archives under "Connection pool")

Also if this is confusing, maybe you should study how a servlet works (and
about its life cycle) in Jason's (or any other basic) book.
Good luck!
Geeta

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

___________________________________________________________________________
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

Reply via email to