我玩了!使用Postgres Addon在Heroku上托管的Framework 2.3项目。 它处理来自移动应用程序的请求(发布消息)。
由于不同的原因,我在数据库中有重复(两次)行(消息):
我还尝试在处理请求之前在memcache中编写消息足迹(在表单验证之后)。但有时我仍然收到两次消息。
我找到的解决方案是:
由于我无法更新移动应用程序,因此我会编写一份定期检查重复项的脚本。
还有其他想法吗? 处理此类并发请求的最佳做法是什么?
附件:我的伪代码
public static Result submit() {
User user = MySecured.getCurrentUser(ctx());
final Form<Message> filledForm = form(Message.class).bindFromRequest();
.... Some database pre-verification
if (filledForm.hasErrors()) {
ObjectNode error = Json.newObject();
error.put("error", filledForm.errorsAsJson());
return ok(error);
} else {
if(Cache.get(KEY_LOCK_FLASH_WRITING+filledForm.data().get("mail"))!=null){
return internalServerError();
}
//Verify this flash hasnt already been handled (requests can come twice from client)
Message sameMessage = Message.findSame(filledForm.get().mail, filledForm.get().message);
if(sameMessage!=null){
Logger.info("[Submit] message already exists" + sameMessage.id);
ObjectNode jsonResult = Json.newObject();
.... Processing a result ... no matter this does not happen.
return ok(jsonResult);
}
final Message flash = filledForm.get();
Cache.set(KEY_LOCK_FLASH_WRITING+flash.mail, "");
... some fields initializations like flash.author = new Author();
... Then some promises
return ok();
}
}