title: How to avoid MySQL 'Deadlock found when trying to get lock; try restarting transaction'
body: <p>I have a innoDB table which records online users. It gets updated on every page refresh by a user to keep track of which pages they are on and their last access date to the site. I then have a cron that runs every 15 minutes to DELETE old records.</p>
  
  <p>I got a 'Deadlock found when trying to get lock; try restarting transaction' for about 5 minutes last night and it appears to be when running INSERTs into this table. Can someone suggest how to avoid this error?</p>
  
  <p>=== EDIT ===</p>
  
  <p>Here are the queries that are running:</p>
  
  <p><strong>First Visit to site:</strong></p>
  
  <pre><code>INSERT INTO onlineusers SET
  ip = 123.456.789.123,
  datetime = now(),
  userid = 321,
  page = '/thispage',
  area = 'thisarea',
  type = 3
  </code></pre>
  
  <p><strong>On each page refresh:</strong></p>
  
  <pre><code>UPDATE onlineusers SET
  ips = 123.456.789.123,
  datetime = now(),
  userid = 321,
  page = '/thispage',
  area = 'thisarea',
  type = 3
  WHERE id = 888
  </code></pre>
  
  <p><strong>Cron every 15 minutes:</strong></p>
  
  <pre><code>DELETE FROM onlineusers WHERE datetime &lt;= now() - INTERVAL 900 SECOND
  </code></pre>
  
  <p>It then does some counts to log some stats (ie: members online, visitors online).</p>