【问题标题】:SELECT...FOR UPDATE using AnormSELECT...FOR UPDATE 使用 Anorm
【发布时间】:2016-08-15 15:58:48
【问题描述】:

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

代码是:

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

任何和所有的帮助将不胜感激。谢谢。

【问题讨论】:

  • 我不明白您打算如何使用“SQL SELECT...FOR UPDATE”实现更好的多线程...

标签: mysql scala anorm


【解决方案1】:

作为大多数 JDBC 基础库,Anorm 使用 PreparedStatement 来安全地与 DB 交互,因此您不应将这样的多语句字符串传递给它,而应将单个语句传递给每个 SQL 调用。

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

【讨论】:

    猜你喜欢
    • 2011-06-28
    • 2012-06-11
    • 2014-11-29
    • 2015-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-13
    • 1970-01-01
    相关资源
    最近更新 更多