Answers.Body: <p>The CP was also showing tasks getting stuck on ‘Deleting Stale Template Caches’ – having seen <a href="https://plus.google.com/+KevinOuellette/posts/NW9VvZCxpK1" rel="nofollow">this post</a>, I changed the task to ‘error’ in the db. The task then ran successfully, and template caching was no longer a problem.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-03T13:25:46.667Z
Answers.LastActivityDate: 2014-07-03T13:25:46.667Z
Answers.UserDisplayName: James
Answers.Body: <p>That error means that one database request comes into MySQL and grabs an exclusive handle on a table.  For whatever reason, it doesn't release that handle in a reasonable amount of time, so another request comes in wanting access to the same table, but gives up waiting on the first one to finish.</p>
  
  <p>If you open up your craft/storage/runtime/logs files and find the error in there, it will come with a full stack trace where you can track down where the error is actually occurring.  Better yet... redact any sensitive details and place it here so we can all learn from it. :)</p>
  
  <p><strong>Update</strong></p>
  
  <p>From the stack trace it looks like the timeout is occurring when Craft is in the process of running it's "DeleteStaleTemplateCache" task.  It also looks like you're not on the latest release.  You should update to the latest 2.1 build where we've made that task much less aggressive when doing it's database queries.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-03T14:13:54.553Z
Answers.Text: do you think a likely cause would be too many/big caches? Deleting Stale Template Caches takes a long time – the database for the site is huge.
Answers.UserDisplayName: James
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-08-20T15:54:06.323Z
Answers.Text: I'm having the same issue here, got a very big template cache and this "Deleting stale template caches" never finishes.
Answers.UserDisplayName: GlennJ
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-03-14T14:22:25.460Z
Answers.Text: Same issue here. This is very annoying - anything we can do against this?
Answers.UserDisplayName: Luke
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-02T15:18:43.067Z
Answers.LastActivityDate: 2014-07-02T16:21:30.990Z
Answers.LastEditDate: 2014-07-02T16:21:30.990Z
Answers.UserDisplayName: Brad Bell
Body: <p>This may not be Craft related of course but I’m getting the following error:</p>
  
  <blockquote>
    <p><strong>Internal Server Error</strong> CDbCommand failed to execute the SQL statement: SQLSTATE[HY000]: General error: 1205 Lock wait timeout
    executed; try restarting transaction</p>
  </blockquote>
  
  <p>Any leads to likely causes very much appreciated.</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>Updated Craft and cleared caches but still got the error. A temporary workaround was to remove the cache tag in the site’s templates.</p>
  
  <p>For reference, here’s what the error looks like in the log:</p>
  
  <pre><code>2014/07/02 15:23:31 [error] [system.db.CDbCommand] CDbCommand::execute() failed: SQLSTATE[HY000]: General error: 1205 Lock wait timeout exceeded; try restarting transaction. The SQL statement executed was: DELETE FROM `craft_templatecaches`.
  2014/07/02 15:23:31 [error] [exception.Twig_Error_Runtime] exception 'CDbException' with message 'CDbCommand failed to execute the SQL statement: SQLSTATE[HY000]: General error: 1205 Lock wait timeout exceeded; try restarting transaction' in /craft/app/framework/db/CDbCommand.php:358
  Stack trace:
  #0 /craft/app/framework/db/CDbCommand.php(1302): CDbCommand-&gt;execute(Array)
  #1 /craft/app/etc/db/DbCommand.php(404): CDbCommand-&gt;delete('craft_templatec...', Array, Array)
  #2 /craft/app/services/TemplateCacheService.php(380): Craft\DbCommand-&gt;delete('templatecaches', Array, Array)
  #3 /craft/app/services/TemplateCacheService.php(406): Craft\TemplateCacheService-&gt;deleteExpiredCaches()
  #4 /craft/app/services/TemplateCacheService.php(37): Craft\TemplateCacheService-&gt;deleteExpiredCachesIfOverdue()
  #5 /craft/storage/runtime/compiled_templates/4e/63/XXXXX.php(54): Craft\TemplateCacheService-&gt;getTemplateCache('XXXXX...', false)
  #6 /craft/app/vendor/twig/twig/lib/Twig/Template.php(144): __TwigTemplate_XXXXX-&gt;block_content(Array, Array)
  #7 /craft/storage/runtime/compiled_templates/4e/7f/XXXXX.php(260): Twig_Template-&gt;displayBlock('content', Array, Array)
  #8 /craft/app/vendor/twig/twig/lib/Twig/Template.php(278): __TwigTemplate_XXXXX-&gt;doDisplay(Array, Array)
  #9 /craft/app/vendor/twig/twig/lib/Twig/Template.php(252): Twig_Template-&gt;displayWithErrorHandling(Array, Array)
  #10 /craft/storage/runtime/compiled_templates/4e/63/XXXXX.php(41): Twig_Template-&gt;display(Array, Array)
  #11 /craft/app/vendor/twig/twig/lib/Twig/Template.php(278): __TwigTemplate_XXXXX-&gt;doDisplay(Array, Array)
  #12 /craft/app/vendor/twig/twig/lib/Twig/Template.php(252): Twig_Template-&gt;displayWithErrorHandling(Array, Array)
  #13 /craft/app/vendor/twig/twig/lib/Twig/Template.php(263): Twig_Template-&gt;display(Array)
  #14 /craft/app/vendor/twig/twig/lib/Twig/Environment.php(292): Twig_Template-&gt;render(Array)
  #15 /craft/app/services/TemplatesService.php(158): Twig_Environment-&gt;render('_views/_section', Array)
  #16 /craft/app/controllers/BaseController.php(75): Craft\TemplatesService-&gt;render('_views/_section', Array)
  #17 /craft/app/controllers/TemplatesController.php(185): Craft\BaseController-&gt;renderTemplate('_views/_section', Array)
  #18 /craft/app/controllers/TemplatesController.php(27): Craft\TemplatesController-&gt;_render('_views/_section', Array)
  #19 [internal function]: Craft\TemplatesController-&gt;actionRender('_views/_section', Array)
  #20 /craft/app/framework/web/actions/CAction.php(108): ReflectionMethod-&gt;invokeArgs(Object(Craft\TemplatesController), Array)
  #21 /craft/app/framework/web/actions/CInlineAction.php(47): CAction-&gt;runWithParamsInternal(Object(Craft\TemplatesController), Object(ReflectionMethod), Array)
  #22 /craft/app/framework/web/CController.php(308): CInlineAction-&gt;runWithParams(Array)
  #23 /craft/app/framework/web/CController.php(286): CController-&gt;runAction(Object(CInlineAction))
  #24 /craft/app/framework/web/CController.php(265): CController-&gt;runActionWithFilters(Object(CInlineAction), NULL)
  #25 /craft/app/framework/web/CWebApplication.php(282): CController-&gt;run('render')
  #26 /craft/app/framework/web/CWebApplication.php(141): CWebApplication-&gt;runController('templates/rende...')
  #27 /craft/app/etc/web/WebApp.php(247): CWebApplication-&gt;processRequest()
  #28 /craft/app/framework/base/CApplication.php(180): Craft\WebApp-&gt;processRequest()
  #29 /craft/app/index.php(196): CApplication-&gt;run()
  #30 /index.php(17): require_once('/var/www/vhosts...')
  #31 {main}
  
  Next exception 'Twig_Error_Runtime' with message 'An exception has been thrown during the rendering of a template ("CDbCommand failed to execute the SQL statement: SQLSTATE[HY000]: General error: 1205 Lock wait timeout exceeded; try restarting transaction") in "_views/_section" at line 35.' in /craft/app/vendor/twig/twig/lib/Twig/Template.php:148
  Stack trace:
  #0 /craft/storage/runtime/compiled_templates/4e/7f/XXXXX.php(260): Twig_Template-&gt;displayBlock('content', Array, Array)
  #1 /craft/app/vendor/twig/twig/lib/Twig/Template.php(278): __TwigTemplate_XXXXX-&gt;doDisplay(Array, Array)
  #2 /craft/app/vendor/twig/twig/lib/Twig/Template.php(252): Twig_Template-&gt;displayWithErrorHandling(Array, Array)
  #3 /craft/storage/runtime/compiled_templates/4e/63/XXXXX.php(41): Twig_Template-&gt;display(Array, Array)
  #4 /craft/app/vendor/twig/twig/lib/Twig/Template.php(278): __TwigTemplate_XXXXX-&gt;doDisplay(Array, Array)
  #5 /craft/app/vendor/twig/twig/lib/Twig/Template.php(252): Twig_Template-&gt;displayWithErrorHandling(Array, Array)
  #6 /craft/app/vendor/twig/twig/lib/Twig/Template.php(263): Twig_Template-&gt;display(Array)
  #7 /craft/app/vendor/twig/twig/lib/Twig/Environment.php(292): Twig_Template-&gt;render(Array)
  #8 /craft/app/services/TemplatesService.php(158): Twig_Environment-&gt;render('_views/_section', Array)
  #9 /craft/app/controllers/BaseController.php(75): Craft\TemplatesService-&gt;render('_views/_section', Array)
  #10 /craft/app/controllers/TemplatesController.php(185): Craft\BaseController-&gt;renderTemplate('_views/_section', Array)
  #11 /craft/app/controllers/TemplatesController.php(27): Craft\TemplatesController-&gt;_render('_views/_section', Array)
  #12 [internal function]: Craft\TemplatesController-&gt;actionRender('_views/_section', Array)
  #13 /craft/app/framework/web/actions/CAction.php(108): ReflectionMethod-&gt;invokeArgs(Object(Craft\TemplatesController), Array)
  #14 /craft/app/framework/web/actions/CInlineAction.php(47): CAction-&gt;runWithParamsInternal(Object(Craft\TemplatesController), Object(ReflectionMethod), Array)
  #15 /craft/app/framework/web/CController.php(308): CInlineAction-&gt;runWithParams(Array)
  #16 /craft/app/framework/web/CController.php(286): CController-&gt;runAction(Object(CInlineAction))
  #17 /craft/app/framework/web/CController.php(265): CController-&gt;runActionWithFilters(Object(CInlineAction), NULL)
  #18 /craft/app/framework/web/CWebApplication.php(282): CController-&gt;run('render')
  #19 /craft/app/framework/web/CWebApplication.php(141): CWebApplication-&gt;runController('templates/rende...')
  #20 /craft/app/etc/web/WebApp.php(247): CWebApplication-&gt;processRequest()
  #21 /craft/app/framework/base/CApplication.php(180): Craft\WebApp-&gt;processRequest()
  #22 /craft/app/index.php(196): CApplication-&gt;run()
  #23 /index.php(17): require_once('/var/www/vhosts...')
  #24 {main}
  REQUEST_URI=/features
  ---
  2014/07/02 15:23:31 [error] [application] Unable to find the template “500”.
  2014/07/02 15:23:31 [error] [application] Unable to find the template “error”.
  </code></pre>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-07-02T15:39:42.370Z
Comments.Text: What version/build of Craft are you on?
Comments.UserDisplayName: Brad Bell
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-07-02T15:46:32.890Z
Comments.Text: Craft Pro 2.0.2543
Comments.UserDisplayName: James
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-07-03T13:07:27.357Z
Comments.Text: @James Glad you got this sorted. To keep your question in-line with the _Question-then-Answers_ format of this site, can you please remove the solution from your question, and post it as an answer? You’re allowed to answer your own questions, and accept it as the best answer.
Comments.UserDisplayName: Brandon Kelly
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-07-03T15:19:48.253Z
Comments.Text: Done. Thanks Brandon, didn’t realise I had those privileges yet.
Comments.UserDisplayName: James
Tags: <templating><database><cache>
Title: CDbCommand failed to execute
ThreadText: # CDbCommand failed to execute
  
  (Asked by: username_0 on 2014-07-02)
  
  This may not be Craft related of course but I’m getting the following error:
  
  > **Internal Server Error** CDbCommand failed to execute the SQL statement: SQLSTATE[HY000]: General error: 1205 Lock wait timeout
  > executed; try restarting transaction
  
  Any leads to likely causes very much appreciated.
  
  **EDIT:**
  
  Updated Craft and cleared caches but still got the error. A temporary workaround was to remove the cache tag in the site’s templates.
  
  For reference, here’s what the error looks like in the log:
  
  ```
  2014/07/02 15:23:31 [error] [system.db.CDbCommand] CDbCommand::execute() failed: SQLSTATE[HY000]: General error: 1205 Lock wait timeout exceeded; try restarting transaction. The SQL statement executed was: DELETE FROM `craft_templatecaches`.
  2014/07/02 15:23:31 [error] [exception.Twig_Error_Runtime] exception 'CDbException' with message 'CDbCommand failed to execute the SQL statement: SQLSTATE[HY000]: General error: 1205 Lock wait timeout exceeded; try restarting transaction' in /craft/app/framework/db/CDbCommand.php:358
  Stack trace:
  #0 /craft/app/framework/db/CDbCommand.php(1302): CDbCommand->execute(Array)
  #1 /craft/app/etc/db/DbCommand.php(404): CDbCommand->delete('craft_templatec...', Array, Array)
  #2 /craft/app/services/TemplateCacheService.php(380): Craft\DbCommand->delete('templatecaches', Array, Array)
  #3 /craft/app/services/TemplateCacheService.php(406): Craft\TemplateCacheService->deleteExpiredCaches()
  #4 /craft/app/services/TemplateCacheService.php(37): Craft\TemplateCacheService->deleteExpiredCachesIfOverdue()
  #5 /craft/storage/runtime/compiled_templates/4e/63/XXXXX.php(54): Craft\TemplateCacheService->getTemplateCache('XXXXX...', false)
  #6 /craft/app/vendor/twig/twig/lib/Twig/Template.php(144): __TwigTemplate_XXXXX->block_content(Array, Array)
  #7 /craft/storage/runtime/compiled_templates/4e/7f/XXXXX.php(260): Twig_Template->displayBlock('content', Array, Array)
  #8 /craft/app/vendor/twig/twig/lib/Twig/Template.php(278): __TwigTemplate_XXXXX->doDisplay(Array, Array)
  #9 /craft/app/vendor/twig/twig/lib/Twig/Template.php(252): Twig_Template->displayWithErrorHandling(Array, Array)
  #10 /craft/storage/runtime/compiled_templates/4e/63/XXXXX.php(41): Twig_Template->display(Array, Array)
  #11 /craft/app/vendor/twig/twig/lib/Twig/Template.php(278): __TwigTemplate_XXXXX->doDisplay(Array, Array)
  #12 /craft/app/vendor/twig/twig/lib/Twig/Template.php(252): Twig_Template->displayWithErrorHandling(Array, Array)
  #13 /craft/app/vendor/twig/twig/lib/Twig/Template.php(263): Twig_Template->display(Array)
  #14 /craft/app/vendor/twig/twig/lib/Twig/Environment.php(292): Twig_Template->render(Array)
  #15 /craft/app/services/TemplatesService.php(158): Twig_Environment->render('_views/_section', Array)
  #16 /craft/app/controllers/BaseController.php(75): Craft\TemplatesService->render('_views/_section', Array)
  #17 /craft/app/controllers/TemplatesController.php(185): Craft\BaseController->renderTemplate('_views/_section', Array)
  #18 /craft/app/controllers/TemplatesController.php(27): Craft\TemplatesController->_render('_views/_section', Array)
  #19 [internal function]: Craft\TemplatesController->actionRender('_views/_section', Array)
  #20 /craft/app/framework/web/actions/CAction.php(108): ReflectionMethod->invokeArgs(Object(Craft\TemplatesController), Array)
  #21 /craft/app/framework/web/actions/CInlineAction.php(47): CAction->runWithParamsInternal(Object(Craft\TemplatesController), Object(ReflectionMethod), Array)
  #22 /craft/app/framework/web/CController.php(308): CInlineAction->runWithParams(Array)
  #23 /craft/app/framework/web/CController.php(286): CController->runAction(Object(CInlineAction))
  #24 /craft/app/framework/web/CController.php(265): CController->runActionWithFilters(Object(CInlineAction), NULL)
  #25 /craft/app/framework/web/CWebApplication.php(282): CController->run('render')
  #26 /craft/app/framework/web/CWebApplication.php(141): CWebApplication->runController('templates/rende...')
  #27 /craft/app/etc/web/WebApp.php(247): CWebApplication->processRequest()
  #28 /craft/app/framework/base/CApplication.php(180): Craft\WebApp->processRequest()
  #29 /craft/app/index.php(196): CApplication->run()
  #30 /index.php(17): require_once('/var/www/vhosts...')
  #31 {main}
  
  Next exception 'Twig_Error_Runtime' with message 'An exception has been thrown during the rendering of a template ("CDbCommand failed to execute the SQL statement: SQLSTATE[HY000]: General error: 1205 Lock wait timeout exceeded; try restarting transaction") in "_views/_section" at line 35.' in /craft/app/vendor/twig/twig/lib/Twig/Template.php:148
  Stack trace:
  #0 /craft/storage/runtime/compiled_templates/4e/7f/XXXXX.php(260): Twig_Template->displayBlock('content', Array, Array)
  #1 /craft/app/vendor/twig/twig/lib/Twig/Template.php(278): __TwigTemplate_XXXXX->doDisplay(Array, Array)
  #2 /craft/app/vendor/twig/twig/lib/Twig/Template.php(252): Twig_Template->displayWithErrorHandling(Array, Array)
  #3 /craft/storage/runtime/compiled_templates/4e/63/XXXXX.php(41): Twig_Template->display(Array, Array)
  #4 /craft/app/vendor/twig/twig/lib/Twig/Template.php(278): __TwigTemplate_XXXXX->doDisplay(Array, Array)
  #5 /craft/app/vendor/twig/twig/lib/Twig/Template.php(252): Twig_Template->displayWithErrorHandling(Array, Array)
  #6 /craft/app/vendor/twig/twig/lib/Twig/Template.php(263): Twig_Template->display(Array)
  #7 /craft/app/vendor/twig/twig/lib/Twig/Environment.php(292): Twig_Template->render(Array)
  #8 /craft/app/services/TemplatesService.php(158): Twig_Environment->render('_views/_section', Array)
  #9 /craft/app/controllers/BaseController.php(75): Craft\TemplatesService->render('_views/_section', Array)
  #10 /craft/app/controllers/TemplatesController.php(185): Craft\BaseController->renderTemplate('_views/_section', Array)
  #11 /craft/app/controllers/TemplatesController.php(27): Craft\TemplatesController->_render('_views/_section', Array)
  #12 [internal function]: Craft\TemplatesController->actionRender('_views/_section', Array)
  #13 /craft/app/framework/web/actions/CAction.php(108): ReflectionMethod->invokeArgs(Object(Craft\TemplatesController), Array)
  #14 /craft/app/framework/web/actions/CInlineAction.php(47): CAction->runWithParamsInternal(Object(Craft\TemplatesController), Object(ReflectionMethod), Array)
  #15 /craft/app/framework/web/CController.php(308): CInlineAction->runWithParams(Array)
  #16 /craft/app/framework/web/CController.php(286): CController->runAction(Object(CInlineAction))
  #17 /craft/app/framework/web/CController.php(265): CController->runActionWithFilters(Object(CInlineAction), NULL)
  #18 /craft/app/framework/web/CWebApplication.php(282): CController->run('render')
  #19 /craft/app/framework/web/CWebApplication.php(141): CWebApplication->runController('templates/rende...')
  #20 /craft/app/etc/web/WebApp.php(247): CWebApplication->processRequest()
  #21 /craft/app/framework/base/CApplication.php(180): Craft\WebApp->processRequest()
  #22 /craft/app/index.php(196): CApplication->run()
  #23 /index.php(17): require_once('/var/www/vhosts...')
  #24 {main}
  REQUEST_URI=/features
  ---
  2014/07/02 15:23:31 [error] [application] Unable to find the template “500”.
  2014/07/02 15:23:31 [error] [application] Unable to find the template “error”.
  
  ```
  
  **Tags:** templating, database, cache
  
  > **Comments:**
  > **username_1**: What version/build of Craft are you on?  
  > **username_0**: Craft Pro 2.0.2543  
  > **username_2**: @username_0 Glad you got this sorted. To keep your question in-line with the _Question-then-Answers_ format of this site, can you please remove the solution from your question, and post it as an answer? You’re allowed to answer your own questions, and accept it as the best answer.  
  > **username_0**: Done. Thanks username_2, didn’t realise I had those privileges yet.  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_0 on 2014-07-03. Score: 1 | Accepted answer
  
  The CP was also showing tasks getting stuck on ‘Deleting Stale Template Caches’ – having seen [this post](https://plus.google.com/+KevinOuellette/posts/NW9VvZCxpK1), I changed the task to ‘error’ in the db. The task then ran successfully, and template caching was no longer a problem.
  
  
  ---
  
  ### Answer by username_1 on 2014-07-02. Score: 3
  
  That error means that one database request comes into MySQL and grabs an exclusive handle on a table. For whatever reason, it doesn't release that handle in a reasonable amount of time, so another request comes in wanting access to the same table, but gives up waiting on the first one to finish.
  
  If you open up your craft/storage/runtime/logs files and find the error in there, it will come with a full stack trace where you can track down where the error is actually occurring. Better yet... redact any sensitive details and place it here so we can all learn from it. :)
  
  **Update**
  
  From the stack trace it looks like the timeout is occurring when Craft is in the process of running it's "DeleteStaleTemplateCache" task. It also looks like you're not on the latest release. You should update to the latest 2.1 build where we've made that task much less aggressive when doing it's database queries.
  
  > **Comments:**
  > **username_0**: do you think a likely cause would be too many/big caches? Deleting Stale Template Caches takes a long time – the database for the site is huge.  
  > **username_3**: I'm having the same issue here, got a very big template cache and this "Deleting stale template caches" never finishes.  
  > **username_4**: Same issue here. This is very annoying - anything we can do against this?  
  
  ---
  
  