Hello,

In his book, J. Hunter wrote a ConnectionHolder object
which links a connection to a session. Something like
that :

public class ConnectionHolder implements
HttpSessionBindingListener{
   private Connection con = null ;


   public ConnectionHolder(Connection con) throws
SQLException{
      this.con = con ;
      try{
         con.setAutoCommit(false) ;
      }
      catch(SQLException e){
         throw e ;
      }

   }

   public Connection getConnection(){
      return con ;
   }

   public void valueBound(HttpSessionBindingEvent
event){
   }

   public void valueUnbound(HttpSessionBindingEvent
event){
      try{
         if ((null != con) && (!con.isClosed())){
            //con.rollback() ;
            con.close() ;
         }
      }
      catch(SQLException e){
         ;
      }
   }
}

This object has to be initiated with a connection and
put into one's session.
Beware : if there are a lot of users and they hang
around your site for quite a lot of time, this may
lead to overstretch your database server capacity.

Hope this helped
Vincent


>are there not this lifecycle listeners for sessions
>(=HttpSessionListener?)?  if
>so try to use it.

Peter

Brian Moynihan wrote:

> Anyone know how to the database connection when the
session
terminates, say
> after 10 minutes of inactivity?
>
> Thanks for your help on maintaining a db connection
between servlets
using
> the session attribute
>
> Brian.
>
> -----Original Message-----
> From: Garcia Buendia, Carlos
[mailto:[EMAIL PROTECTED]]
> Sent: Tuesday, January 08, 2002 9:32 AM
> To: [EMAIL PROTECTED]
> Subject: Re: Can Database connection be maintained
between servlets
...
>
> You can use the session object to share the database
connection
between
> servlets.
>
> After verifying username and password, in the
session object you put
the
> database connection:
>
>                 //Get the session
>                 HttpSession mySession =
req.getSession(true);
>                 //Set the database connection as a
session attribute
>                 mySession.setAttribute("DBConn",
con);
>
> 'con' is the Connection object
>
> Each servlet that wanted to use that connection must
get the
connection
> object from the session:
>
>                 // Get the session
>                 HttpSession session =
req.getSession(true);
>
>                 //Get the connection object from the
session
>                 Connection con =
(Connection)session.getAttribute("DBConn");
>
> >
> >Hi there,
> >
> >Does anyone know how to maintain a database
connection between
> >servlets:
> >
> >i.e. Open the connection through a login servlet
which gives
> >the database
> >username and password. What we need, is for all
other servlets in
the
> >application to have access to this connection.
> >
> >A possible workaround would be passing the username
and
> >password and opening
> >the connection in each individual servlet, but this
I presume
> >would not be
> >very efficient in terms of speed with each servlet
opening and
> >closing the
> >database connection.
> >
> >Any help or direction would be appreciated,
> >Brian.
> >
> >


=====
Vincent BUI,
[EMAIL PROTECTED]

PS : la signature de propagande qui suit n'est pas de mon fait.

___________________________________________________________
Do You Yahoo!? -- Une adresse @yahoo.fr gratuite et en fran�ais !
Yahoo! Courrier : http://courrier.yahoo.fr

___________________________________________________________________________
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