title: PHP MySQL get_lock
body: <p>In a script I'm trying to check whether the same script is already running using MySQL <code>GET_LOCK</code>. The problem is, when a script tries to get lock, which isn't free, it blocks forever regardless of the parameter I provide.</p>
  
  <pre><code>&lt;?php
  
  class Controller_Refresher extends Controller {
      public function action_run($key) {
          echo date('H:i:s'."\n", time());
          $this-&gt;die_if_running();
          $this-&gt;run();
          echo date('H:i:s'."\n", time());
      }
  
      private function die_if_running() {
          $result = DB::query(Database::SELECT, "SELECT IS_FREE_LOCK('refresher_running') AS free")-&gt;execute();
          if (! intval($result[0]['free'])) die('Running already');
          $result = DB::query(Database::SELECT, "SELECT GET_LOCK('refresher_running', 1)")-&gt;execute();
      }
  
      private function run() {
          echo "Starting\n";
          ob_flush();
          sleep(10);
  
          DB::query(Database::SELECT, "SELECT RELEASE_LOCK('refresher_running')")-&gt;execute();
      }
  }
  </code></pre>
  
  <p>When I run this in 2 tabs in browser, I get e.g.:</p>
  
  <pre><code>-tab 1-
  20:48:16
  Starting
  20:48:26
  -tab 2-
  20:48:27
  Starting
  20:48:37
  </code></pre>
  
  <p>While what I want to do is to make the second tab <code>die('Running already');</code>.</p>