On Fri, 2004-04-02 at 13:47, Kal Govindu wrote:
> I'd like to know how to implement connection pooling in my
> application.
> This is what I had done so far.  Pls let me know what I need to change


you don't have to write any database pooling functions, it comes built
in!

http://jakarta.apache.org//tomcat/tomcat-5.0-doc/jndi-datasource-examples-howto.html

basically, add some stuff to the server.xml file (and restart)

then in your app (note paranoid cleaning up of database handles!):
        Connection con = null;
        Statement stmt = null;
        ResultSet rst = null;
        try
        {
            Context ctx = new InitialContext();
            if(ctx == null )
            {
                System.err.println("No Context\n");
            }
            else
            {
                DataSource ds =
(DataSource)ctx.lookup("java:comp/env/jdbc/MySQLDB");
                if (ds != null)
                {
                    con = ds.getConnection();
                    stmt = con.createStatement();
                    String sql = "select ....";
                    rst = stmt.executeQuery(sqlStmt.toString());
                    while rst.next()
                    {
....            blah blah
                    }
                   rst.close(); rst = null;
                   stmt.close(); stmt = null;
                }
            }
        }
        catch (java.sql.SQLException sqle)
        {
            if (rst != null)
                try { rst.close(); rst = null; } catch (SQLException
sqle3) { }
            if (stmt != null)
                try { stmt.close(); stmt = null;} catch (SQLException
sqle2) { }
        }


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to