You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Laurent Féral-Pierssens <la...@distincthorizon.com> on 2002/05/01 21:23:17 UTC

db connections and Abusive refresh

Hi all,

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

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

Thanks for the help,
Laurent




--
To unsubscribe:   <ma...@jakarta.apache.org>
For additional commands: <ma...@jakarta.apache.org>
Troubles with the list: <ma...@jakarta.apache.org>


Re: db connections and Abusive refresh

Posted by Eddie Bush <ek...@swbell.net>.
Use MVC - Struts works good.  Make life easier on yourself.

http://jakarta.apache.org/struts/index.html

----- Original Message -----
From: "Laurent Féral-Pierssens" <la...@distincthorizon.com>
To: "'Tomcat Users List'" <to...@jakarta.apache.org>; <gj...@qwest.net>
Sent: Thursday, May 02, 2002 10:41 AM
Subject: RE: db connections and Abusive refresh


>
> Jay,
>
> From what we do in the page, it is hardly possible to actually do that.
> But we'll try to make some example and compare both methods.
>
> Thanks,
> Laurent
>
>
> -----Original Message-----
> From: Jay Gardner [mailto:gjk@qwest.net]
> Sent: May 1, 2002 6:33 PM
> To: Tomcat Users List; Jacob Kjome
> Subject: RE: db connections and Abusive refresh
>
>
> If you are not going to use MVC is there any reason why you can't do
> your database accesses at the top of your JSP and the put the results in
> variables to be output below in the page? Sorry, if I don't understand
> your question, but it sounded like you were holding a database
> connection open until a scriplet closes it at the bottom of the page.
>
> --JG
>
> -----Original Message-----
> From: Jacob Kjome [mailto:hoju@visi.com]
> Sent: Wednesday, May 01, 2002 1:31 PM
> To: Tomcat Users List
> Subject: Re: db connections and Abusive refresh
>
> 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
> LFP> an 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
> LFP> disconnects at the bottom. When someone uses the refresh in the
> LFP> browser it leaves connections hanging/sleeping in MySQL. Is there
> LFP> anyway to avoid this?
>
> LFP> Thanks for the help,
> LFP> Laurent
>
>
>
>
> LFP> --
> LFP> To unsubscribe:
> <ma...@jakarta.apache.org>
> LFP> For additional commands:
> LFP> <ma...@jakarta.apache.org>
> LFP> Troubles with the list:
> <ma...@jakarta.apache.org>
>
>
>
> --
> Best regards,
>  Jacob                            mailto:hoju@visi.com
>
>
> --
> To unsubscribe:   <ma...@jakarta.apache.org>
> For additional commands: <ma...@jakarta.apache.org>
> Troubles with the list: <ma...@jakarta.apache.org>
>
>
> --
> To unsubscribe:   <ma...@jakarta.apache.org>
> For additional commands: <ma...@jakarta.apache.org>
> Troubles with the list: <ma...@jakarta.apache.org>
>
>
>
> --
> To unsubscribe:   <ma...@jakarta.apache.org>
> For additional commands: <ma...@jakarta.apache.org>
> Troubles with the list: <ma...@jakarta.apache.org>
>


--
To unsubscribe:   <ma...@jakarta.apache.org>
For additional commands: <ma...@jakarta.apache.org>
Troubles with the list: <ma...@jakarta.apache.org>


RE: db connections and Abusive refresh

Posted by Laurent Féral-Pierssens <la...@distincthorizon.com>.
Jay,

>From what we do in the page, it is hardly possible to actually do that.
But we'll try to make some example and compare both methods.

Thanks,
Laurent


-----Original Message-----
From: Jay Gardner [mailto:gjk@qwest.net] 
Sent: May 1, 2002 6:33 PM
To: Tomcat Users List; Jacob Kjome
Subject: RE: db connections and Abusive refresh


If you are not going to use MVC is there any reason why you can't do
your database accesses at the top of your JSP and the put the results in
variables to be output below in the page? Sorry, if I don't understand
your question, but it sounded like you were holding a database
connection open until a scriplet closes it at the bottom of the page.

--JG

-----Original Message-----
From: Jacob Kjome [mailto:hoju@visi.com]
Sent: Wednesday, May 01, 2002 1:31 PM
To: Tomcat Users List
Subject: Re: db connections and Abusive refresh

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 
LFP> an 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 
LFP> disconnects at the bottom. When someone uses the refresh in the 
LFP> browser it leaves connections hanging/sleeping in MySQL. Is there 
LFP> anyway to avoid this?

LFP> Thanks for the help,
LFP> Laurent




LFP> --
LFP> To unsubscribe:
<ma...@jakarta.apache.org>
LFP> For additional commands: 
LFP> <ma...@jakarta.apache.org>
LFP> Troubles with the list:
<ma...@jakarta.apache.org>



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


--
To unsubscribe:   <ma...@jakarta.apache.org>
For additional commands: <ma...@jakarta.apache.org>
Troubles with the list: <ma...@jakarta.apache.org>


--
To unsubscribe:   <ma...@jakarta.apache.org>
For additional commands: <ma...@jakarta.apache.org>
Troubles with the list: <ma...@jakarta.apache.org>



--
To unsubscribe:   <ma...@jakarta.apache.org>
For additional commands: <ma...@jakarta.apache.org>
Troubles with the list: <ma...@jakarta.apache.org>


RE: db connections and Abusive refresh

Posted by Jay Gardner <gj...@qwest.net>.
If you are not going to use MVC is there any reason why you can't do your
database accesses at the top of your JSP and the put the results in
variables to be output below in the page? Sorry, if I don't understand your
question, but it sounded like you were holding a database connection open
until a scriplet closes it at the bottom of the page.

--JG

-----Original Message-----
From: Jacob Kjome [mailto:hoju@visi.com]
Sent: Wednesday, May 01, 2002 1:31 PM
To: Tomcat Users List
Subject: Re: db connections and Abusive refresh

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:   <ma...@jakarta.apache.org>
LFP> For additional commands: <ma...@jakarta.apache.org>
LFP> Troubles with the list: <ma...@jakarta.apache.org>



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


--
To unsubscribe:   <ma...@jakarta.apache.org>
For additional commands: <ma...@jakarta.apache.org>
Troubles with the list: <ma...@jakarta.apache.org>


--
To unsubscribe:   <ma...@jakarta.apache.org>
For additional commands: <ma...@jakarta.apache.org>
Troubles with the list: <ma...@jakarta.apache.org>


Re: db connections and Abusive refresh

Posted by Jacob Kjome <ho...@visi.com>.
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:   <ma...@jakarta.apache.org>
LFP> For additional commands: <ma...@jakarta.apache.org>
LFP> Troubles with the list: <ma...@jakarta.apache.org>



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


--
To unsubscribe:   <ma...@jakarta.apache.org>
For additional commands: <ma...@jakarta.apache.org>
Troubles with the list: <ma...@jakarta.apache.org>