You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Stefan Urban <su...@pevo.de> on 2000/09/18 16:44:25 UTC

Still Problems with Servlets and MySQL - JDBC

Hi,

I still have a very big Problem with Servlets and MySQL - JDBC.
The code is not a hack. Just a very usally way to get the Database
Connection. I still got the Problem, that when my Servlets run for a while
I don't get any Data from the Connection. I mean for a while everything
went well and some times later it won't work. I even get no error messages
in some of the known Log-Files!!! I even tried the same Code with JServ
(latest Version) and it works. You find the Code below. If I get any help
and any solutions I will use Tomcat (and I want to). Otherwise I have to
install the JServ Mod.

Thanx for any help. Best regards.

-Stefan

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.sql.*;
import servapletcom.*;
import navigation.*;
import tabsite1.*;


public class TaxiServlet extends HttpServlet {

  protected Connection dbCon;

  public void init() throws ServletException {

    dbCon = null;

    String strDBDriver = getInitParameter("JDBC_DRIVER");
    String strDBConn   = getInitParameter("DATABASE_CONNECTION");

    try {
     Class.forName(strDBDriver);
     dbCon = DriverManager.getConnection(strDBConn);
    }
    catch (Exception e) {
     System.err.println("Databaseconnection failed (init)");
     System.err.println(strDBDriver);
     System.err.println(strDBConn);
     System.err.println(e.toString());
     return;
    }
  }

  public void destroy() {

   if (dbCon != null)
   {
    try {
     dbCon.close();
     dbCon = null;
    }
    catch (Exception e) {
     System.err.println("Database close failed: " + e.toString());
    }
   }

  }


  public void service (HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {

  if (dbCon == null)
  {
      String strDBDriver = getInitParameter("JDBC_DRIVER");
      String strDBConn   = getInitParameter("DATABASE_CONNECTION");

      try {
       Class.forName(strDBDriver);
       dbCon = DriverManager.getConnection(strDBConn);
      }
      catch (Exception e) {
       System.err.println("Databaseconnection failed (init)");
       System.err.println(strDBDriver);
       System.err.println(strDBConn);
       System.err.println(e.toString());
       return;
      }
  }

        String strRS =  request.getParameter("rs");
        String strWhich = request.getParameter("which");

    if (strWhich.compareTo("__TAXI__REFRESH") == 0) {

      refresh( request, response);

    }

    if (strWhich.compareTo("__TAXI__UPDATE") == 0) {
      save(strRS, response);
    }

    if (strWhich.compareTo("__TAXI__DELETE") == 0) {
      delete(strRS, response);
    }

    if (strWhich.compareTo("__TAXI__INSERT") == 0) {
      create(strRS, response);
    }

  }

  protected void refresh(HttpServletRequest request, HttpServletResponse
response)
  {
   try {
    Statement sQuery = dbCon.createStatement();

    ResultSet rsAdressen = sQuery.executeQuery("SELECT * FROM adressen");
    String sOutput = new String();
    ArtikelDataClass adcTmp = new ArtikelDataClass();

    while (rsAdressen.next()) {

    ...  not interesting

    }

    sQuery.close();

    PrintWriter out = new PrintWriter (response.getOutputStream());
    response.setContentType("text/html");
    out.println(sOutput);
    out.close();

   } catch (SQLException e) {
     System.out.println(e.toString());
   }
    catch (IOException e) {
     System.out.println(e.toString());
   }


  }

  protected void save(String strRS, HttpServletResponse response)
  {
   ....

  }

  protected void delete(String strRS, HttpServletResponse response)
  {
    ...
   }

  protected void create(String strRS, HttpServletResponse response)
  {
   ....
  }

}



RE: mod_jk vs. mod_jserv

Posted by Mike Bremford <mi...@paperx.com>.
I believe it's also supposed to differentiate between HTTP and HTTPS
correctly.

I haven't looked at the mod_jk source, but I know that the jserv protocol
as it stands can't identify an SSL connection to tomcat. Apache/JServ got
around this with a hack (if the connection was on port 443, it's secure),
but
tomcat hasn't done this, which is why the scheme is always HTTP and
isSecure()
always returns false if you connect via mod_jserv.

Cheers... Mike


> -----Original Message-----
> From: Thomas T. Veldhouse [mailto:veldy@veldy.net]
> Sent: 18 September 2000 18:31
> To: tomcat-user@jakarta.apache.org; Daniel.Barclay@digitalfocus.com
> Subject: Re: mod_jk vs. mod_jserv
>
>
> It seems to me that this modules will not instantiate a
> separate instance of
> the Java interpreter, as mod_jserv does.  Thus you save on
> resources.  I
> assume that for this very reason, it is probably faster as well.
>
> Tom Veldhouse
> veldy@veldy.net
>
> ----- Original Message -----
> From: Daniel Barclay <Da...@digitalfocus.com>
> To: <to...@jakarta.apache.org>
> Sent: Monday, September 18, 2000 12:02 PM
> Subject: mod_jk vs. mod_jserv
>
>
> >
> > Is there any advantage to using mod_jk instead of mod_jserv (with
> > Apache)?  More specifically, is there any need to use mod_jk (e.g.,
> > to get exclusive features) instead of mod_jserv?
> >
> > The "Working with mod_jk" (at
> >
> http://jakarta.apache.org/tomcat/jakarta-tomcat/src/doc/mod_jk
-howto.html)
> doesn't seem to say anything about that.
>
> Thanks,
>
> Daniel
> --
> Daniel Barclay
> Digital Focus
> Daniel.Barclay@digitalfocus.com
>



Re: mod_jk vs. mod_jserv

Posted by "Thomas T. Veldhouse" <ve...@veldy.net>.
It seems to me that this modules will not instantiate a separate instance of
the Java interpreter, as mod_jserv does.  Thus you save on resources.  I
assume that for this very reason, it is probably faster as well.

Tom Veldhouse
veldy@veldy.net

----- Original Message -----
From: Daniel Barclay <Da...@digitalfocus.com>
To: <to...@jakarta.apache.org>
Sent: Monday, September 18, 2000 12:02 PM
Subject: mod_jk vs. mod_jserv


>
> Is there any advantage to using mod_jk instead of mod_jserv (with
> Apache)?  More specifically, is there any need to use mod_jk (e.g.,
> to get exclusive features) instead of mod_jserv?
>
> The "Working with mod_jk" (at
> http://jakarta.apache.org/tomcat/jakarta-tomcat/src/doc/mod_jk-howto.html)
> doesn't seem to say anything about that.
>
> Thanks,
>
> Daniel
> --
> Daniel Barclay
> Digital Focus
> Daniel.Barclay@digitalfocus.com
>


handling .jsp files in Apache user directories

Posted by Daniel Barclay <Da...@digitalfocus.com>.
Is this a FAQ that is already answered somewhere?:

Can TomCat 3.1 serve as an Apache handler for .jsp files that are contained 
within the regular content directories, specifically including user direc-
tories (e.g., "http://server/~username")?


I've figured out a (crude) way to set up TomCat to handle .jsp files 
under Apache's document root directory.  

(In server.xml, I used '<Context path="" docbase="/path/to/Apache/htdocs" ...>' 
to have TomCat look for .jsp files in the same directories from which Apache is
serving out regular files.

That is, just as the URL http://localhost/f normally maps to file .../htdocs/f, 
the URL http://localhost/f.jsp maps to file .../htdocs/f.jsp (instead of a file
f.jsp in a completely separate directory).)

However, that of course doesn't work for .jsp files in user directories.  
(Tomcat reports the error:
    "/path/to/Apache/htdocs/~someuser/test.jsp" not found 
.)


Is there a way for TomCat to look for files relative to the directory 
("/home/username/public_html") usually associated with the URL instead of 
relative to the original URL (".../~username")?



Thanks,

Daniel
-- 
Daniel Barclay
Digital Focus
Daniel.Barclay@digitalfocus.com

mod_jk vs. mod_jserv

Posted by Daniel Barclay <Da...@digitalfocus.com>.
Is there any advantage to using mod_jk instead of mod_jserv (with
Apache)?  More specifically, is there any need to use mod_jk (e.g.,
to get exclusive features) instead of mod_jserv?

The "Working with mod_jk" (at 
http://jakarta.apache.org/tomcat/jakarta-tomcat/src/doc/mod_jk-howto.html)
doesn't seem to say anything about that.

Thanks,

Daniel
-- 
Daniel Barclay
Digital Focus
Daniel.Barclay@digitalfocus.com

Re: Still Problems with Servlets and MySQL - JDBC

Posted by Kevin Sangeelee <ke...@susa.net>.
We had this one a while back and from what I recall there is a parameter
in mysqld that specifies a connection timeout. After this period of time
elapses, the server thread will close down, breaking the connection, and a
new thread will be created. It does this to reduce the chances of resource
wind ups etc.

Two solutions suggested were to (1) change the mysqld timeout and/or (2)
use a connection pooling class that verifies a connection before handing
it to you, re-connecting where necessary.

In my opinion, it's not a good idea to create connections in init()
because lots of things can happen after this call.

Kevin Sangeelee

p.s. Open Source JSP tree control (CSTreeBean-0.3) at 
http://www.coolservlets.com

On Mon, 18 Sep 2000, Stefan Urban wrote:

> I still have a very big Problem with Servlets and MySQL - JDBC.
> The code is not a hack. Just a very usally way to get the Database
> Connection. I still got the Problem, that when my Servlets run for a while
> I don't get any Data from the Connection. I mean for a while everything
> went well and some times later it won't work. I even get no error messages
> in some of the known Log-Files!!! I even tried the same Code with JServ
> (latest Version) and it works. You find the Code below. If I get any help
> and any solutions I will use Tomcat (and I want to). Otherwise I have to
> install the JServ Mod.