Skip to content

Commit

Permalink
use lock when the database is single-threaded only
Browse files Browse the repository at this point in the history
  • Loading branch information
HSGamer committed Jan 23, 2025
1 parent 3440263 commit 47c180b
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
import me.hsgamer.hscore.database.Setting;
import me.hsgamer.hscore.database.client.sql.java.JavaSqlClient;
import me.hsgamer.hscore.database.driver.mysql.MySqlDriver;
import me.hsgamer.hscore.logger.common.LogLevel;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collections;
import java.util.List;
import java.util.function.Consumer;
Expand All @@ -21,19 +18,8 @@ public MySqlStorageSupplier(Consumer<Setting> databaseSettingConsumer) {
}

@Override
protected Connection getConnection() throws SQLException {
Connection connection = client.getConnection();
connection.setAutoCommit(false);
return connection;
}

@Override
protected void flushConnection(Connection connection) {
try {
connection.close();
} catch (SQLException e) {
logger.log(LogLevel.ERROR, "Failed to close connection", e);
}
public JavaSqlClient getClient() {
return client;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package me.hsgamer.topper.storage.simple.supplier;

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

protected abstract Connection getConnection() throws SQLException;
protected abstract SqlClient<?> getClient();

protected abstract void flushConnection(Connection connection);

protected boolean shouldLockWhenModify() {
protected boolean isSingleThread() {
return false;
}

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

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

private Connection getConnection() throws SQLException {
Connection connection = getClient().getConnection();
connection.setAutoCommit(false);
return connection;
}

private void flushConnection(Connection connection) {
try {
connection.close();
} catch (SQLException e) {
logger.log(LogLevel.ERROR, "Failed to close connection", e);
}
}

private void lock() {
if (isSingleThread()) {
lock.lock();
}
}

private void unlock() {
if (isSingleThread()) {
lock.unlock();
}
}

@Override
public <K, V> DataStorage<K, V> getStorage(String name, ValueConverter<K> keyConverter, ValueConverter<V> valueConverter) {
return new DataStorage<K, V>() {
@Override
public Map<K, V> load() {
lock();
Connection connection = null;
try {
connection = getConnection();
Expand All @@ -52,11 +78,13 @@ public Map<K, V> load() {
if (connection != null) {
flushConnection(connection);
}
unlock();
}
}

@Override
public Optional<V> load(K key) {
lock();
Connection connection = null;
try {
connection = getConnection();
Expand Down Expand Up @@ -88,14 +116,13 @@ public Optional<V> load(K key) {
if (connection != null) {
flushConnection(connection);
}
unlock();
}
}

@Override
public Optional<Modifier<K, V>> modify() {
if (shouldLockWhenModify()) {
lock.lock();
}
lock();
try {
Connection connection = getConnection();
Modifier<K, V> modifier = new Modifier<K, V>() {
Expand Down Expand Up @@ -154,9 +181,7 @@ public void commit() {
logger.log(LogLevel.ERROR, "Failed to commit", e);
} finally {
flushConnection(connection);
if (shouldLockWhenModify()) {
lock.unlock();
}
unlock();
}
}

Expand All @@ -168,21 +193,21 @@ public void rollback() {
logger.log(LogLevel.ERROR, "Failed to rollback", e);
} finally {
flushConnection(connection);
if (shouldLockWhenModify()) {
lock.unlock();
}
unlock();
}
}
};
return Optional.of(modifier);
} catch (SQLException e) {
logger.log(LogLevel.ERROR, "Failed to get connection", e);
unlock();
return Optional.empty();
}
}

@Override
public void onRegister() {
lock();
Connection connection = null;
try {
connection = getConnection();
Expand Down Expand Up @@ -224,6 +249,7 @@ public void onRegister() {
if (connection != null) {
flushConnection(connection);
}
unlock();
}
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,14 @@
import me.hsgamer.hscore.database.Setting;
import me.hsgamer.hscore.database.client.sql.java.JavaSqlClient;
import me.hsgamer.hscore.database.driver.sqlite.SqliteFileDriver;
import me.hsgamer.hscore.logger.common.LogLevel;

import java.io.File;
import java.sql.Connection;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Consumer;

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

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

@Override
protected Connection getConnection() {
return connectionReference.updateAndGet(connection -> {
try {
if (connection == null || connection.isClosed()) {
Connection clientConnection = client.getConnection();
clientConnection.setAutoCommit(false);
return clientConnection;
} else {
return connection;
}
} catch (Exception e) {
logger.log(LogLevel.ERROR, "Failed to get the connection", e);
return null;
}
});
public JavaSqlClient getClient() {
return client;
}

@Override
protected void flushConnection(Connection connection) {
// EMPTY
}

@Override
protected boolean shouldLockWhenModify() {
protected boolean isSingleThread() {
return true;
}

Expand Down Expand Up @@ -115,16 +93,4 @@ protected List<Object[]> toSaveValues(Object[] keys, Object[] values) {
updateValues
);
}

@Override
public void disable() {
Connection connection = connectionReference.getAndSet(null);
try {
if (connection != null && !connection.isClosed()) {
connection.close();
}
} catch (Exception e) {
logger.log(LogLevel.ERROR, "Failed to close the connection", e);
}
}
}

0 comments on commit 47c180b

Please sign in to comment.