xp tracker service: fix deadlock acquiring connections
This commit is contained in:
@@ -62,9 +62,9 @@ public class XpTrackerService
|
||||
{
|
||||
try (Connection con = sql2o.open())
|
||||
{
|
||||
PlayerEntity playerEntity = findOrCreatePlayer(username);
|
||||
PlayerEntity playerEntity = findOrCreatePlayer(con, username);
|
||||
|
||||
XpEntity currentXp = findXpAtTime(username, Instant.now());
|
||||
XpEntity currentXp = findXpAtTime(con, username, Instant.now());
|
||||
if (currentXp != null)
|
||||
{
|
||||
XpData hiscoreData = XpMapper.INSTANCE.hiscoreResultToXpData(hiscoreResult);
|
||||
@@ -139,9 +139,7 @@ public class XpTrackerService
|
||||
}
|
||||
}
|
||||
|
||||
private synchronized PlayerEntity findOrCreatePlayer(String username)
|
||||
{
|
||||
try (Connection con = sql2o.open())
|
||||
private synchronized PlayerEntity findOrCreatePlayer(Connection con, String username)
|
||||
{
|
||||
PlayerEntity playerEntity = con.createQuery("select * from player where name = :name")
|
||||
.addParameter("name", username)
|
||||
@@ -165,11 +163,8 @@ public class XpTrackerService
|
||||
playerEntity.setTracked_since(now);
|
||||
return playerEntity;
|
||||
}
|
||||
}
|
||||
|
||||
public XpEntity findXpAtTime(String username, Instant time)
|
||||
{
|
||||
try (Connection con = sql2o.open())
|
||||
private XpEntity findXpAtTime(Connection con, String username, Instant time)
|
||||
{
|
||||
return con.createQuery("select * from xp join player on player.id=xp.player where player.name = :username and time <= :time order by time desc limit 1")
|
||||
.throwOnMappingFailure(false)
|
||||
@@ -177,5 +172,12 @@ public class XpTrackerService
|
||||
.addParameter("time", time)
|
||||
.executeAndFetchFirst(XpEntity.class);
|
||||
}
|
||||
|
||||
public XpEntity findXpAtTime(String username, Instant time)
|
||||
{
|
||||
try (Connection con = sql2o.open())
|
||||
{
|
||||
return findXpAtTime(con, username, time);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user