How do I deal with concurrent changes in a web application?

后端 未结 3 798
半阙折子戏
半阙折子戏 2021-01-05 07:40

Here are two potential workflows I would like to perform in a web application.

Variation 1

  • user sends request
  • server reads data
  • serve
3条回答
  •  灰色年华
    2021-01-05 07:47

    If you do not have transactions in mysql, you can use the update command to ensure that the data is not corrupted.

    UPDATE tableA  SET status=2  WHERE status = 1
    

    If status is one, then only one process well get the result that a record was updated. In the code below, returns -1 if the update was NOT executed (if there were no rows to update).

    PreparedStatement query;
    query = connection.prepareStatement(s);
    int rows = -1;
    try
    {
        rows = query.executeUpdate();
        query.close();
    }
    catch (Exception e)
    {
       e.printStackTrace();
    }
    return rows;
    

提交回复
热议问题