Skip to content

Commit 47c180b

Browse files
committed
use lock when the database is single-threaded only
1 parent 3440263 commit 47c180b

File tree

3 files changed

+44
-66
lines changed

3 files changed

+44
-66
lines changed

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

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@
33
import me.hsgamer.hscore.database.Setting;
44
import me.hsgamer.hscore.database.client.sql.java.JavaSqlClient;
55
import me.hsgamer.hscore.database.driver.mysql.MySqlDriver;
6-
import me.hsgamer.hscore.logger.common.LogLevel;
76

8-
import java.sql.Connection;
9-
import java.sql.SQLException;
107
import java.util.Collections;
118
import java.util.List;
129
import java.util.function.Consumer;
@@ -21,19 +18,8 @@ public MySqlStorageSupplier(Consumer<Setting> databaseSettingConsumer) {
2118
}
2219

2320
@Override
24-
protected Connection getConnection() throws SQLException {
25-
Connection connection = client.getConnection();
26-
connection.setAutoCommit(false);
27-
return connection;
28-
}
29-
30-
@Override
31-
protected void flushConnection(Connection connection) {
32-
try {
33-
connection.close();
34-
} catch (SQLException e) {
35-
logger.log(LogLevel.ERROR, "Failed to close connection", e);
36-
}
21+
public JavaSqlClient getClient() {
22+
return client;
3723
}
3824

3925
@Override

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

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package me.hsgamer.topper.storage.simple.supplier;
22

33
import me.hsgamer.hscore.database.client.sql.BatchBuilder;
4+
import me.hsgamer.hscore.database.client.sql.SqlClient;
45
import me.hsgamer.hscore.database.client.sql.StatementBuilder;
56
import me.hsgamer.hscore.logger.common.LogLevel;
67
import me.hsgamer.hscore.logger.common.Logger;
@@ -19,23 +20,48 @@ public abstract class SqlStorageSupplier implements DataStorageSupplier {
1920
protected final Logger logger = LoggerProvider.getLogger(getClass());
2021
private final Lock lock = new ReentrantLock();
2122

22-
protected abstract Connection getConnection() throws SQLException;
23+
protected abstract SqlClient<?> getClient();
2324

24-
protected abstract void flushConnection(Connection connection);
25-
26-
protected boolean shouldLockWhenModify() {
25+
protected boolean isSingleThread() {
2726
return false;
2827
}
2928

3029
protected abstract List<String> toSaveStatement(String name, String[] keyColumns, String[] valueColumns);
3130

3231
protected abstract List<Object[]> toSaveValues(Object[] keys, Object[] values);
3332

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+
47+
private void lock() {
48+
if (isSingleThread()) {
49+
lock.lock();
50+
}
51+
}
52+
53+
private void unlock() {
54+
if (isSingleThread()) {
55+
lock.unlock();
56+
}
57+
}
58+
3459
@Override
3560
public <K, V> DataStorage<K, V> getStorage(String name, ValueConverter<K> keyConverter, ValueConverter<V> valueConverter) {
3661
return new DataStorage<K, V>() {
3762
@Override
3863
public Map<K, V> load() {
64+
lock();
3965
Connection connection = null;
4066
try {
4167
connection = getConnection();
@@ -52,11 +78,13 @@ public Map<K, V> load() {
5278
if (connection != null) {
5379
flushConnection(connection);
5480
}
81+
unlock();
5582
}
5683
}
5784

5885
@Override
5986
public Optional<V> load(K key) {
87+
lock();
6088
Connection connection = null;
6189
try {
6290
connection = getConnection();
@@ -88,14 +116,13 @@ public Optional<V> load(K key) {
88116
if (connection != null) {
89117
flushConnection(connection);
90118
}
119+
unlock();
91120
}
92121
}
93122

94123
@Override
95124
public Optional<Modifier<K, V>> modify() {
96-
if (shouldLockWhenModify()) {
97-
lock.lock();
98-
}
125+
lock();
99126
try {
100127
Connection connection = getConnection();
101128
Modifier<K, V> modifier = new Modifier<K, V>() {
@@ -154,9 +181,7 @@ public void commit() {
154181
logger.log(LogLevel.ERROR, "Failed to commit", e);
155182
} finally {
156183
flushConnection(connection);
157-
if (shouldLockWhenModify()) {
158-
lock.unlock();
159-
}
184+
unlock();
160185
}
161186
}
162187

@@ -168,21 +193,21 @@ public void rollback() {
168193
logger.log(LogLevel.ERROR, "Failed to rollback", e);
169194
} finally {
170195
flushConnection(connection);
171-
if (shouldLockWhenModify()) {
172-
lock.unlock();
173-
}
196+
unlock();
174197
}
175198
}
176199
};
177200
return Optional.of(modifier);
178201
} catch (SQLException e) {
179202
logger.log(LogLevel.ERROR, "Failed to get connection", e);
203+
unlock();
180204
return Optional.empty();
181205
}
182206
}
183207

184208
@Override
185209
public void onRegister() {
210+
lock();
186211
Connection connection = null;
187212
try {
188213
connection = getConnection();
@@ -224,6 +249,7 @@ public void onRegister() {
224249
if (connection != null) {
225250
flushConnection(connection);
226251
}
252+
unlock();
227253
}
228254
}
229255
};

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

Lines changed: 3 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,14 @@
33
import me.hsgamer.hscore.database.Setting;
44
import me.hsgamer.hscore.database.client.sql.java.JavaSqlClient;
55
import me.hsgamer.hscore.database.driver.sqlite.SqliteFileDriver;
6-
import me.hsgamer.hscore.logger.common.LogLevel;
76

87
import java.io.File;
9-
import java.sql.Connection;
108
import java.util.Arrays;
119
import java.util.List;
12-
import java.util.concurrent.atomic.AtomicReference;
1310
import java.util.function.Consumer;
1411

1512
public class SqliteStorageSupplier extends SqlStorageSupplier {
1613
private final JavaSqlClient client;
17-
private final AtomicReference<Connection> connectionReference = new AtomicReference<>();
1814

1915
public SqliteStorageSupplier(Consumer<Setting> databaseSettingConsumer, File baseHolder) {
2016
Setting setting = Setting.create(new SqliteFileDriver(baseHolder));
@@ -23,30 +19,12 @@ public SqliteStorageSupplier(Consumer<Setting> databaseSettingConsumer, File bas
2319
}
2420

2521
@Override
26-
protected Connection getConnection() {
27-
return connectionReference.updateAndGet(connection -> {
28-
try {
29-
if (connection == null || connection.isClosed()) {
30-
Connection clientConnection = client.getConnection();
31-
clientConnection.setAutoCommit(false);
32-
return clientConnection;
33-
} else {
34-
return connection;
35-
}
36-
} catch (Exception e) {
37-
logger.log(LogLevel.ERROR, "Failed to get the connection", e);
38-
return null;
39-
}
40-
});
22+
public JavaSqlClient getClient() {
23+
return client;
4124
}
4225

4326
@Override
44-
protected void flushConnection(Connection connection) {
45-
// EMPTY
46-
}
47-
48-
@Override
49-
protected boolean shouldLockWhenModify() {
27+
protected boolean isSingleThread() {
5028
return true;
5129
}
5230

@@ -115,16 +93,4 @@ protected List<Object[]> toSaveValues(Object[] keys, Object[] values) {
11593
updateValues
11694
);
11795
}
118-
119-
@Override
120-
public void disable() {
121-
Connection connection = connectionReference.getAndSet(null);
122-
try {
123-
if (connection != null && !connection.isClosed()) {
124-
connection.close();
125-
}
126-
} catch (Exception e) {
127-
logger.log(LogLevel.ERROR, "Failed to close the connection", e);
128-
}
129-
}
13096
}

0 commit comments

Comments
 (0)