Skip to content

Commit

Permalink
新增错误处理
Browse files Browse the repository at this point in the history
  • Loading branch information
mcchampions committed Jan 14, 2025
1 parent 6953936 commit 6bc0fdd
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
import com.xzavier0722.mc.plugin.slimefun4.storage.common.DataScope;
import com.xzavier0722.mc.plugin.slimefun4.storage.common.RecordSet;
import com.zaxxer.hikari.HikariDataSource;
import io.github.thebusybiscuit.slimefun4.api.ErrorReport;
import io.github.thebusybiscuit.slimefun4.implementation.Slimefun;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collections;
import java.util.List;

public abstract class SqlCommonAdapter<T extends ISqlCommonConfig> implements IDataSourceAdapter<T> {
Expand All @@ -30,16 +33,19 @@ public void prepare(T config) {
protected void executeSql(String sql) {
try (Connection conn = ds.getConnection()) {
SqlUtils.execSql(conn, sql);
} catch (SQLException e) {
throw new IllegalStateException("An exception thrown while executing sql: " + sql, e);
} catch (Exception e) {
Slimefun.logger().warning("执行SQL失败, 抛出异常.");
e.printStackTrace();
}
}

protected List<RecordSet> executeQuery(String sql) {
try (Connection conn = ds.getConnection()) {
return SqlUtils.execQuery(conn, sql);
} catch (SQLException e) {
throw new IllegalStateException("An exception thrown while executing sql: " + sql, e);
} catch (Exception e) {
Slimefun.logger().warning("执行SQL失败, 抛出异常.");
e.printStackTrace();
return Collections.emptyList();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ public void shutdown() {
callbackExecutor.shutdownNow();
try {
float totalTask = scheduledWriteTasks.size();
var pendingTask = scheduledWriteTasks.size();
int pendingTask = scheduledWriteTasks.size();
while (pendingTask > 0) {
var doneTaskPercent = String.format("%.1f", (totalTask - pendingTask) / totalTask * 100);
String doneTaskPercent = String.format("%.1f", (totalTask - pendingTask) / totalTask * 100);
logger.log(Level.INFO, "数据保存中,请稍候... 剩余 {0} 个任务 ({1}%)", new Object[] {pendingTask, doneTaskPercent});
TimeUnit.SECONDS.sleep(1);
pendingTask = scheduledWriteTasks.size();
Expand Down Expand Up @@ -88,8 +88,8 @@ protected void scheduleWriteTask(ScopeKey scopeKey, RecordKey key, RecordSet dat
protected void scheduleWriteTask(ScopeKey scopeKey, RecordKey key, Runnable task, boolean forceScopeKey) {
lock.lock(scopeKey);
try {
var scopeToUse = forceScopeKey ? scopeKey : key;
var queuedTask = scheduledWriteTasks.get(scopeKey);
ScopeKey scopeToUse = forceScopeKey ? scopeKey : key;
QueuedWriteTask queuedTask = scheduledWriteTasks.get(scopeKey);
if (queuedTask == null && scopeKey != scopeToUse) {
queuedTask = scheduledWriteTasks.get(scopeToUse);
}
Expand Down

0 comments on commit 6bc0fdd

Please sign in to comment.