title: Mysql deadlock Unexpected
body: <p>I am experiencing a deadlock in my stored procedure and I cannot for the life of me figure out why that is</p>
  
  <p>Here is the part of my procedure where I think the deadlock is happening</p>
  
  <pre><code>-- if the taskId doesnt exist in the task execution table for a particular month year, taskId and companyId combination then add it in
  if NOT exists (select id from T_EXECUTIONS where usertask_id = userTaskId and org_id = orgId and month=currentMonth and year = currentYear)
  Then
       insert into T_EXECUTIONS(operations_performed, success_operations, failure_operations, month, year, org_id, usertask_id, created_date, updated_date)
              values(0,0,0,currentMonth, currentYear, orgId, userTaskId, now(), now());
  END IF;
  
  -- Update the task execution entry for this month, year, companyId and userTaskid
  if isSuccess Then
          update T_EXECUTIONS set operations_performed = operations_performed + operationsPerformed, success_operations = success_operations + operationsPerformed, updated_date=now() WHERE usertask_id=userTaskId and org_id = orgId and month=currentMonth and year = currentYear;
  else
          update T_EXECUTIONS set operations_performed = operations_performed + operationsPerformed, failure_operations = failure_operations + operationsPerformed, updated_date=now() WHERE usertask_id=userTaskId and org_id = orgId and month=currentMonth and year = currentYear;
  END IF; 
  </code></pre>
  
  <p>From the DB logs this is what I am seeing</p>
  
  <pre><code>LATEST DETECTED DEADLOCK
   ------------------------
   2015-05-12 19:35:04 2afa9066b700
   *** (1) TRANSACTION:
   TRANSACTION 23099124, ACTIVE 0 sec inserting
   mysql tables in use 1, locked 1
   LOCK WAIT 10 lock struct(s), heap size 1184, 246 row lock(s), undo log entries 3
   MySQL thread id 1947206, OS thread handle 0x2afa90b7f700, query id 223242109 10.20.0.197 beta_v1_master update
   insert into T_EXECUTIONS(operations_performed, success_operations, failure_operations, month, year, org_id, usertask_id, created_date, updated_date)
                                         values(0,0,0, NAME_CONST('currentMonth',5),  NAME_CONST('currentYear',2015),  NAME_CONST('org_id',353722),  NAME_CONST('userTaskId',75801), now(), now())
   *** (1) WAITING FOR THIS LOCK TO BE GRANTED:
   RECORD LOCKS space id 2468 page no 4 n bits 192 index `fk_execution_org_id` of table `beta_v1_db`.`T_EXECUTIONS` trx id 23099124 lock_mode X insert intention waiting
   Record lock, heap no 1 PHYSICAL RECORD: n_fields 1; compact format; info bits 0
    0: len 8; hex 73757072656d756d; asc supremum;;
  
   *** (2) TRANSACTION:
   TRANSACTION 23099125, ACTIVE 0 sec inserting
   mysql tables in use 1, locked 1
   10 lock struct(s), heap size 1184, 246 row lock(s), undo log entries 3
   MySQL thread id 1947212, OS thread handle 0x2afa9066b700, query id 223242110 10.20.0.197 beta_v1_master update
   insert into T_EXECUTIONS(operations_performed, success_operations, failure_operations, month, year, org_id, usertask_id, created_date, updated_date)
                                         values(0,0,0, NAME_CONST('currentMonth',5),  NAME_CONST('currentYear',2015),  NAME_CONST('org_id',353722),  NAME_CONST('userTaskId',75786), now(), now())
   *** (2) HOLDS THE LOCK(S):
   RECORD LOCKS space id 2468 page no 4 n bits 192 index `fk_execution_org_id` of table `beta_v1_db`.`T_EXECUTIONS` trx id 23099125 lock mode S
   Record lock, heap no 1 PHYSICAL RECORD: n_fields 1; compact format; info bits 0
    0: len 8; hex 73757072656d756d; asc supremum;;
  </code></pre>
  
  <p>I cant figure out why deadlock is occuring for this particular block of code.
  Should I perform this sort of task in a differnt way?</p>
  
  <p>Cheers
  Damien</p>