Hi, I notice that you are doing the JDBC lookup in the "init" function. This doesn't appear to work properly (at least on 4.1.6), move the initialisation code to the "doGet/doPost" and see if that helps. This cured my JNDI/JDBC problems.
Regards. ----- Original Message ----- From: "Przemyslaw Kowalczyk" <[EMAIL PROTECTED]> To: "'Tomcat Users List'" <[EMAIL PROTECTED]> Sent: Tuesday, July 02, 2002 11:08 AM Subject: Tomcat 4.0.4, jndi, jdbc and postgresql [long] > Hi > > I've just started to write servlets. Servlets that only do processing, without > connecting to database work fine, but I have some problems to get the > connection with postgresql via jdbc work. > > Tomcat: 4.0.4 (on linux) > Postgresql: 7.2.1 (on linux) > Jdbc: pgjdbc2.jar (from jdbc.postgresql.org) > > The servlet is quite simple: > > import java.io.*; > import java.util.*; > import java.sql.*; > import javax.sql.*; > import javax.naming.*; > import javax.servlet.*; > import javax.servlet.http.*; > > public class InfoServlet extends HttpServlet { > > Connection con; > private boolean conFree = true; > > public void init() throws ServletException { > try { > Context initCtx = new InitialContext(); > Context envCtx = > (Context)initCtx.lookup("java:comp/env"); > DataSource ds = (DataSource) > envCtx.lookup("jdbc/BookDB"); > Connection con = ds.getConnection(); > } catch (Exception ex) { > throw new ServletException("Couldn't open connection > to database: " + ex.getMessage()); > } > } > > public void destroy() { > try { > con.close(); > } catch (SQLException ex) { > System.out.println(ex.getMessage()); > } > } > > public void doGet (HttpServletRequest request, HttpServletResponse > response) throws ServletException, IOException { > > HttpSession session = request.getSession(); > > // set content-type header before accessing the Writer > response.setContentType("text/html"); > response.setBufferSize(8192); > PrintWriter out = response.getWriter(); > > // then write the data of the response > out.println("<html>" + > "<head><title>Duke's Bookstore</title></head>"); > } > } > > in web/WEB-INF/web.xml I defined: > > > <!DOCTYPE web-app > PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" > "http://java.sun.com/dtd/web-app_2_3.dtd"> > <web-app> > <display-name>Currency Converter Application</display-name> > <description> > Test servlet > </description> > <servlet> > <servlet-name>info</servlet-name> > <display-name>info</display-name> > <description>no description</description> > <servlet-class>InfoServlet</servlet-class> > </servlet> > <servlet-mapping> > <servlet-name>info</servlet-name> > <url-pattern>/info</url-pattern> > </servlet-mapping> > <session-config> > <session-timeout>30</session-timeout> > </session-config> > <resource-ref> > <res-ref-name>jdbc/BookDB</res-ref-name> > <res-type>javax.sql.DataSource</res-type> > <res-auth>Containter</res-auth> > </resource-ref> > </web-app> > > And in server.xml in the contex of the application: > > <Context path="/bookstore" docBase="bookstore" debug="0" > reloadable="true" crossContext="true"> > <Logger className="org.apache.catalina.logger.FileLogger" > prefix="localhost_bookstore_log." suffix=".txt" > timestamp="true"/> > <Resource name="jdbc/BookDB" auth="Container" > type="javax.sql.DataSource"/> > <ResourceParams name="jdbc/BookDB"> > <parameter> > <name>user</name> > <value>java</value> > </parameter> > <parameter> > <name>password</name> > <value>java</value> > </parameter> > <parameter> > <name>driverClassName</name> > <value>org.postgresql.Driver</value> > </parameter> > <parameter> > <name>driverName</name> > <value>jdbc:postgresql:public</value> > </parameter> > </ResourceParams> > </Context> > > I've tried a few different driverName parameters: (restarting Tomcat after > each change) > jdbc:postgresql://localhost/public > jdbc:postgresql://full.server.name/public > jdbc:postgresql:public > jdbc:postgresql://localhost/ > > But, despite the parameters, I always get the following error: > > exception > javax.servlet.ServletException: Couldn't open connection to database: > Exception creating DataSource: org.hsql.jdbcDriver > at InfoServlet.init(Unknown Source) > at javax.servlet.GenericServlet.init(GenericServlet.java:258) > at > org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:91 8) > .... > > {cut} > > I have no idea why Tomcat wants to connect using hsql driver. I grepped > through tomcat directory and found only one reference to this driver (in > examples section). I removed event the sample section, restarted Tomcat but > it didn't help. > > Does anyone know what is wrong? > > regards > Przem > > > -- > To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]> > For additional commands, e-mail: <mailto:[EMAIL PROTECTED]> > > -- To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]> For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>