public boolean optimizeTable()

in commons-jcs3-core/src/main/java/org/apache/commons/jcs3/auxiliary/disk/jdbc/mysql/MySQLTableOptimizer.java [152:229]


    public boolean optimizeTable()
    {
        final ElapsedTimer timer = new ElapsedTimer();
        boolean success = false;

        if ( tableState.getState() == TableState.OPTIMIZATION_RUNNING )
        {
            log.warn( "Skipping optimization. Optimize was called, but the "
                    + "table state indicates that an optimization is currently running." );
            return false;
        }

        try
        {
            tableState.setState( TableState.OPTIMIZATION_RUNNING );
            log.info( "Optimizing table [{0}]", getTableName());

            try (Connection con = dataSource.getConnection())
            {
                // TEST
                try (Statement sStatement = con.createStatement())
                {
                    try (ResultSet rs = sStatement.executeQuery( "optimize table " + getTableName() ))
                    {
                        // first row is error, then status
                        // if there is only one row in the result set, everything
                        // should be fine.
                        // This may be mysql version specific.
                        if ( rs.next() )
                        {
                            final String status = rs.getString( "Msg_type" );
                            final String message = rs.getString( "Msg_text" );

                            log.info( "Message Type: {0}", status );
                            log.info( "Message: {0}", message );

                            if ( "error".equals( status ) )
                            {
                                log.warn( "Optimization was in error. Will attempt "
                                        + "to repair the table. Message: {0}", message);

                                // try to repair the table.
                                success = repairTable( sStatement );
                            }
                            else
                            {
                                success = true;
                            }
                        }
                    }

                    // log the table status
                    final String statusString = getTableStatus( sStatement );
                    log.info( "Table status after optimizing table [{0}]: {1}",
                            getTableName(), statusString );
                }
                catch ( final SQLException e )
                {
                    log.error( "Problem optimizing table [{0}]",
                            getTableName(), e );
                    return false;
                }
            }
            catch ( final SQLException e )
            {
                log.error( "Problem getting connection.", e );
            }
        }
        finally
        {
            tableState.setState( TableState.FREE );

            log.info( "Optimization of table [{0}] took {1} ms.",
                    this::getTableName, timer::getElapsedTime);
        }

        return success;
    }