Skip to content

Commit bab3ea3

Browse files
committed
try connection per transaction on SQLite
1 parent 00a3adf commit bab3ea3

File tree

1 file changed

+10
-30
lines changed

1 file changed

+10
-30
lines changed

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

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

88
import java.io.File;
99
import java.sql.Connection;
10+
import java.sql.SQLException;
1011
import java.util.Arrays;
1112
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<>();
1817

1918
public SqliteStorageSupplier(Consumer<Setting> databaseSettingConsumer, File baseHolder) {
2019
Setting setting = Setting.create(new SqliteFileDriver(baseHolder));
@@ -23,26 +22,19 @@ public SqliteStorageSupplier(Consumer<Setting> databaseSettingConsumer, File bas
2322
}
2423

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

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

4840
@Override
@@ -110,16 +102,4 @@ protected List<Object[]> toSaveValues(Object[] keys, Object[] values) {
110102
updateValues
111103
);
112104
}
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-
}
125105
}

0 commit comments

Comments
 (0)