Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring MongoDB不一致持久性问题_Java_Spring_Mongodb - Fatal编程技术网

Java Spring MongoDB不一致持久性问题

Java Spring MongoDB不一致持久性问题,java,spring,mongodb,Java,Spring,Mongodb,我正在使用Spring和MongoDB为游戏制作一个web服务。其中一个特点是,每天,玩家都可以投票选择杀死另一个玩家。当天结束时,计票,得票最多的球员被杀。以下是杀死投票最多的玩家的代码(在一个名为endDay()的较大方法中): 下面是GetPlayerByd(): 和doKill(): 和updatePlayer(): 正如你所看到的,当我在杀死玩家后询问玩家的身份时(在endDay()),他似乎已经死了,这就是我们想要的 现在我请求/player/id,其中id是一个播放器的id。这将返

我正在使用Spring和MongoDB为游戏制作一个web服务。其中一个特点是,每天,玩家都可以投票选择杀死另一个玩家。当天结束时,计票,得票最多的球员被杀。以下是杀死投票最多的玩家的代码(在一个名为
endDay()
的较大方法中):

下面是GetPlayerByd():

和doKill():

和updatePlayer():

正如你所看到的,当我在杀死玩家后询问玩家的身份时(在
endDay()
),他似乎已经死了,这就是我们想要的

现在我请求
/player/id
,其中
id
是一个播放器的id。这将返回结果播放器对象的JSON编码(使用与上面相同的
getPlayerByID()
)。如果我用死者的身份证提出这个请求,他会活着回来

更复杂的是,其他更新会在请求之间顺利完成。例如,某些玩家可以在没有投票的情况下杀死其他玩家。这次杀戮使用了完全相同的
Player
getPlayerByID()
doKill()
,和
updatePlayer()
,受害者的死亡状态被持续


我目前正在本地主机上的Tomcat v7.0服务器上运行此功能。

已修复!后来我才发现,我在反复浏览并更新一份过时的玩家名单。所以被杀的玩家意外复活了


这个故事的寓意:确保你的域对象总是最新的

如果您愿意,也可以直接查看源代码:。
//mostVotes is the player's id in the database.
//Player is just a transient domain object
//playerDAO is a custom DAO that interfaces with MongoDB (see below)
Player accused = playerDAO.getPlayerByID(mostVotes);
logger.info(accused.getIsDead()); //This is false
doKill(accused);
logger.info(playerDAO.getPlayerByID(mostVotes).getIsDead()); //This is true
//mongoTemplate is a MongoOperations object
public Player getPlayerByID(String id) throws NoPlayerFoundException {
    Player p = mongoTemplate.findById(id, Player.class);
    if (p == null) {
        throw new NoPlayerFoundException(id);
    }
    return p;
}
private void doKill(Player p) {
    p.setIsDead(true);
    playerDAO.updatePlayer(p);
}
public void updatePlayer(Player p) {
    mongoTemplate.save(p);  
}