> At 10:41 AM -0400 7/20/99, Joe Yandle wrote:
> >I had a similar problem on linux / apache-1.3.6 / JServ-1.0 /
> >mysql-jdbc-1.0. When a servlet stayed loaded for a few days, the
> >Connection would get stale and I'd get a nasty IOException when the
> >connection was used.
> >
> >I solved it by having my Database class implement runnable, and whenever
> >my servlets would instantiate a Database object they would also create a
> >thread with that object and call start(). All the run() method did was
> >sleep for an hour and then recreate the connection.
> >
> >
> >Joe Yandle
> >Internet Programmer
> >Westlake Consulting Group
>
> I'd wondered how people handle that, but I guess I still do. Do you then
> synchronize your db access, so that your sleepy thread doesn't come and
> tear down the connection and rebuild it just as someone wants to (or is)
> using it?
>
I generally use the following framework for servlet apps. I use a MVC
type paradigm, with an abstract Model class that inherits from
HttpServlet. I then have a Database class that creates a Connection
object in its contructor.
The Database object is created in the Model constructor, along with the
Thread object. The Models don't keep the Connection itself; when they
need one, they call the getConnection() method of class Database. They
Models pass this to anything that needs it.
The beauty of this approach is that any object that got a Connection from
the Model will still have the Connection, even when Database refreshes its
Connection object, since the original Connection was passed by reference.
The old Connection won't be discarded until nothing is using it anymore,
and anytime a new Connection is requested it will provide the new one.
Did this make sense?
Joe Yandle
Internet Programmer
Westlake Consulting Group
--
--------------------------------------------------------------
To subscribe: [EMAIL PROTECTED]
To unsubscribe: [EMAIL PROTECTED]
READ THE FAQ!!!! <http://java.apache.org/faq/>
Archives and Other: <http://java.apache.org/main/mail.html/>
Problems?: [EMAIL PROTECTED]