List:MySQL++« Previous MessageNext Message »
From:Simon Pickles Date:December 29 2007 9:36am
Subject:Lock Failed Exception
View as plain text  
Hello,

What is the cause of a "lock failed" mysqlpp exception, and what is the 
best approach for dealing with it?  It only happens occasionally.

I am currently just using a try/catch to field it, and re-attempting the 
select command that caused the exception:

    ///
    /// Performs SELECT command, filling vector of Type structures with 
results
    ///
    template <class Type> void Select( std::vector<Type>& results, const 
std::string& table, const std::string& where = "" )
    {
        m_sql = "SELECT * FROM ";
        m_sql.append( table );

        if (where.length())
        {
            m_sql.append(" WHERE ");
            m_sql.append( where );
        }

        while ( 1 )
        {
            try
            {
                if ( !m_conn->connected() )
                {
                    g_log->ERROR("DATABASE NOT CONNECTED\n" );
                    return;
                }
                mysqlpp::Query q = m_conn->query();
                q << m_sql;
                g_log->DAT("%s\n", q.preview().c_str() );

                m_result.reset();
                m_result = boost::shared_ptr<mysqlpp::Result>( new 
mysqlpp::Result(q.store() ) );
               
                //my struct
                Type r;

                for ( size_t i = 0; i < m_result->size(); ++i )
                {
                    r.FillFrom( m_result->at(i) );
                    results.push_back( r );
                }

                if ( m_result->size() == 0 )
                    g_log->DAT("No results\n");

                break;
            }

            catch (const mysqlpp::BadQuery& er)
            {
                // Handle any query errors
                g_log->ERROR("Database Query error: %s\n", er.what() );
                return;
            }
            catch (const mysqlpp::BadConversion& er)
            {
                // Handle bad conversions; e.g. type mismatch populating
                g_log->ERROR("Database Conversion error: %s, retrieved 
size: %d, actual size: %d\n", er.what(), er.retrieved, er.actual_size );
                return;
            }
            catch (const mysqlpp::Exception& er)
            {
                // Catch-all for any other MySQL++ exceptions
                if ( strcmp( er.what(), "lock failed" ) == 0 )
                {
                    g_log->ERROR("Database Error: %s... retrying\n", 
er.what() );
                    continue;
                }
                else
                    g_log->ERROR("Database Error: %s\n", er.what() );
                return;
            }
        }
    }


//////////////////////

Thanks for your advice.

Simon


-- 
Linux user #458601 - http://counter.li.org.



Thread
Lock Failed ExceptionSimon Pickles29 Dec
Re: Lock Failed ExceptionWarren Young31 Dec
  • Re: Lock Failed ExceptionSimon Pickles31 Dec