From 271f1b56b003af4c997c82f7dc33ecf8f6fc3558 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=9D=8F=E9=BB=91?= Date: Sat, 3 Nov 2018 23:02:20 +0800 Subject: [PATCH] TabooLib v4.6-R4 + SQL update. --- .../me/skymc/taboolib/TabooLibLoader.java | 14 +++++ .../skymc/taboolib/mysql/builder/SQLHost.java | 18 ++++++- .../taboolib/mysql/hikari/HikariHandler.java | 26 +++------- src/main/resources/hikarisettings.yml | 52 +++++++++---------- 4 files changed, 65 insertions(+), 45 deletions(-) diff --git a/src/main/java/me/skymc/taboolib/TabooLibLoader.java b/src/main/java/me/skymc/taboolib/TabooLibLoader.java index ceaf053..6a7333c 100644 --- a/src/main/java/me/skymc/taboolib/TabooLibLoader.java +++ b/src/main/java/me/skymc/taboolib/TabooLibLoader.java @@ -38,6 +38,7 @@ public class TabooLibLoader implements Listener { static TabooLibDeprecated tabooLibDeprecated; static Map> pluginClasses = Maps.newHashMap(); static List loaders = Lists.newArrayList(); + static List tasks = Lists.newArrayList(); static void setup() { testInternet(); @@ -56,6 +57,15 @@ public class TabooLibLoader implements Listener { } catch (Exception e) { e.printStackTrace(); } + Bukkit.getScheduler().runTask(TabooLib.instance(), () -> { + for (Runnable task : tasks) { + try { + task.run(); + } catch (Exception e) { + e.printStackTrace(); + } + } + }); } static void unregister() { @@ -80,6 +90,10 @@ public class TabooLibLoader implements Listener { return classes == null ? new ArrayList<>() : new ArrayList<>(classes); } + public static void runTaskOnEnabled(Runnable runnable) { + tasks.add(runnable); + } + static boolean isLoader(Class pluginClass) { return !Loader.class.equals(pluginClass) && Loader.class.isAssignableFrom(pluginClass); } diff --git a/src/main/java/me/skymc/taboolib/mysql/builder/SQLHost.java b/src/main/java/me/skymc/taboolib/mysql/builder/SQLHost.java index a0195bb..9b095a6 100644 --- a/src/main/java/me/skymc/taboolib/mysql/builder/SQLHost.java +++ b/src/main/java/me/skymc/taboolib/mysql/builder/SQLHost.java @@ -1,10 +1,13 @@ package me.skymc.taboolib.mysql.builder; import com.ilummc.tlib.util.Strings; +import me.skymc.taboolib.string.ArrayUtils; import org.bukkit.configuration.ConfigurationSection; import org.bukkit.plugin.Plugin; +import java.util.List; import java.util.Objects; +import java.util.stream.Collectors; /** * @Author sky @@ -19,6 +22,7 @@ public class SQLHost { private String database; private Plugin plugin; private boolean autoClose; + private List flags = ArrayUtils.asList("characterEncoding=utf-8", "useSSL=false"); public SQLHost(ConfigurationSection section, Plugin plugin) { this(section, plugin, false); @@ -70,14 +74,26 @@ public class SQLHost { return autoClose; } + public List getFlags() { + return flags; + } + public String getConnectionUrl() { - return Strings.replaceWithOrder("jdbc:mysql://{0}:{1}/{2}?characterEncoding=utf-8&useSSL=false", this.host, this.port, this.database); + return Strings.replaceWithOrder("jdbc:mysql://{0}:{1}/{2}" + getFlagsInUrl(), this.host, this.port, this.database); } public String getConnectionUrlSimple() { return Strings.replaceWithOrder("jdbc:mysql://{0}:{1}/{2}", this.host, this.port, this.database); } + public String getFlagsInUrl() { + if (flags.isEmpty()) { + return ""; + } + String collect = flags.stream().map(f -> f + "&").collect(Collectors.joining()); + return "?" + collect.substring(0, collect.length() - 1); + } + @Override public boolean equals(Object o) { if (this == o) { diff --git a/src/main/java/me/skymc/taboolib/mysql/hikari/HikariHandler.java b/src/main/java/me/skymc/taboolib/mysql/hikari/HikariHandler.java index a422313..d18cc9b 100644 --- a/src/main/java/me/skymc/taboolib/mysql/hikari/HikariHandler.java +++ b/src/main/java/me/skymc/taboolib/mysql/hikari/HikariHandler.java @@ -64,28 +64,18 @@ public class HikariHandler { config.setJdbcUrl(sqlHost.getConnectionUrl()); config.setUsername(sqlHost.getUser()); config.setPassword(sqlHost.getPassword()); - config.setConnectionTestQuery("SELECT 1"); config.setAutoCommit(settings.getBoolean("DefaultSettings.AutoCommit", true)); - config.setMinimumIdle(settings.getInt("DefaultSettings.MinimumIdle", 1)); + config.setMinimumIdle(settings.getInt("DefaultSettings.MinimumIdle", -1)); config.setMaximumPoolSize(settings.getInt("DefaultSettings.MaximumPoolSize", 10)); - config.setValidationTimeout(settings.getInt("DefaultSettings.ValidationTimeout", 3000)); - config.setConnectionTimeout(settings.getInt("DefaultSettings.ConnectionTimeout", 10000)); - config.setIdleTimeout(settings.getInt("DefaultSettings.IdleTimeout", 60000)); - config.setMaxLifetime(settings.getInt("DefaultSettings.MaxLifetime", 60000)); + config.setValidationTimeout(settings.getInt("DefaultSettings.ValidationTimeout", 5000)); + config.setConnectionTimeout(settings.getInt("DefaultSettings.ConnectionTimeout", 30000)); + config.setIdleTimeout(settings.getInt("DefaultSettings.IdleTimeout", 600000)); + config.setMaxLifetime(settings.getInt("DefaultSettings.MaxLifetime", 1800000)); + if (settings.contains("DefaultSettings.ConnectionTestQuery")) { + config.setConnectionTestQuery(settings.getString("DefaultSettings.ConnectionTestQuery")); + } if (settings.contains("DefaultSettings.DataSourceProperty")) { settings.getConfigurationSection("DefaultSettings.DataSourceProperty").getKeys(false).forEach(key -> config.addDataSourceProperty(key, settings.getString("DefaultSettings.DataSourceProperty." + key))); - } else { - config.addDataSourceProperty("cachePrepStmts", "true"); - config.addDataSourceProperty("prepStmtCacheSize", "250"); - config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048"); - config.addDataSourceProperty("useServerPrepStmts", "true"); - config.addDataSourceProperty("useLocalSessionState", "true"); - config.addDataSourceProperty("useLocalTransactionState", "true"); - config.addDataSourceProperty("rewriteBatchedStatements", "true"); - config.addDataSourceProperty("cacheResultSetMetadata", "true"); - config.addDataSourceProperty("cacheServerConfiguration", "true"); - config.addDataSourceProperty("elideSetAutoCommits", "true"); - config.addDataSourceProperty("maintainTimeStats", "false"); } return config; } diff --git a/src/main/resources/hikarisettings.yml b/src/main/resources/hikarisettings.yml index 81b6f5b..3d87887 100644 --- a/src/main/resources/hikarisettings.yml +++ b/src/main/resources/hikarisettings.yml @@ -1,32 +1,32 @@ # 默认连接池配置 DefaultSettings: DriverClassName: 'com.mysql.jdbc.Driver' + # 自动提交从池中返回的连接 AutoCommit: true + # 池中维护的最小空闲连接数 MinimumIdle: 1 - # 连接池大小 + # 池中最大连接数,包括闲置和使用中的连接 MaximumPoolSize: 10 - # 用来指定验证连接有效性的超时时间(毫秒/默认: 5秒) - ValidationTimeout: 3000 - # 等待连接池分配连接的最大时长(毫秒/默认: 30秒) - # 超过这个时长还没可用的连接则发生 SQLException - ConnectionTimeout: 10000 - # 一个连接idle状态的最大时长(毫秒/默认: 10分钟),超时则被释放 - IdleTimeout: 60000 - # 一个连接的生命时长(毫秒/默认: 30分钟),超时而且没被使用则被释放 - MaxLifetime: 60000 - # 是否自定义配置,为true时下面两个参数才生效 - DataSourceProperty: - cachePrepStmts: true - # 连接池大小默认25,官方推荐250-500 - prepStmtCacheSize: 250 - # 单条语句最大长度默认256,官方推荐2048 - prepStmtCacheSqlLimit: 2048 - # 新版本MySQL支持服务器端准备,开启能够得到显著性能提升 - useServerPrepStmts: true - useLocalSessionState: true - useLocalTransactionState: true - rewriteBatchedStatements: true - cacheResultSetMetadata: true - cacheServerConfiguration: true - elideSetAutoCommits: true - maintainTimeStats: false \ No newline at end of file + # 用来指定验证连接有效性的超时时间 + ValidationTimeout: 5000 + # 等待来自池的连接的最大毫秒数 + ConnectionTimeout: 30000 + # 一个连接idle状态的最大时长,超时则被释放 + IdleTimeout: 600000 + # 一个连接的生命时长,超时而且没被使用则被释放 + MaxLifetime: 1800000 + # 如果您的驱动程序支持JDBC4,我们强烈建议您不要设置此属性 + ConnectionTestQuery: SELECT 1 + # 其他自定义配置 + DataSourceProperty: {} +# cachePrepStmts: true +# prepStmtCacheSize: 250 +# prepStmtCacheSqlLimit: 2048 +# useServerPrepStmts: true +# useLocalSessionState: true +# useLocalTransactionState: true +# rewriteBatchedStatements: true +# cacheResultSetMetadata: true +# cacheServerConfiguration: true +# elideSetAutoCommits: true +# maintainTimeStats: false \ No newline at end of file