Skip to content

Commit a888e44

Browse files
committed
reuse db connection
1 parent f59d2da commit a888e44

File tree

5 files changed

+45
-56
lines changed

5 files changed

+45
-56
lines changed

src/main/java/pro/cloudnode/smp/smpcore/CitizenRequest.java

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import org.jetbrains.annotations.Nullable;
77
import pro.cloudnode.smp.smpcore.command.Command;
88

9-
import java.sql.Connection;
109
import java.sql.PreparedStatement;
1110
import java.sql.ResultSet;
1211
import java.sql.SQLException;
@@ -130,10 +129,10 @@ public CitizenRequest(final @NotNull ResultSet rs) throws SQLException {
130129

131130
public void save() {
132131
try (
133-
final @NotNull Connection conn = SMPCore.getInstance().db().getConnection();
134-
final @NotNull PreparedStatement stmt = conn.prepareStatement(
135-
"INSERT INTO `citizen_requests` (`member`, `nation`, `mode`, `created`, `expires`) VALUES (?,"
136-
+ " ?, ?, ?, ?)")
132+
final @NotNull PreparedStatement stmt = SMPCore.getInstance().conn.prepareStatement(
133+
"INSERT INTO `citizen_requests` (`member`, `nation`, `mode`, `created`, `expires`) VALUES (?,"
134+
+ " ?, ?, ?, ?)"
135+
)
137136
) {
138137
stmt.setString(1, uuid.toString());
139138
stmt.setString(2, nationID);
@@ -155,8 +154,7 @@ public void save() {
155154

156155
public void delete() {
157156
try (
158-
final @NotNull Connection conn = SMPCore.getInstance().db().getConnection();
159-
final @NotNull PreparedStatement stmt = conn.prepareStatement(
157+
final @NotNull PreparedStatement stmt = SMPCore.getInstance().conn.prepareStatement(
160158
"DELETE FROM `citizen_requests` WHERE `member` = ? AND `nation` = ?")
161159
) {
162160
stmt.setString(1, uuid.toString());
@@ -185,8 +183,7 @@ public void delete() {
185183
final @NotNull Nation nation
186184
) {
187185
try (
188-
final @NotNull Connection conn = SMPCore.getInstance().db().getConnection();
189-
final @NotNull PreparedStatement stmt = conn.prepareStatement(
186+
final @NotNull PreparedStatement stmt = SMPCore.getInstance().conn.prepareStatement(
190187
"SELECT * FROM `citizen_requests` WHERE `member` = ? AND `nation` = ? LIMIT 1")
191188
) {
192189
stmt.setString(1, member.uuid.toString());
@@ -214,8 +211,7 @@ public void delete() {
214211
*/
215212
public static @NotNull List<@NotNull CitizenRequest> get(final @NotNull Nation nation, final boolean mode) {
216213
try (
217-
final @NotNull Connection conn = SMPCore.getInstance().db().getConnection();
218-
final @NotNull PreparedStatement stmt = conn.prepareStatement(
214+
final @NotNull PreparedStatement stmt = SMPCore.getInstance().conn.prepareStatement(
219215
"SELECT * FROM `citizen_requests` WHERE `nation` = ? AND `mode` = ? ORDER BY `created`")
220216
) {
221217
stmt.setString(1, nation.id);
@@ -242,8 +238,7 @@ public void delete() {
242238
*/
243239
public static @NotNull List<@NotNull CitizenRequest> get(final @NotNull Member member, final boolean mode) {
244240
try (
245-
final @NotNull Connection conn = SMPCore.getInstance().db().getConnection();
246-
final @NotNull PreparedStatement stmt = conn.prepareStatement(
241+
final @NotNull PreparedStatement stmt = SMPCore.getInstance().conn.prepareStatement(
247242
"SELECT * FROM `citizen_requests` WHERE `member` = ? AND `mode` = ? ORDER BY `created`")
248243
) {
249244
stmt.setString(1, member.uuid.toString());
@@ -273,19 +268,19 @@ public void delete() {
273268
*/
274269
public static void delete(final @NotNull List<@NotNull CitizenRequest> requests) {
275270
try (
276-
final @NotNull Connection conn = SMPCore.getInstance().db().getConnection();
277-
final @NotNull PreparedStatement stmt = conn.prepareStatement(
271+
final @NotNull PreparedStatement stmt = SMPCore.getInstance().conn.prepareStatement(
278272
"DELETE FROM `citizen_requests` WHERE `member` = ? AND `nation` = ?")
279273
) {
280-
conn.setAutoCommit(false);
274+
SMPCore.getInstance().conn.setAutoCommit(false);
281275
for (final @NotNull CitizenRequest request : requests) {
282276
stmt.setString(1, request.uuid.toString());
283277
stmt.setString(2, request.nationID);
284278
stmt.addBatch();
285279
}
286280

287281
stmt.executeBatch();
288-
conn.commit();
282+
SMPCore.getInstance().conn.commit();
283+
SMPCore.getInstance().conn.setAutoCommit(true);
289284
}
290285
catch (final @NotNull SQLException e) {
291286
SMPCore.getInstance().getLogger().log(

src/main/java/pro/cloudnode/smp/smpcore/Member.java

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import org.jetbrains.annotations.NotNull;
66
import org.jetbrains.annotations.Nullable;
77

8-
import java.sql.Connection;
98
import java.sql.PreparedStatement;
109
import java.sql.ResultSet;
1110
import java.sql.SQLException;
@@ -69,8 +68,7 @@ public boolean isAlt() {
6968
public @NotNull Set<@NotNull Member> getAlts() {
7069
final @NotNull Set<@NotNull Member> alts = new HashSet<>();
7170
try (
72-
final @NotNull Connection conn = SMPCore.getInstance().db()
73-
.getConnection(); final @NotNull PreparedStatement stmt = conn.prepareStatement("SELECT * FROM `members` WHERE `alt_owner` = ?")
71+
final @NotNull PreparedStatement stmt = SMPCore.getInstance().conn.prepareStatement("SELECT * FROM `members` WHERE `alt_owner` = ?")
7472
) {
7573
stmt.setString(1, uuid.toString());
7674
final @NotNull ResultSet rs = stmt.executeQuery();
@@ -89,8 +87,7 @@ public void unban() {
8987

9088
public void save() {
9189
try (
92-
final @NotNull Connection conn = SMPCore.getInstance().db()
93-
.getConnection(); final @NotNull PreparedStatement stmt = conn.prepareStatement("INSERT OR REPLACE INTO `members` (`uuid`, `nation`, `staff`, `alt_owner`, `added`) VALUES (?, ?, ?, ?, ?)")
90+
final @NotNull PreparedStatement stmt = SMPCore.getInstance().conn.prepareStatement("INSERT OR REPLACE INTO `members` (`uuid`, `nation`, `staff`, `alt_owner`, `added`) VALUES (?, ?, ?, ?, ?)")
9491
) {
9592
stmt.setString(1, uuid.toString());
9693
stmt.setString(2, nationID == null ? null : nationID);
@@ -109,8 +106,7 @@ public void save() {
109106
*/
110107
private void remove() {
111108
try (
112-
final @NotNull Connection conn = SMPCore.getInstance().db()
113-
.getConnection(); final @NotNull PreparedStatement stmt = conn.prepareStatement("DELETE FROM `members` WHERE `uuid` = ?")
109+
final @NotNull PreparedStatement stmt = SMPCore.getInstance().conn.prepareStatement("DELETE FROM `members` WHERE `uuid` = ?")
114110
) {
115111
stmt.setString(1, uuid.toString());
116112
stmt.executeUpdate();
@@ -157,8 +153,7 @@ public boolean delete() {
157153

158154
public static @NotNull Optional<@NotNull Member> get(final @NotNull UUID uuid) {
159155
try (
160-
final @NotNull Connection conn = SMPCore.getInstance().db()
161-
.getConnection(); final @NotNull PreparedStatement stmt = conn.prepareStatement("SELECT * FROM `members` WHERE `uuid` = ? LIMIT 1")
156+
final @NotNull PreparedStatement stmt = SMPCore.getInstance().conn.prepareStatement("SELECT * FROM `members` WHERE `uuid` = ? LIMIT 1")
162157
) {
163158
stmt.setString(1, uuid.toString());
164159
final @NotNull ResultSet rs = stmt.executeQuery();
@@ -174,8 +169,7 @@ public boolean delete() {
174169
public static @NotNull Set<@NotNull Member> get() {
175170
final @NotNull Set<@NotNull Member> members = new HashSet<>();
176171
try (
177-
final @NotNull Connection conn = SMPCore.getInstance().db()
178-
.getConnection(); final @NotNull PreparedStatement stmt = conn.prepareStatement("SELECT * FROM `members`")
172+
final @NotNull PreparedStatement stmt = SMPCore.getInstance().conn.prepareStatement("SELECT * FROM `members`")
179173
) {
180174
final @NotNull ResultSet rs = stmt.executeQuery();
181175
while (rs.next()) members.add(new Member(rs));
@@ -190,8 +184,7 @@ public boolean delete() {
190184
final int offset = (page - 1) * limit;
191185
final @NotNull Set<@NotNull Member> members = new HashSet<>();
192186
try (
193-
final @NotNull Connection conn = SMPCore.getInstance().db()
194-
.getConnection(); final @NotNull PreparedStatement stmt = conn.prepareStatement("SELECT * FROM `members` LIMIT ? OFFSET ?")
187+
final @NotNull PreparedStatement stmt = SMPCore.getInstance().conn.prepareStatement("SELECT * FROM `members` LIMIT ? OFFSET ?")
195188
) {
196189
stmt.setInt(1, limit);
197190
stmt.setInt(2, offset);
@@ -207,8 +200,7 @@ public boolean delete() {
207200
public static @NotNull Set<@NotNull Member> get(final @NotNull Nation nation) {
208201
final @NotNull Set<@NotNull Member> members = new HashSet<>();
209202
try (
210-
final @NotNull Connection conn = SMPCore.getInstance().db()
211-
.getConnection(); final @NotNull PreparedStatement stmt = conn.prepareStatement("SELECT * FROM `members` WHERE `nation` = ?")
203+
final @NotNull PreparedStatement stmt = SMPCore.getInstance().conn.prepareStatement("SELECT * FROM `members` WHERE `nation` = ?")
212204
) {
213205
stmt.setString(1, nation.id);
214206
final @NotNull ResultSet rs = stmt.executeQuery();
@@ -222,8 +214,7 @@ public boolean delete() {
222214

223215
public static int count() {
224216
try (
225-
final @NotNull Connection conn = SMPCore.getInstance().db()
226-
.getConnection(); final @NotNull PreparedStatement stmt = conn.prepareStatement("SELECT COUNT(*) as `n` FROM `members`")
217+
final @NotNull PreparedStatement stmt = SMPCore.getInstance().conn.prepareStatement("SELECT COUNT(*) as `n` FROM `members`")
227218
) {
228219
final @NotNull ResultSet rs = stmt.executeQuery();
229220
rs.next();

src/main/java/pro/cloudnode/smp/smpcore/Nation.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,7 @@ public Nation(final @NotNull ResultSet rs) throws SQLException {
187187

188188
public void save() {
189189
try (
190-
final @NotNull Connection conn = SMPCore.getInstance().db()
191-
.getConnection(); final @NotNull PreparedStatement stmt = conn.prepareStatement("INSERT OR REPLACE INTO `nations` (`id`, `name`, `short_name`, `color`, `leader`, `vice`, `founded`, `founded_ticks`, `bank`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)")
190+
final @NotNull PreparedStatement stmt = SMPCore.getInstance().conn.prepareStatement("INSERT OR REPLACE INTO `nations` (`id`, `name`, `short_name`, `color`, `leader`, `vice`, `founded`, `founded_ticks`, `bank`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)")
192191
) {
193192
stmt.setString(1, id);
194193
stmt.setString(2, name);
@@ -212,8 +211,7 @@ public void save() {
212211

213212
public static @NotNull Optional<@NotNull Nation> get(final @NotNull String id) {
214213
try (
215-
final @NotNull Connection conn = SMPCore.getInstance().db()
216-
.getConnection(); final @NotNull PreparedStatement stmt = conn.prepareStatement("SELECT * FROM `nations` WHERE `id` = ? LIMIT 1")
214+
final @NotNull PreparedStatement stmt = SMPCore.getInstance().conn.prepareStatement("SELECT * FROM `nations` WHERE `id` = ? LIMIT 1")
217215
) {
218216
stmt.setString(1, id);
219217
final @NotNull ResultSet rs = stmt.executeQuery();
@@ -228,8 +226,7 @@ public void save() {
228226

229227
public static @NotNull Set<@NotNull Nation> get() {
230228
try (
231-
final @NotNull Connection conn = SMPCore.getInstance().db()
232-
.getConnection(); final @NotNull PreparedStatement stmt = conn.prepareStatement("SELECT * FROM `nations`")
229+
final @NotNull PreparedStatement stmt = SMPCore.getInstance().conn.prepareStatement("SELECT * FROM `nations`")
233230
) {
234231
final @NotNull ResultSet rs = stmt.executeQuery();
235232
final @NotNull Set<@NotNull Nation> nations = new HashSet<>();

src/main/java/pro/cloudnode/smp/smpcore/SMPCore.java

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,8 @@ public final class SMPCore extends JavaPlugin {
4646
}
4747

4848
public final @NotNull HikariConfig hikariConfig = new HikariConfig();
49-
private HikariDataSource dbSource;
50-
51-
public @NotNull HikariDataSource db() {
52-
return dbSource;
53-
}
49+
private HikariDataSource db;
50+
Connection conn;
5451

5552
private @Nullable Configuration config;
5653
private @Nullable Messages messages;
@@ -99,7 +96,13 @@ public void onEnable() {
9996

10097
@Override
10198
public void onDisable() {
102-
db().close();
99+
try {
100+
conn.close();
101+
}
102+
catch (SQLException e) {
103+
getLogger().log(Level.SEVERE, "failed to close db connection", e);
104+
}
105+
db.close();
103106
if (rest != null) rest.javalin.stop();
104107
}
105108

@@ -130,7 +133,14 @@ private void setupDatabase() {
130133
hikariConfig.addDataSourceProperty("elideSetAutoCommits", "true");
131134
hikariConfig.addDataSourceProperty("maintainTimeStats", "true");
132135

133-
dbSource = new HikariDataSource(hikariConfig);
136+
db = new HikariDataSource(hikariConfig);
137+
try {
138+
conn = db.getConnection();
139+
}
140+
catch (final SQLException e) {
141+
getLogger().log(Level.SEVERE, "could not get db connection", e);
142+
disable();
143+
}
134144
}
135145

136146
private void initDatabase() {
@@ -148,7 +158,7 @@ private void initDatabase() {
148158
final String query = q.stripTrailing().stripIndent().replaceAll("^\\s+(?:--.+)*", "");
149159
if (query.isBlank()) continue;
150160
try (
151-
final Connection conn = db().getConnection();
161+
final Connection conn = db.getConnection();
152162
final PreparedStatement stmt = conn.prepareStatement(query)
153163
) {
154164
stmt.executeUpdate();

src/main/java/pro/cloudnode/smp/smpcore/Token.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@ public Token(final @NotNull ResultSet rs) throws SQLException {
3838

3939
public void save() {
4040
try (
41-
final @NotNull Connection conn = SMPCore.getInstance().db()
42-
.getConnection(); final @NotNull PreparedStatement stmt = conn.prepareStatement("INSERT OR REPLACE INTO `tokens` (`token`, `member`, `created`, `last_used`) VALUES (?, ?, ?, ?)")
41+
final @NotNull PreparedStatement stmt = SMPCore.getInstance().conn.prepareStatement("INSERT OR REPLACE INTO `tokens` (`token`, `member`, `created`, `last_used`) VALUES (?, ?, ?, ?)")
4342
) {
4443
stmt.setString(1, token.toString());
4544
stmt.setString(2, memberUUID.toString());
@@ -54,8 +53,7 @@ public void save() {
5453

5554
public void delete() {
5655
try (
57-
final @NotNull Connection conn = SMPCore.getInstance().db()
58-
.getConnection(); final @NotNull PreparedStatement stmt = conn.prepareStatement("DELETE FROM `tokens` WHERE `token` = ?")
56+
final @NotNull PreparedStatement stmt = SMPCore.getInstance().conn.prepareStatement("DELETE FROM `tokens` WHERE `token` = ?")
5957
) {
6058
stmt.setString(1, token.toString());
6159
stmt.executeUpdate();
@@ -67,8 +65,7 @@ public void delete() {
6765

6866
public static @NotNull Optional<@NotNull Token> get(final @NotNull UUID token) throws SQLException {
6967
try (
70-
final @NotNull Connection conn = SMPCore.getInstance().db()
71-
.getConnection(); final @NotNull PreparedStatement stmt = conn.prepareStatement("SELECT * FROM `tokens` WHERE `token` = ? LIMIT 1")
68+
final @NotNull PreparedStatement stmt = SMPCore.getInstance().conn.prepareStatement("SELECT * FROM `tokens` WHERE `token` = ? LIMIT 1")
7269
) {
7370
stmt.setString(1, token.toString());
7471
final @NotNull ResultSet rs = stmt.executeQuery();
@@ -80,8 +77,7 @@ public void delete() {
8077
public static @NotNull Set<@NotNull Token> get(final @NotNull Member member) {
8178
final @NotNull Set<@NotNull Token> tokens = new HashSet<>();
8279
try (
83-
final @NotNull Connection conn = SMPCore.getInstance().db()
84-
.getConnection(); final @NotNull PreparedStatement stmt = conn.prepareStatement("SELECT * FROM `tokens` WHERE `member` = ?")
80+
final @NotNull PreparedStatement stmt = SMPCore.getInstance().conn.prepareStatement("SELECT * FROM `tokens` WHERE `member` = ?")
8581
) {
8682
stmt.setString(1, member.uuid.toString());
8783
final @NotNull ResultSet rs = stmt.executeQuery();

0 commit comments

Comments
 (0)