Skip to content

Commit e10abfa

Browse files
committed
improve(platform/velocity): initialize player/world inside transaction
1 parent c08809a commit e10abfa

2 files changed

Lines changed: 58 additions & 26 deletions

File tree

minecraft/core/src/main/java/net/okocraft/monitor/core/storage/PlayerStorage.java

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import net.okocraft.monitor.core.models.MonitorPlayer;
66
import org.jetbrains.annotations.NotNullByDefault;
77

8+
import java.sql.Connection;
89
import java.sql.SQLException;
910
import java.util.UUID;
1011

@@ -20,25 +21,40 @@ public PlayerStorage(Database database) {
2021
}
2122

2223
public MonitorPlayer initializePlayer(UUID uuid, String name) throws SQLException {
24+
MonitorPlayer player;
2325
try (var connection = this.database.getConnection()) {
24-
MonitorPlayer player = this.operators.players().getPlayerByUUID(connection, uuid);
25-
26-
if (player == null) {
27-
int playerId = this.operators.players().insertPlayer(connection, uuid, name);
28-
this.operators.playerNameHistory().insertHistory(connection, playerId, name);
29-
return new MonitorPlayer(playerId, uuid, name);
30-
}
31-
32-
if (player.name().equals(name)) {
33-
return player;
26+
try {
27+
connection.setAutoCommit(false);
28+
player = this.initializePlayer(connection, uuid, name);
29+
connection.commit();
30+
} catch (SQLException e) {
31+
connection.rollback();
32+
throw e;
33+
} finally {
34+
connection.setAutoCommit(true);
3435
}
36+
}
37+
return player;
38+
}
3539

36-
MonitorPlayer updatedPlayer = new MonitorPlayer(player.playerId(), uuid, name);
40+
private MonitorPlayer initializePlayer(Connection connection, UUID uuid, String name) throws SQLException {
41+
MonitorPlayer player = this.operators.players().getPlayerByUUID(connection, uuid);
3742

38-
this.operators.players().updatePlayer(connection, updatedPlayer);
39-
this.operators.playerNameHistory().insertHistory(connection, player.playerId(), name);
43+
if (player == null) {
44+
int playerId = this.operators.players().insertPlayer(connection, uuid, name);
45+
this.operators.playerNameHistory().insertHistory(connection, playerId, name);
46+
return new MonitorPlayer(playerId, uuid, name);
47+
}
4048

41-
return updatedPlayer;
49+
if (player.name().equals(name)) {
50+
return player;
4251
}
52+
53+
MonitorPlayer updatedPlayer = new MonitorPlayer(player.playerId(), uuid, name);
54+
55+
this.operators.players().updatePlayer(connection, updatedPlayer);
56+
this.operators.playerNameHistory().insertHistory(connection, player.playerId(), name);
57+
58+
return updatedPlayer;
4359
}
4460
}

minecraft/core/src/main/java/net/okocraft/monitor/core/storage/WorldStorage.java

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import net.okocraft.monitor.core.database.operator.Operators;
55
import net.okocraft.monitor.core.models.MonitorWorld;
66

7+
import java.sql.Connection;
78
import java.sql.SQLException;
89
import java.util.UUID;
910

@@ -18,23 +19,38 @@ public WorldStorage(Database database) {
1819
}
1920

2021
public MonitorWorld initializeWorld(int serverId, UUID uid, String name) throws SQLException {
22+
MonitorWorld world;
2123
try (var connection = this.database.getConnection()) {
22-
MonitorWorld world = this.operators.worlds().getWorldByUID(connection, serverId, uid);
23-
24-
if (world == null) {
25-
int worldId = this.operators.worlds().insertWorld(connection, serverId, uid, name);
26-
return new MonitorWorld(worldId, serverId, uid, name);
27-
}
28-
29-
if (world.name().equals(name)) {
30-
return world;
24+
try {
25+
connection.setAutoCommit(false);
26+
world = this.initializeWorld(connection, serverId, uid, name);
27+
connection.commit();
28+
} catch (SQLException e) {
29+
connection.rollback();
30+
throw e;
31+
} finally {
32+
connection.setAutoCommit(true);
3133
}
34+
}
35+
return world;
36+
}
3237

33-
MonitorWorld updatedWorld = new MonitorWorld(world.worldId(), serverId, uid, name);
38+
private MonitorWorld initializeWorld(Connection connection, int serverId, UUID uid, String name) throws SQLException {
39+
MonitorWorld world = this.operators.worlds().getWorldByUID(connection, serverId, uid);
3440

35-
this.operators.worlds().updateWorld(connection, updatedWorld);
41+
if (world == null) {
42+
int worldId = this.operators.worlds().insertWorld(connection, serverId, uid, name);
43+
return new MonitorWorld(worldId, serverId, uid, name);
44+
}
3645

37-
return updatedWorld;
46+
if (world.name().equals(name)) {
47+
return world;
3848
}
49+
50+
MonitorWorld updatedWorld = new MonitorWorld(world.worldId(), serverId, uid, name);
51+
52+
this.operators.worlds().updateWorld(connection, updatedWorld);
53+
54+
return updatedWorld;
3955
}
4056
}

0 commit comments

Comments
 (0)