tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jacob Kjome <h...@visi.com>
Subject Re: db connections and Abusive refresh
Date Wed, 01 May 2002 19:31:00 GMT
Hello Laurent,

Yes,

Here is an example.  Take notice of how connections, result sets, and
prepared statements are guaranteed to be closed in the "finally"
blocks:


public Site getSite( int _id ) throws SQLException
    {
        Connection conn = null;
        Site newSite    = null;
        
        try {
            conn = openConn();
            newSite = getSite(_id, conn);
            closeConn(conn);
            return newSite;
        }
        catch(SQLException e) {
            System.out.println(MSG_ERROR_CONNECTION + e.getMessage());
            throw e;
        }
        finally {
            if (conn != null)  try { closeConn(conn); } catch(Exception e2) {}
        }
    }
    
    //  Get a single site by id
    public Site getSite( int _id, Connection _conn ) throws SQLException
    {
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        Site newSite = null;
        
        String query = "SELECT id, url, description, created, updated, deleted " +
                       "FROM site WHERE id = ?";
        
        try {
            pstmt = _conn.prepareStatement(query);
            pstmt.setInt(1,_id);
            rs = pstmt.executeQuery();
            
            while (rs.next()) {
                newSite = Site.fromResults(rs);
                if (debug) System.out.println("\n" + newSite);
            }
            rs.close();
            pstmt.close();
            return newSite;
        }
        catch(SQLException e) {
            System.out.println(MSG_ERROR_RESULTSET + e.getMessage());
            throw e;
        }
        finally {
            if (rs != null)    try { rs.close(); }    catch(Exception e2) {}
            if (pstmt != null) try { pstmt.close(); } catch(Exception e3) {}
        }
    }


You can also use Syncronization if it is still a problem.
    
Jake
    
Wednesday, May 01, 2002, 2:23:17 PM, you wrote:


LFP> Hi all,

LFP> We are experiencing a few problems with our DB connection code and an
LFP> abusive usage of the F5/refresh function on the client side.

LFP> Each of our jsp actually connects to the db at the top and disconnects
LFP> at the bottom. When someone uses the refresh in the browser it leaves
LFP> connections hanging/sleeping in MySQL. Is there anyway to avoid this?

LFP> Thanks for the help,
LFP> Laurent




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



-- 
Best regards,
 Jacob                            mailto:hoju@visi.com


--
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