DO NOT REPLY TO THIS EMAIL, BUT PLEASE POST YOUR BUG 
RELATED COMMENTS THROUGH THE WEB INTERFACE AVAILABLE AT
<http://issues.apache.org/bugzilla/show_bug.cgi?id=27438>.
ANY REPLY MADE TO THIS MESSAGE WILL NOT BE COLLECTED AND 
INSERTED IN THE BUG DATABASE.

http://issues.apache.org/bugzilla/show_bug.cgi?id=27438

JDBCAppender doesn't release connection in case of failure





------- Additional Comments From [EMAIL PROTECTED]  2004-03-29 01:14 -------
Below is our implementation that allows a retry due to error errors executing 
a piece of SQL statement. It assumes that this error is caused by a stale JDBC 
Connection.

   /**
     * This method is called to execute the instance of the SQL statement
     * for the first time.
     * 
     * @param sql - sql to execute
     * @throws SQLException
     */
    protected void execute(String sql) throws SQLException
    {
        execute(sql, true);
    }
    /** 
     *
     * Override this to provide an alertnate method of getting
     * connections (such as caching).  One method to fix this is to open
     * connections at the start of flushBuffer() and close them at the
     * end.  I use a connection pool outside of JDBCAppender which is
     * accessed in an override of this method.
     * 
     * Retry the statement if it failed the first time as this may due
     * to the database connection(due to being inactive beyond the timeout
     * value) being dropped by the firewall.
     * 
     * @param sql - sql statement to execute
     * @param firstTime - indicate whether this is the first time this
     *        particular instance of the SQL is executed
     **/
    protected void execute(String sql, boolean firstTime) throws SQLException
    {
        Connection con = null;
        Statement stmt = null;
        try
        {
            con = getConnection();
            stmt = con.createStatement();
            stmt.executeUpdate(sql);
        }
        catch (SQLException e)
        {
            try
            {
                if (stmt != null)
                    stmt.close();
            }
            finally
            {
                if (firstTime)
                {
                    retry(con, sql);
                }
                else
                {
                    if (con != null)
                    {
                        connection = null;
                        try
                        {
                            con.close();
                        }
                        catch (Exception ignore)
                        {}
                    }
                    throw e;
                }
            }
        }
        stmt.close();
        closeConnection(con);
    }
    /**
     * Retry the execution of the SQL statement only once.
     * @param con - connection to close and reset to null
     * @param sql - statement to reexecute
     * @throws SQLException
     */
    protected void retry(Connection con, String sql) throws SQLException
    {
        // if there is an error executing the SQL statement then
        // close the connection too. This is to overcome problems
        // associated with the firewall dropping inactive 
        // connections
        if (con != null)
        {
            connection = null;
            try
            {
                con.close();
            }
            catch (Exception ignore)
            {}
        }
        execute(sql, false);
    }

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

Reply via email to