Skip to content

Commit 9339079

Browse files
committed
move some connection code to utilize try-with-resource
1 parent 47c180b commit 9339079

File tree

1 file changed

+15
-36
lines changed

1 file changed

+15
-36
lines changed

storage/simple/src/main/java/me/hsgamer/topper/storage/simple/supplier/SqlStorageSupplier.java

Lines changed: 15 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,6 @@ protected boolean isSingleThread() {
3030

3131
protected abstract List<Object[]> toSaveValues(Object[] keys, Object[] values);
3232

33-
private Connection getConnection() throws SQLException {
34-
Connection connection = getClient().getConnection();
35-
connection.setAutoCommit(false);
36-
return connection;
37-
}
38-
39-
private void flushConnection(Connection connection) {
40-
try {
41-
connection.close();
42-
} catch (SQLException e) {
43-
logger.log(LogLevel.ERROR, "Failed to close connection", e);
44-
}
45-
}
46-
4733
private void lock() {
4834
if (isSingleThread()) {
4935
lock.lock();
@@ -62,9 +48,7 @@ public <K, V> DataStorage<K, V> getStorage(String name, ValueConverter<K> keyCon
6248
@Override
6349
public Map<K, V> load() {
6450
lock();
65-
Connection connection = null;
66-
try {
67-
connection = getConnection();
51+
try (Connection connection = getClient().getConnection()) {
6852
return StatementBuilder.create(connection)
6953
.setStatement("SELECT * FROM `" + name + "`;")
7054
.queryList(resultSet -> new AbstractMap.SimpleEntry<>(keyConverter.fromSqlResultSet(resultSet), valueConverter.fromSqlResultSet(resultSet)))
@@ -75,19 +59,14 @@ public Map<K, V> load() {
7559
logger.log(LogLevel.ERROR, "Failed to load top holder", e);
7660
return Collections.emptyMap();
7761
} finally {
78-
if (connection != null) {
79-
flushConnection(connection);
80-
}
8162
unlock();
8263
}
8364
}
8465

8566
@Override
8667
public Optional<V> load(K key) {
8768
lock();
88-
Connection connection = null;
89-
try {
90-
connection = getConnection();
69+
try (Connection connection = getClient().getConnection()) {
9170
String[] keyColumns = keyConverter.getSqlColumns();
9271
Object[] keyValues = keyConverter.toSqlValues(key);
9372

@@ -113,9 +92,6 @@ public Optional<V> load(K key) {
11392
logger.log(LogLevel.ERROR, "Failed to load top holder", e);
11493
return Optional.empty();
11594
} finally {
116-
if (connection != null) {
117-
flushConnection(connection);
118-
}
11995
unlock();
12096
}
12197
}
@@ -124,7 +100,8 @@ public Optional<V> load(K key) {
124100
public Optional<Modifier<K, V>> modify() {
125101
lock();
126102
try {
127-
Connection connection = getConnection();
103+
Connection connection = getClient().getConnection();
104+
connection.setAutoCommit(false);
128105
Modifier<K, V> modifier = new Modifier<K, V>() {
129106
@Override
130107
public void save(Map<K, V> map) throws SQLException {
@@ -173,14 +150,22 @@ public void remove(Collection<K> keys) throws SQLException {
173150
batchBuilder.execute();
174151
}
175152

153+
private void close() {
154+
try {
155+
connection.close();
156+
} catch (SQLException e) {
157+
logger.log(LogLevel.ERROR, "Failed to close connection", e);
158+
}
159+
}
160+
176161
@Override
177162
public void commit() {
178163
try {
179164
connection.commit();
180165
} catch (SQLException e) {
181166
logger.log(LogLevel.ERROR, "Failed to commit", e);
182167
} finally {
183-
flushConnection(connection);
168+
close();
184169
unlock();
185170
}
186171
}
@@ -192,7 +177,7 @@ public void rollback() {
192177
} catch (SQLException e) {
193178
logger.log(LogLevel.ERROR, "Failed to rollback", e);
194179
} finally {
195-
flushConnection(connection);
180+
close();
196181
unlock();
197182
}
198183
}
@@ -208,9 +193,7 @@ public void rollback() {
208193
@Override
209194
public void onRegister() {
210195
lock();
211-
Connection connection = null;
212-
try {
213-
connection = getConnection();
196+
try (Connection connection = getClient().getConnection()) {
214197
String[] keyColumns = keyConverter.getSqlColumns();
215198
String[] keyColumnDefinitions = keyConverter.getSqlColumnDefinitions();
216199
String[] valueColumns = valueConverter.getSqlColumns();
@@ -242,13 +225,9 @@ public void onRegister() {
242225
StatementBuilder.create(connection)
243226
.setStatement(statement.toString())
244227
.update();
245-
connection.commit();
246228
} catch (SQLException e) {
247229
logger.log(LogLevel.ERROR, "Failed to create table", e);
248230
} finally {
249-
if (connection != null) {
250-
flushConnection(connection);
251-
}
252231
unlock();
253232
}
254233
}

0 commit comments

Comments
 (0)