2016-08-15 95 views
1

我正在尝试编写SQL SELECT ... FOR UPDATE在Play中使用Anorm,以便我可以让多个线程与同一个数据库进行交互,但是会引发问题。选择...更新使用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以便我可以使用​​或executeUpdate

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

+0

我不明白你是如何使用“SQL SELECT ... FOR UPDATE”实现更好的多线程... – cchantep

回答

1

由于大多数JDBC基本库,Anorm使用PreparedStatement来安全地与数据库进行交互,因此您不应该将这样的多语句字符串传递给每个SQL调用。

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