tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Michael Donaghy <micha...@michaeldonaghy.com>
Subject Tomcat4.0.2 & MySQL Connection Pools
Date Tue, 19 Feb 2002 01:41:36 GMT
After reading the JNDI stuff, I can get a MySQL connection from Tomcat.
>From my reading of the docs, it sounds like Tomcat will handle
connection pooling & that I should be able to use MySQL instead of
HypersonicSQL.  I'm using the mm.mysql-2.0.11 JDBC driver & have placed the
mm.mysql-2.0.11-bin.jar file in the common/lib directory.

I'm includeing portions of my server.xml, web.xml, A.java & a.jsp files.

The output of the servlet/A page is:
ds: tyrex.jdbc.xa.EnabledDataSource
dbclass: org.gjt.mm.mysql.jdbc2.Connection
a: 1

I'm thinking the dbclass should be some type of pooled connection
instead of the standard connection.

Thanks for any help!

Michael




SERVER.XML
==========
        <Context path="/myapp" docBase="myapp" debug="0"
reloadable="true" crossContext="true">
          <Resource name="jdbc/MyDB" auth="Container" type="javax.sql.DataSource"/>
          <ResourceParams name="jdbc/MyDB">
            <parameter>
              <name>user</name>
              <value>mydbuname</value>
             </parameter>
            <parameter>
              <name>password</name>
              <value>abc12345</value>
             </parameter>
            <parameter>
              <name>driverClassName</name>
              <value>org.gjt.mm.mysql.Driver</value>
             </parameter>
            <parameter>
              <name>driverName</name>
              <value>jdbc:mysql://localhost:3306/mydbname</value>
             </parameter>
          </ResourceParams>

WEB.XML
=======

    <resource-ref>
      <description>Resource reference for database</description>
      <res-ref-name>jdbc/MyDB</res-ref-name>
      <res-type>javax.sql.DataSource</res-type>
      <res-auth>Container</res-auth>
    </resource-ref>


A.jsp
========
<body>
ds: <% out.print(request.getAttribute("ds")); %><BR>
dbclass: <% out.print(request.getAttribute("dbclass")); %><BR>
a: <% out.print(request.getAttribute("a")); %>
</body>


A.java
==========
                try {
                        Context initCtx = new InitialContext();
                        Context envCtx = (Context) initCtx.lookup("java:comp/env");
                        javax.sql.DataSource ds = (javax.sql.DataSource) envCtx.lookup("jdbc/MyDB");
                        req.setAttribute("ds",ds.getClass().getName());
                        con = ds.getConnection();
                        req.setAttribute("dbclass", con.getClass().getName());
                        stmt = con.prepareStatement("SELECT 1");
                        rs = stmt.executeQuery();
                        if(rs.next()) {
                          a = rs.getObject(1);
                          req.setAttribute("a",a);
                        }
                } catch (Exception ex) {
                        ex.printStackTrace();
                } finally {
                        try { stmt.close(); } catch (Exception ex) { }
                        try { rs.close(); } catch (Exception ex) { }
                        try { con.close(); } catch (Exception ex) { }
                }
                getServletConfig().getServletContext().getRequestDispatcher("/a.jsp").forward(req,res);


--
To unsubscribe:   <mailto:tomcat-user-unsubscribe@jakarta.apache.org>
For additional commands: <mailto:tomcat-user-help@jakarta.apache.org>
Troubles with the list: <mailto:tomcat-user-owner@jakarta.apache.org>


Mime
View raw message