Deadlock!

Tagged as ,
July 11th, 2008 | by mk |
private boolean insertRecordOnMultipleTables() {
      Connection conn = getConnection();
      conn.setAutoCommit(false);
      PreparedStatement ps = conn.prepareStatement("insert into person values ('Ada', 36)");
      ps.executeUpdate();
      int totalRecords = selectRecord();
      conn.commit();
      conn.setAutoCommit(true);
      ps.close();
      conn.close();
      return totalRecords > 0;
}

private int selectRecord() {
      int result = 0;
      Connection conn = getConnection();
      PreparedStatement ps = conn.prepareStatement("select count(1) from person");
      ResultSet rs = ps.executeQuery();
      if (rs.next()) {
            result = rs.getInt(1);
      }
      rs.close();
      ps.close();
      conn.close();
      return result;
}

See where the problem is?

Related Posts

  1. 2 Responses to “Deadlock!”

  2. By kingslee
    on Jul 29, 2008 | Reply

    database dead lock.

    int totalRecords = selectRecord();
    conn.commit();

    ==>

    conn.commit();
    int totalRecords = selectRecord();

    should ok.

    “insert” lock down all the table until “commit”, and “select” can not perform while table is locked.

  3. By mk
    on Jul 29, 2008 | Reply

    @kingslee : That’s correct, that’s Database deadlock.

Post a Comment