title: Lock wait timeout exceeded; try restarting transaction Java with spring and Mysql
body: <p>In my application i am using spring with plain jdbc.
  and there are multiple thread that do various database operation.like checking if value does not exist in table then insert it. Problem is i am getting exception >> <strong>Lock wait timeout exceeded; try restarting transaction</strong> I have check in mysql SHOW ENGINE INNODB STATUS\G; and it gives desc    </p>
  
  <pre><code>------------
  TRANSACTIONS
  ------------
  Trx id counter D467
  Purge done for trx's n:o &lt; D459 undo n:o &lt; 0
  History list length 1050
  LIST OF TRANSACTIONS FOR EACH SESSION:
  ---TRANSACTION 0, not started
  MySQL thread id 64, OS thread handle 0xa6a0ab40, query id 9370 localhost root
  SHOW ENGINE INNODB STATUS
  ---TRANSACTION D466, ACTIVE 22 sec
  5 lock struct(s), heap size 320, 2 row lock(s), undo log entries 2
  MySQL thread id 74, OS thread handle 0xa6c9eb40, query id 9369 localhost 127.0.0.1 root
  Trx read view will not see trx with id &gt;= D467, sees &lt; D457
  ---TRANSACTION D464, ACTIVE 24 sec
  5 lock struct(s), heap size 320, 2 row lock(s), undo log entries 2
  MySQL thread id 70, OS thread handle 0xa6aceb40, query id 9354 localhost 127.0.0.1 root
  Trx read view will not see trx with id &gt;= D465, sees &lt; D457
  ---TRANSACTION D462, ACTIVE 28 sec
  2 lock struct(s), heap size 320, 1 row lock(s)
  MySQL thread id 73, OS thread handle 0xa6c3cb40, query id 9347 localhost 127.0.0.1 root
  Trx read view will not see trx with id &gt;= D463, sees &lt; D457
  ---TRANSACTION D45A, ACTIVE 87 sec inserting
  mysql tables in use 1, locked 1
  LOCK WAIT 34 lock struct(s), heap size 2496, 81 row lock(s), undo log entries 151
  MySQL thread id 72, OS thread handle 0xa6a3bb40, query id 9054 localhost 127.0.0.1 root update
  Insert into TABLE_1 ( ID,NAME,STATUS)values (44545,'temp','disable')
  Trx read view will not see trx with id &gt;= D45B, sees &lt; D450
  ------- TRX HAS BEEN WAITING 26 SEC FOR THIS LOCK TO BE GRANTED:
  RECORD LOCKS space id 0 page no 159967 n bits 312 index `PRIMARY` of table `test2`.`TABLE_1` trx id D45A lock mode S locks rec but not gap waiting
  Record lock, heap no 67 PHYSICAL RECORD: n_fields 12; compact format; info bits 0
   0: len 4; hex 80016687; asc   f ;;
   1: len 6; hex 00000000d457; asc      W;;
   2: len 7; hex 2f0001dda310ee; asc /      ;;
   3: len 10; hex 4d6f68642e20417a697a; asc Mohd. Aziz;;
   4: SQL NULL;
   5: SQL NULL;
   6: len 1; hex 4d; asc M;;
   7: SQL NULL;
   8: len 1; hex 30; asc 0;;
   9: len 10; hex 496e646976696475616c; asc Individual;;
   10: len 6; hex 414354495645; asc ACTIVE;;
   11: len 4; hex 53a90e01; asc S   ;;
  
  ------------------
  ---TRANSACTION D457, ACTIVE 130 sec
  77 lock struct(s), heap size 5504, 141 row lock(s), undo log entries 287
  MySQL thread id 71, OS thread handle 0xa6affb40, query id 9001 localhost 127.0.0.1 root
  Trx read view will not see trx with id &gt;= D458, sees &lt; D42E
  --------
  FILE I/O
  --------
  I/O thread 0 state: waiting for i/o request (insert buffer thread)
  I/O thread 1 state: waiting for i/o request (log thread)
  I/O thread 2 state: waiting for i/o request (read thread)
  I/O thread 3 state: waiting for i/o request (read thread)
  I/O thread 4 state: waiting for i/o request (read thread)
  I/O thread 5 state: waiting for i/o request (read thread)
  I/O thread 6 state: waiting for i/o request (write thread)
  I/O thread 7 state: waiting for i/o request (write thread)
  I/O thread 8 state: waiting for i/o request (write thread)
  I/O thread 9 state: waiting for i/o request (write thread)
  Pending normal aio reads: 0 [0, 0, 0, 0] , aio writes: 0 [0, 0, 0, 0] ,
   ibuf aio reads: 0, log i/o's: 0, sync i/o's: 0
  Pending flushes (fsync) log: 0; buffer pool: 0
  2386 OS file reads, 1108 OS file writes, 155 OS fsyncs
  0.00 reads/s, 0 avg bytes/read, 0.00 writes/s, 0.00 fsyncs/s
  -------------------------------------
  INSERT BUFFER AND ADAPTIVE HASH INDEX
  -------------------------------------
  Ibuf: size 1, free list len 884, seg size 886, 12 merges
  merged operations:
   insert 102, delete mark 6, delete 0
  discarded operations:
   insert 0, delete mark 0, delete 0
  Hash table size 553253, node heap has 18 buffer(s)
  0.00 hash searches/s, 0.00 non-hash searches/s
  ---
  LOG
  </code></pre>
  
  <p>I just understand from my research that mysql INNODB uses row level lock and for delete it aquires index lock (exclusive) and gap lock as shared. and for some reason it wont release it. Then for some other insert in the same table it requires that lock.. and i get this excpetion. Can anyone suggest me how to resolve this issue?
      Other required details >></p>
  
  <pre><code>datasource.xml
  
  &lt;bean id="transactionManager"
          class="org.springframework.jdbc.datasource.DataSourceTransactionManager"&gt;
          &lt;property name="dataSource" ref="dataSource" /&gt;
      &lt;/bean&gt;
  
      &lt;bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" &gt;
        &lt;property name="url" value="jdbc:mysql://localhost:3306/test2" /&gt;
        &lt;property name="driverClassName" value="com.mysql.jdbc.Driver" /&gt;
        &lt;property name="username" value="root" /&gt;
        &lt;property name="password" value="root" /&gt;
        &lt;property name="removeAbandoned" value="true" /&gt;
        &lt;property name="initialSize" value="20" /&gt;
        &lt;property name="maxActive" value="30" /&gt;
      &lt;/bean&gt;
  
      &lt;!-- Enable Annotation based Declarative Transaction Management --&gt;
      &lt;tx:annotation-driven proxy-target-class="true"
          transaction-manager="transactionManager" /&gt;
  </code></pre>
  
  <p>This is how i used plain jdbc with spring transaction >></p>
  
  <pre><code>@Repository
  @Transactional(rollbackFor=Exception.class)
  public class StudentDaoImpl implements StudentDao {
  
  private static final Logger log = Logger
          .getLogger(StudentDaoImpl.class);
  
  @Autowired
  private DataSource dataSource;
  
  PreparedStatement stmt = null;
          ResultSet rs = null;
          int count = 0;
          String sql = "Select count(*) from SOMETABLE where ID= ?";
          Connection conn = null;
          try {
              conn = DataSourceUtils.getConnection(dataSource);
              stmt = conn.prepareStatement(sql);
              stmt.setInt(1, id);
              rs = stmt.executeQuery();
  
              while (rs.next()) {
                  count = rs.getInt(1);
              }
  
          } catch (SQLException e) {
              e.printStackTrace();
              log.error(e);
              throw new Exception(e);
          } finally {
              close(stmt, rs);
              DataSourceUtils.releaseConnection(conn, dataSource);
          }
          return count;
  
  } 
  </code></pre>