Deadlock!

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?

2 thoughts on “Deadlock!

  1. 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.

Leave a Reply

Your email address will not be published. Required fields are marked *