Brian,
I don't know if this is the best solution, but that's what I'm using:
In the class I open the session I create a TimerTask class called
RemindTask.
This TimerTask is called when a session is opened from the
onOpenSession(String,Locale) method. In this method I set the database
connection as an attribute of the session and I create a timer object that
call the method schedule wich has a RemindTask() attribute.
The RemindTask check the inactive period of the session.
If the interval between the last access to the session and 'now' is longer
than 10 minutes (600 seconds) the RemindTask class call the CloseSession()
method.
Here is the code I use:
Timer timer;
public void onOpenSession(String UserName, Locale currentLocale) {
try {
mySession.setAttribute("DBConn", con);
int seconds = 2;
timer = new Timer();
timer.schedule(new RemindTask(),
0, //initial delay
seconds*1000); //subsequent rate
} catch (Exception EXC) {}
}//END of onOpenSession method
class RemindTask extends TimerTask {
public void run() {
long lastTime = mySession.getLastAccessedTime();
Date nowDate = new Date();
long now = nowDate.getTime();
long inactivePeriod = now - lastTime;
long maxInactivePeriod = 600;
if ((inactivePeriod/1000) > maxInactivePeriod) {
CloseSession();
timer.cancel();
}
}
}//END of RemindTask class
public boolean CloseSession() {
try {
if (con != null){
con.rollback();
con.close();
}
} catch (SQLException SQLE) {
return false;
}
try {
mySession.removeAttribute("DBConn");
return true;
} catch (Exception EXC) {
return false;
}
}//END of CloseSession method
I hope it helps.
Carlos
>-----Original Message-----
>From: Brian Moynihan [mailto:[EMAIL PROTECTED]]
>Sent: Wednesday, January 09, 2002 10:32 AM
>To: [EMAIL PROTECTED]
>Subject: Close an existing db connection when the session dies???
>
>
>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.
>>
>>
>>**********************************************************************
>>Privileged, confidential and/or copyright information may be contained
>>in this e-mail. This e-mail is for the use only of the intended
>>addressee. If you are not the intended addressee, or the person
>>responsible for delivering it to the intended addressee, you may not
>>copy, forward, disclose or otherwise use it or any part of it in any
>>way whatsoever. To do so is prohibited and may be unlawful.
>>
>>If you receive this e-mail by mistake please advise the sender
>>immediately by using the reply facility in your e-mail software.
>>
>>Orygen (Ireland) Limited may monitor the content of e-mails sent and
>>received via its network for the purposes of ensuring compliance with
>>its policies and procedures.
>>
>>This message is subject to and does not create or vary any contractual
>>relationship between Orygen (Ireland) Limited and you.
>>**********************************************************************
>>
>>_______________________________________________________________
>>____________
>>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
>
>
>**********************************************************************
>Privileged, confidential and/or copyright information may be contained
>in this e-mail. This e-mail is for the use only of the intended
>addressee. If you are not the intended addressee, or the person
>responsible for delivering it to the intended addressee, you may not
>copy, forward, disclose or otherwise use it or any part of it in any
>way whatsoever. To do so is prohibited and may be unlawful.
>
>If you receive this e-mail by mistake please advise the sender
>immediately by using the reply facility in your e-mail software.
>
>Orygen (Ireland) Limited may monitor the content of e-mails sent and
>received via its network for the purposes of ensuring compliance with
>its policies and procedures.
>
>This message is subject to and does not create or vary any contractual
>relationship between Orygen (Ireland) Limited and you.
>**********************************************************************
>
>_______________________________________________________________
>____________
>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