Andy
If it were me, I'd have my init call another method that started the
connection, then if during a query, your connection fails you can trap the
exception and handle it by calling your connect method again.
Doing it this way you wouldn't never have to worry about the disconnect as
the Exception handler would trap it.
Also, if my link kept dropping out, I'd be banging on the door of my link
provider.
Good luck!
Chris
On Tue, 13 Jun 2000 08:58:52 +0100, Andy C wrote:
>I've had a Servlet connected to a SQL 7 database for around
>18 months now at:
>
>http://www.r2-dvd.org
>
>Currently the webserver (JWS2.0) is on one machine and the sql server
>on another that are in the same physical location and on the same ethernet
>switch. However I need (for policital reasons) to move the webserver to
>another location (around 600 miles away) where it will be on a completely
>different network seperated from the sql server by many IP Routers.
>
>During testing I noticed that the servle would often lose connection to the
>sql server and only reloading the servlet will reconnect it.
>
>The question is, how do I make the servlet re-connect to the database should
>the original connection be lost. ?
>
>The code is something like
>
>public class DBDirect extends HttpServlet {
>Stack Connections = null;
>boolean addConnections(){
> try{
> Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
> }catch(Exception E){
> System.out.println("Can Not Load to drivert: " + E);
> }
> Connections = new Stack();
> int x=0;
> System.out.println("*-----------------------------------------*");
> System.out.println("*-------------- Opening -----------------*");
> System.out.println("*----------- Connections -----------------*");
> System.out.println("*-----------------------------------------*");
>
> try {
> for( x=0; x<10; x++){
>
>Connections.push(DriverManager.getConnection("jdbc:odbc:DVD-List","sa","") )
>;
> System.out.println("Connection created :"+x);
> }
> }
> catch(SQLException E) {
> System.out.println("Connection Error at :"+x+" E= " + E);
>// E.printStackTrace();
> Connections = null;
> return false;
> }
>ServletConfig Config;
>
>public void init(ServletConfig config) throws ServletException{
> super.init(config);
> this.Config=config;
>
> addConnections() ;
>
>}
>
>
>
>
>public void service(HttpServletRequest req, HttpServletResponse res)
> throws ServletException, IOException{
>
> Addr=req.getRemoteAddr();
> System.out.println("DBDirect Entering service:" +Addr);
>
> try {
> if ( Connections.empty() == true){
> System.out.println("Out of connections, waiting" +Addr);
> wait();
> }
> }
> catch( InterruptedException E){
> System.out.println("Interrupted error");
>
> }
> /* Do some sort of query using Result Set and Statement. Its during
>this point that
> the class will throw an exception if the connection is lost. I need to
>reconnect it here.*/
>
>
>
>
>Many Thanks
>Andy C
>
>___________________________________________________________________________
>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