Skip to content

Commit 732ffa7

Browse files
committed
Revert "try connection per transaction on SQLite"
This reverts commit bab3ea3.
1 parent 1c439f7 commit 732ffa7

File tree

1 file changed

+30
-10
lines changed

1 file changed

+30
-10
lines changed

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

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@
77

88
import java.io.File;
99
import java.sql.Connection;
10-
import java.sql.SQLException;
1110
import java.util.Arrays;
1211
import java.util.List;
12+
import java.util.concurrent.atomic.AtomicReference;
1313
import java.util.function.Consumer;
1414

1515
public class SqliteStorageSupplier extends SqlStorageSupplier {
1616
private final JavaSqlClient client;
17+
private final AtomicReference<Connection> connectionReference = new AtomicReference<>();
1718

1819
public SqliteStorageSupplier(Consumer<Setting> databaseSettingConsumer, File baseHolder) {
1920
Setting setting = Setting.create(new SqliteFileDriver(baseHolder));
@@ -22,19 +23,26 @@ public SqliteStorageSupplier(Consumer<Setting> databaseSettingConsumer, File bas
2223
}
2324

2425
@Override
25-
protected Connection getConnection() throws SQLException {
26-
Connection connection = client.getConnection();
27-
connection.setAutoCommit(false);
28-
return connection;
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+
});
2941
}
3042

3143
@Override
3244
protected void flushConnection(Connection connection) {
33-
try {
34-
connection.close();
35-
} catch (SQLException e) {
36-
logger.log(LogLevel.ERROR, "Failed to close connection", e);
37-
}
45+
// EMPTY
3846
}
3947

4048
@Override
@@ -102,4 +110,16 @@ protected List<Object[]> toSaveValues(Object[] keys, Object[] values) {
102110
updateValues
103111
);
104112
}
113+
114+
@Override
115+
public void disable() {
116+
Connection connection = connectionReference.getAndSet(null);
117+
try {
118+
if (connection != null && !connection.isClosed()) {
119+
connection.close();
120+
}
121+
} catch (Exception e) {
122+
logger.log(LogLevel.ERROR, "Failed to close the connection", e);
123+
}
124+
}
105125
}

0 commit comments

Comments
 (0)