SELECT...使用 Anorm 进行更新

SELECT...FOR UPDATE using Anorm

我正在尝试编写一个 SQL SELECT...FOR UPDATE 在 Play 中使用 Anorm 以便我可以让多个线程与同一个数据库交互,但它引发了一个问题。

密码是:

db.withConnection { implicit connection: Connection =>
      SQL"""

          start transaction;

          select * from push_messages where vendorId=$vendorId for update;

          UPDATE push_messages set stageOne=$first, stageTwo=$second, stageThree=$third,
          stageFour=$fourth, stageFive=$fifth, highestStage=$highestStage, iOSTotal=$iOSTotal,
          androidTotal=$androidTotal, iOSRunningCount=$iOSRunningCount, androidRunningCount=$androidRunningCount,
          problem=$newProblem, iOSComplete=$iOSCompleted, androidComplete=$newAndroidComplete,
          totalStageThrees=$totalStageThrees, totalStageFours=$totalStageFours, expectedTotals=$expectedTotals,
          startTime=$startTime, date=$date, topics=$topics, androidFailures=$androidFailures, iOSFailures=$iOSFailures where vendorId=$vendorId;

          commit;

        """.execute
    }

但是,它似乎不喜欢在 select 语句中使用 .execute。有没有什么好的方法可以将其分解为 select...for update,这样我就可以使用 execute()executeUpdate

任何帮助都将不胜感激。谢谢。

作为大多数 JDBC 基础库,Anorm 使用 PreparedStatement 来安全地与数据库交互,所以你不应该将这样的多语句字符串传递给它,而应该只向每个 SQL呼叫.

此外,关于start transaction,你最好使用JDBC方式(例如使用Play DB DB.withTransaction { ... })。