Nearly. That's it probably shouldn't be done that way.
Pierre-Yves
-----Message d'origine-----
De : A mailing list for discussion about Sun Microsystem's Java Servlet
API Technology. [mailto:[EMAIL PROTECTED]]De la part de Andi
Setiyadi
Envoy� : vendredi 27 avril 2001 22:49
� : [EMAIL PROTECTED]
Objet : Re: Session & Database connection
If everyone has a each own session, correct me if I am wrong, it means if 5
people = 5 session = need 5 different connection object?
Thank you.
Geeta Ramani
<[EMAIL PROTECTED] To:
[EMAIL PROTECTED]
OM> cc:
Sent by: "A mailing Subject: Re: Session &
Database connection
list for discussion
about Sun
Microsystem's Java
Servlet API
Technology."
<SERVLET-INTEREST@JAV
A.SUN.COM>
04/27/01 02:12 PM
Please respond to "A
mailing list for
discussion about Sun
Microsystem's Java
Servlet API
Technology."
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
___________________________________________________________________________
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