|
> From: TitanRebel > > Anyway, I am in the process of rewriting this so that a > Connection will be created for each session. I will store the > Connection object and the CallableStatement objects in the user's > session. Now, I'm worried about sessions that timeout. Sorry I didn't get into this yesterday. Okay, a couple of things. First, you should never have any class variables in a servlet. As you've noted, servlets are multi-threaded. So, in effect, any class variable is shared among all the servlets. This causes problems all over, not just in JDBC. However, I have a simple answer for you. Create your own session object. Here's one technique, using only static methods from a helper class: First time logic: MySession.createSession(this, req); For every request: MySession.doRequest(req, res); MySession looks like this: public class MySession { private static final String KEY = "MYSESSION"; private HttpServlet servlet; // Here you can have any other session-specific values that // you would like. public MySession(HttpServlet servlet) { this.servlet = servlet; } public static void createSession(HttpServlet servlet, HttpServletRequest req) { HttpSession httpSession = req.getSession(true); httpSession.putValue(KEY, new MySession(servlet)); session.process(req, res); } public static void doRequest(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { HttpSession httpSession = req.getSession(); MySession = (MySession) httpSession.getValue(KEY); session.process(req, res); } private void process(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { // Do your thing } } The call to createSession will create a new object of type MySession and attach it to the HTTP session. From then on, each call to doRequest will retrieve the MySession object and execute its process routine. This is in effect an adapter from the multithreaded servlet class to your own session class. There are a number of modifications that could be made to this pattern. Among them is timeout logic. Simply create a timeout thread and poke it every time you process a request. If there are no requests in the specified time period, call the session's timeout method (which should first remove itself from the HTTP session, then close connections, release resources and whatever other cleanup is required). Joe
As an Amazon Associate we earn from qualifying purchases.
This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].
Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.