Hi,

Following is configuration for Tomcat Connection pool which you have to write 
in your context.xml.
If you are using Tomcat Admin, you specify parameters in web GUI. 

<Resource name="jdbc/mydbconn" type="javax.sql.DataSource" />
  <ResourceParams name="jdbc/mydbconn">
    <parameter>
      <name>maxWait</name>
      <value>5000</value>
    </parameter>
    <parameter>
      <name>maxActive</name>
      <value>10</value>
    </parameter>
    <parameter>
      <name>url</name>
      <value>*JDBC URL*</value>
    </parameter>
    <parameter>
      <name>driverClassName</name>
      <value>*DRIVER CLASS*</value>
    </parameter>
    <parameter>
      <name>maxIdle</name>
      <value>2</value>
    </parameter>
    <parameter>
      <name>username</name>
      <value>*DB USERNAME*</value>
    </parameter>
    <parameter>
      <name>password</name>
      <value>*DB PASSWORD*</value>
    </parameter>
  </ResourceParams>

Please refer to documentation
(http://tomcat.apache.org/tomcat-5.5-doc/jndi-datasource-examples-howto.html) 
if you want fine
tune parameters like maxWait, maxActive, maxIdle, etc. and believe me you will 
find it very
useful.

Now how to use that connection in your application pages. Following is the code 
snippet that you
can use it.

Context init = new InitialContext();
Context ctx = (Context) init.lookup("java:comp/env");
DataSource ds = (DataSource) ctx.lookup("jdbc/mydbconn"); //BE CAREFUL ABOUT 
NAME - jdbc/mydbconn.

public ... getData(...)
    {
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        try
        {
            conn = ds.getConnection();
            stmt = conn.createStatement();
            ...
            ...
            ...
            rs.close();
            rs = null;
            stmt.close();
            stmt = null;
            conn.close(); // Return to connection pool
            conn = null;
        }
        catch(SQLException e)
        {
            e.printStackTrace();
        }
        finally
        {
            // Always make sure result sets and statements are closed,
            // and the connection is returned to the pool
            if (rs != null)
            {
                try
                { rs.close(); } catch (SQLException e)
                { ; }
                rs = null;
            }
            if (stmt != null)
            {
                try
                { stmt.close(); } catch (SQLException e)
                { ; }
                stmt = null;
            }
            if (conn != null)
            {
                try
                { conn.close(); } catch (SQLException e)
                { ; }
                conn = null;
            }            
        }        
        ...
    }

Also you can write generic bean and use it in your JSP pages. This way it will 
ease your work.

About making website faster, there are many parameters involved. Above thing 
will definitely gain
speed if you fine tune it based upon your application and hardware. Now if you 
want more speed,
you can go with better hardware or provide more memory to tomcat. But first try 
above snippet. I
am sure you won't need another machine.


Regards,
D
--- Michael Ni <[EMAIL PROTECTED]> wrote:

> great replies from everyone.
> 
> even with connection pooling, how many connections are we looking at here?  
> if my project works as intended, im predicting from 30 to 1000 poeple 
> simultaneously hitting tomcat and sql server.
> 
> i remember when websites like friendster.com came out, it was really slow.  
> now it is much faster, do you guys know where does a student learn about how 
> to handle high traffic web applications?  is there any classes?
> 
> 
> >From: Christopher Schultz <[EMAIL PROTECTED]>
> >Reply-To: "Tomcat Users List" <users@tomcat.apache.org>
> >To: Tomcat Users List <users@tomcat.apache.org>
> >Subject: Re: web application - student need help
> >Date: Fri, 05 Jan 2007 17:10:13 -0500
> >
> >-----BEGIN PGP SIGNED MESSAGE-----
> >Hash: SHA1
> >
> >Michael,
> >
> >Michael Ni wrote:
> > > i don't get any error when there isn't that much traffic
> > >
> > > but i dont close my jdbc connections, could that be a problem?
> >
> >Oooooh, yeah. Failing to close connections is very likely to give you
> >errors, as you will end up running out of memory, or the database server
> >will cut you off when you reach the maximum number of connections.
> >
> >Someone else mentioned the use of a connection pool, which I highly
> >recommend. You can use Tomcat to configure a connection pool and then
> >obtain a JDBC DataSource through JNDI. Then, you can get a connection
> >from that for use.
> >
> >It's not really as complicated as it sounds ;)
> >
> > >         try {
> > >             DBConstants db = new DBConstants();
> > >             Class.forName(db.getDrivername());
> > >             Connection conn;
> > >             conn =
> > > DriverManager.getConnection("jdbc:microsoft:sqlserver://" +
> > > db.getHostname() + "","" + db.getUsername() + "","" + db.getPassword() +
> > > "");
> > >
> > >
> > >             Statement stmt = conn.createStatement();
> > >             ResultSet rs = stmt.executeQuery(queryStr);
> > >             return rs;
> > >         }
> > >         catch(Exception e) {
> > >             e.printStackTrace();
> > >             System.out.println("getData error");
> > >             throw new Exception();
> > >         }
> > >     }
> >
> >Let me suggest some changes to your code:
> >
> >Connection conn = null;
> >Statement stmt = null;
> >ResultSet rs = null;
> >
> >try
> >{
> >     conn = (get connection from jndi datasource)
> >     stmt = conn.createStatement();
> >     rs = stmt.executeQuery(queryStr);
> >
> >     // Package the results into Java objects
> >     // instead of using the ResultSet directly
> >
> >     return ??;
> >}
> >catch (SQLException sqle)  // Don't catch "Exception" unless you have to
> >{
> >     sqle.printStackTrace();
> >     System.err.println("getData error");  // System.err is better
> >
> >     throw sqle; // No need for a new exception; use existing one
> >}
> >finally
> >{
> >     if(null != rs)
> >         try { rs.close(); } catch (SQLException sqle)
> >         { System.err.println("Could not close ResultSet"); }
> >     if(null != stmt)
> >         try { stmt.close(); } catch (SQLException sqle)
> >         { System.err.println("Could not close Statement"); }
> >     if(null != conn)
> >         try { conn.close(); } catch (SQLException sqle)
> >         { System.err.println("Could not close Connection"); }
> >}
> >
> >This is about as clean as you can get. If you must return the ResultSet
> >to to the calling method, then you will have to do something different.
> >I recommend you copy from the ResultSet into something like an
> >ArraryList of objects specific for your needs.
> >
> >One last thing: when possible, use PreparedStatement instead of
> >Statement for your queries. The use of this class goes a long way
> >towards protecting you against SQL injection attacks (where people can
> >do nasty things like drop tables and stuff). It requires a bit more
> >setup (especially if you are trying to write a reusable method to
> >execute all your queries) but I think it's worth it.
> >
> >- -chris
> >-----BEGIN PGP SIGNATURE-----
> >Version: GnuPG v1.4.6 (MingW32)
> >Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
> >
> >iD8DBQFFnszE9CaO5/Lv0PARAjlpAJ9yhTlVRo5InvBnHsTIlTpIHJ5/+wCgtZMx
> >SoN9PKZ3jxEx4YflzuLg97o=
> >=3nJD
> >-----END PGP SIGNATURE-----
> >
> >---------------------------------------------------------------------
> >To start a new topic, e-mail: users@tomcat.apache.org
> >To unsubscribe, e-mail: [EMAIL PROTECTED]
> >For additional commands, e-mail: [EMAIL PROTECTED]
> >
> 
> _________________________________________________________________
> Get live scores and news about your team: Add the Live.com Football Page 
> www.live.com/?addtemplate=football&icid=T001MSN30A0701
> 
> 
> ---------------------------------------------------------------------
> To start a new topic, e-mail: users@tomcat.apache.org
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> 
> 


__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to