1
mirror of https://github.com/CarmJos/MineSQL.git synced 2026-06-05 00:48:16 +08:00

改用BeeCP,修改部分接口方法。

This commit is contained in:
2022-02-22 02:13:54 +08:00
parent 48eed63ee7
commit 67bd7a4141
13 changed files with 207 additions and 142 deletions
+2 -2
View File
@@ -31,8 +31,8 @@
</dependency>
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<groupId>com.github.chris2018998</groupId>
<artifactId>beecp</artifactId>
<scope>compile</scope>
</dependency>
@@ -9,6 +9,8 @@ import java.util.logging.Logger;
public interface EasySQLPluginPlatform {
@NotNull Map<String, DBConfiguration> readConfigurations();
@NotNull Map<String, Properties> readProperties();
@@ -4,21 +4,22 @@ import cc.carm.lib.easysql.api.SQLManager;
import cc.carm.lib.easysql.api.SQLQuery;
import cc.carm.lib.easysql.manager.SQLManagerImpl;
import cc.carm.plugin.easysql.api.DBConfiguration;
import cc.carm.plugin.easysql.api.EasySQLManager;
import cc.carm.plugin.easysql.api.EasySQLRegistry;
import cn.beecp.BeeDataSource;
import cn.beecp.BeeDataSourceConfig;
import com.google.common.collect.ImmutableMap;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.Unmodifiable;
import java.util.*;
import java.util.function.Consumer;
public class EasySQLManagerImpl implements EasySQLManager {
public class EasySQLRegistryImpl implements EasySQLRegistry {
private final HashMap<String, SQLManager> sqlManagerRegistry = new HashMap<>();
public EasySQLManagerImpl(@NotNull EasySQLPluginPlatform platform) {
public EasySQLRegistryImpl(@NotNull EasySQLPluginPlatform platform) {
Map<String, DBConfiguration> configurations = platform.readConfigurations();
if (configurations.isEmpty()) {
@@ -64,54 +65,53 @@ public class EasySQLManagerImpl implements EasySQLManager {
@Override
public @NotNull SQLManager create(@Nullable String name, @NotNull DBConfiguration configuration) {
HikariConfig config = new HikariConfig();
BeeDataSourceConfig config = new BeeDataSourceConfig();
config.setDriverClassName(configuration.getDriverClassName());
config.setJdbcUrl(configuration.getUrlPrefix() + configuration.getUrl());
Optional.ofNullable(configuration.getPoolName()).ifPresent(config::setPoolName);
Optional.ofNullable(configuration.getUsername()).ifPresent(config::setUsername);
Optional.ofNullable(configuration.getPassword()).ifPresent(config::setPassword);
Optional.ofNullable(configuration.getMaxPoolSize()).ifPresent(config::setMaximumPoolSize);
Optional.ofNullable(configuration.getMinIdle()).ifPresent(config::setMinimumIdle);
Optional.ofNullable(configuration.getPoolName()).ifPresent(config::setPoolName);
Optional.ofNullable(configuration.getMaxPoolSize()).ifPresent(config::setMaxActive);
Optional.ofNullable(configuration.getMaxActive()).ifPresent(config::setMaxActive);
Optional.ofNullable(configuration.getIdleTimeout()).ifPresent(config::setIdleTimeout);
Optional.ofNullable(configuration.getKeepaliveTime()).ifPresent(config::setKeepaliveTime);
Optional.ofNullable(configuration.getConnectionTimeout()).ifPresent(config::setConnectionTimeout);
Optional.ofNullable(configuration.getValidationTimeout()).ifPresent(config::setValidationTimeout);
Optional.ofNullable(configuration.getMaxLifetime()).ifPresent(config::setMaxLifetime);
Optional.ofNullable(configuration.getLeakDetectionThreshold()).ifPresent(config::setLeakDetectionThreshold);
Optional.ofNullable(configuration.getConnectionTestQuery()).ifPresent(config::setConnectionTestQuery);
Optional.ofNullable(configuration.getConnectionInitSql()).ifPresent(config::setConnectionInitSql);
Optional.ofNullable(configuration.getAutoCommit()).ifPresent(config::setAutoCommit);
Optional.ofNullable(configuration.getReadOnly()).ifPresent(config::setReadOnly);
Optional.ofNullable(configuration.getSchema()).ifPresent(config::setSchema);
Optional.ofNullable(configuration.getMaxWaitTime()).ifPresent(config::setMaxWait);
Optional.ofNullable(configuration.getMaxHoldTime()).ifPresent(config::setHoldTimeout);
Optional.ofNullable(configuration.getAutoCommit()).ifPresent(config::setDefaultAutoCommit);
Optional.ofNullable(configuration.getReadOnly()).ifPresent(config::setDefaultReadOnly);
Optional.ofNullable(configuration.getSchema()).ifPresent(config::setDefaultSchema);
Optional.ofNullable(configuration.getValidationSQL()).ifPresent(config::setValidTestSql);
Optional.ofNullable(configuration.getValidationTimeout()).ifPresent(config::setValidTestTimeout);
Optional.ofNullable(configuration.getValidationInterval()).ifPresent(config::setValidAssumeTime);
return create(name, config);
}
@Override
public @NotNull SQLManager create(@Nullable String name, @NotNull Properties properties) {
return create(name, new HikariConfig(properties));
return create(name, new BeeDataSourceConfig(properties));
}
@Override
public @NotNull SQLManager create(@Nullable String name, @NotNull String propertyFileName) {
return create(name, new HikariConfig(propertyFileName));
return create(name, new BeeDataSourceConfig(propertyFileName));
}
public @NotNull SQLManager create(@Nullable String name, @NotNull HikariConfig configuration) {
return new SQLManagerImpl(new HikariDataSource(configuration), name);
public @NotNull SQLManager create(@Nullable String name, @NotNull BeeDataSourceConfig configuration) {
return new SQLManagerImpl(new BeeDataSource(configuration), name);
}
@Override
public Map<UUID, SQLQuery> shutdown(SQLManager manager, boolean forceClose) {
Map<UUID, SQLQuery> queries = manager.getActiveQuery();
if (forceClose) manager.getActiveQuery().values().forEach(SQLQuery::close);
public void shutdown(SQLManager manager, @Nullable Consumer<Map<UUID, SQLQuery>> activeQueries) {
if (activeQueries != null) activeQueries.accept(manager.getActiveQuery());
if (manager.getDataSource() instanceof HikariDataSource) {
//Close hikari pool
((HikariDataSource) manager.getDataSource()).close();
if (manager.getDataSource() instanceof BeeDataSource) {
BeeDataSource dataSource = (BeeDataSource) manager.getDataSource();
dataSource.close(); //Close bee connection pool
}
return queries;
}
}
@@ -0,0 +1,11 @@
&5&l ______ _____ ____ _ &d&l_____ _ _
&5&l| ____| / ____|/ __ \| | &d&l| __ \| | (_)
&5&l| |__ __ _ ___ _ _| (___ | | | | | &d&l| |__) | |_ _ __ _ _ _ __
&5&l| __| / _` / __| | | |\___ \| | | | | &d&l| ___/| | | | |/ _` | | '_ \
&5&l| |___| (_| \__ \ |_| |____) | |__| | |____ &d&l| | | | |_| | (_| | | | | |
&5&l|______\__,_|___/\__, |_____/ \___\_\______| &d&l|_| |_|\__,_|\__, |_|_| |_|
&5&l __/ | &d&l __/ |
&5&l |___/ &d&l|___/
&f View more information at Github &d&lhttps://github.com/CarmJos/EasySQL-Plugin
@@ -3,16 +3,17 @@
# 检查更新为异步操作,绝不会影响性能与使用体验。
check-update: true
properties:
enable: true
folder: "properties/"
instances:
name1:
configurations:
"example-mariadb": # database id
driver-type: mariadb
url: 127.0.0.1 # 数据库地址
port: 3306 # 数据库端口
use-ssl: false # 是否使用SSL连接
username: minecraft # 数据库用户名
password: password #数据库连接密码
database: minecraft #数据库名
url: jdbc:mysql://localhost:3306/name1
port: 3306
username: mc
password: password
use-ssl: false
name2:
url: jdbc:mysql://localhost:3306/name2
username: mc
password: password