Add more information on failed database connection attempts

This commit is contained in:
GeorgH93 2020-04-27 00:59:35 +02:00
parent 020aae6b88
commit b9ee7dfc41
No known key found for this signature in database
GPG Key ID: D1630D37F9E4B3C8
7 changed files with 189 additions and 26 deletions

View File

@ -36,7 +36,7 @@
<dependency>
<groupId>at.pcgamingfreaks</groupId>
<artifactId>PluginLib</artifactId>
<version>1.0.22-SNAPSHOT</version>
<version>1.0.23-SNAPSHOT</version>
</dependency>
<!-- BadRabbit -->
<dependency>

View File

@ -84,39 +84,48 @@ public void close()
unCacheStrategie.close();
}
public static Database getDatabase(Minepacks plugin)
public static @Nullable Database getDatabase(Minepacks plugin)
{
String dbType = plugin.getConfiguration().getDatabaseType().toLowerCase(Locale.ROOT);
ConnectionProvider connectionProvider = null;
if(dbType.equals("shared") || dbType.equals("external") || dbType.equals("global"))
try
{
String dbType = plugin.getConfiguration().getDatabaseType().toLowerCase(Locale.ROOT);
ConnectionProvider connectionProvider = null;
if(dbType.equals("shared") || dbType.equals("external") || dbType.equals("global"))
{
/*if[STANDALONE]
plugin.getLogger().warning(ConsoleColor.RED + "The shared database connection option is not available in standalone mode!" + ConsoleColor.RESET);
return null;
else[STANDALONE]*/
at.pcgamingfreaks.PluginLib.Database.DatabaseConnectionPool pool = at.pcgamingfreaks.PluginLib.Bukkit.PluginLib.getInstance().getDatabaseConnectionPool();
if(pool == null)
{
plugin.getLogger().warning(ConsoleColor.RED + "The shared connection pool is not initialized correctly!" + ConsoleColor.RESET);
return null;
at.pcgamingfreaks.PluginLib.Database.DatabaseConnectionPool pool = at.pcgamingfreaks.PluginLib.Bukkit.PluginLib.getInstance().getDatabaseConnectionPool();
if(pool == null)
{
plugin.getLogger().warning(ConsoleColor.RED + "The shared connection pool is not initialized correctly!" + ConsoleColor.RESET);
return null;
}
dbType = pool.getDatabaseType().toLowerCase(Locale.ROOT);
connectionProvider = pool.getConnectionProvider();
/*end[STANDALONE]*/
}
dbType = pool.getDatabaseType().toLowerCase(Locale.ROOT);
connectionProvider = pool.getConnectionProvider();
/*end[STANDALONE]*/
Database database;
switch(dbType)
{
case "mysql": database = new MySQL(plugin, connectionProvider); break;
case "sqlite": database = new SQLite(plugin, connectionProvider); break;
case "flat":
case "file":
case "files":
database = new Files(plugin); break;
default: plugin.getLogger().warning(String.format(MESSAGE_UNKNOWN_DB_TYPE, plugin.getConfiguration().getDatabaseType())); return null;
}
database.init();
return database;
}
Database database;
switch(dbType)
catch(IllegalStateException ignored) {}
catch(Exception e)
{
case "mysql": database = new MySQL(plugin, connectionProvider); break;
case "sqlite": database = new SQLite(plugin, connectionProvider); break;
case "flat":
case "file":
case "files":
database = new Files(plugin); break;
default: plugin.getLogger().warning(String.format(MESSAGE_UNKNOWN_DB_TYPE, plugin.getConfiguration().getDatabaseType())); return null;
e.printStackTrace();
}
database.init();
return database;
return null;
}
public void backup(@NotNull Backpack backpack)

View File

@ -37,7 +37,7 @@
public abstract class SQL extends Database
{
private ConnectionProvider dataSource;
private final ConnectionProvider dataSource;
protected String tablePlayers, tableBackpacks, tableCooldowns; // Table Names
protected String fieldPlayerName, fieldPlayerID, fieldPlayerUUID, fieldBpOwner, fieldBpIts, fieldBpVersion, fieldBpLastUpdate, fieldCdPlayer, fieldCdTime; // Table Fields
@ -50,6 +50,7 @@ public SQL(@NotNull Minepacks plugin, @NotNull ConnectionProvider connectionProv
super(plugin);
dataSource = connectionProvider;
if(!dataSource.isAvailable()) throw new IllegalStateException("Failed to initialize database connection!");
loadSettings();
buildQuerys();

View File

@ -33,6 +33,7 @@
import at.pcgamingfreaks.Minepacks.Bukkit.Database.Helper.WorldBlacklistMode;
import at.pcgamingfreaks.Minepacks.Bukkit.Database.Language;
import at.pcgamingfreaks.Minepacks.Bukkit.Listener.*;
import at.pcgamingfreaks.Minepacks.Bukkit.SpecialInfoWorker.NoDatabaseWorker;
import at.pcgamingfreaks.StringUtils;
import at.pcgamingfreaks.Updater.UpdateProviders.BukkitUpdateProvider;
import at.pcgamingfreaks.Updater.UpdateProviders.JenkinsUpdateProvider;
@ -177,6 +178,11 @@ private void load()
{
lang.load(config);
database = Database.getDatabase(this);
if(database == null)
{
new NoDatabaseWorker(this);
return;
}
maxSize = config.getBackpackMaxSize();
at.pcgamingfreaks.Minepacks.Bukkit.Backpack.setShrinkApproach(config.getShrinkApproach());
at.pcgamingfreaks.Minepacks.Bukkit.Backpack.setTitle(config.getBPTitle(), config.getBPTitleOther());

View File

@ -0,0 +1,94 @@
/*
* Copyright (C) 2020 GeorgH93
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package at.pcgamingfreaks.Minepacks.Bukkit.SpecialInfoWorker;
import at.pcgamingfreaks.Bukkit.Message.Message;
import at.pcgamingfreaks.Bukkit.Message.MessageBuilder;
import at.pcgamingfreaks.Bukkit.RegisterablePluginCommand;
import at.pcgamingfreaks.Message.MessageColor;
import at.pcgamingfreaks.Minepacks.Bukkit.Minepacks;
import at.pcgamingfreaks.Minepacks.Bukkit.Permissions;
import at.pcgamingfreaks.Reflection;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.HandlerList;
import org.jetbrains.annotations.NotNull;
/**
* This worker will inform the admin that the plugin failed to connect to the database, he hopefully is able to solve the problem.
* It registers a new command that only allows to reload the plugin to prevent unnecessary downtime for the server cause of restarts.
*/
public class NoDatabaseWorker extends SpecialInfoBase implements CommandExecutor
{
private final Minepacks plugin;
private final RegisterablePluginCommand command;
private final Message messageDBProblem;
public NoDatabaseWorker(final @NotNull Minepacks plugin)
{
super(plugin, Permissions.RELOAD);
this.plugin = plugin;
Bukkit.getPluginManager().registerEvents(this, plugin);
command = new RegisterablePluginCommand(plugin, "backpack");
command.registerCommand();
command.setExecutor(this);
messageDBProblem = new MessageBuilder("Minepacks", MessageColor.GOLD).append(" failed to connect to its database!", MessageColor.RED).appendNewLine()
.append("Please check your configuration and reload the plugin (", MessageColor.RED).append("/backpack reload", MessageColor.BLUE).command("/backpack reload").append(")!", MessageColor.RED).getMessage();
}
@Override
protected void sendMessage(Player player)
{
messageDBProblem.send(player);
}
@Override
public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command cmd, @NotNull String s, String[] strings)
{
if(strings.length != 1 || !strings[0].equalsIgnoreCase("reload"))
{
commandSender.sendMessage(MessageColor.RED + "Only \"/backpack reload\" is available at the moment!");
}
else
{
if(commandSender.hasPermission(Permissions.RELOAD))
{
command.unregisterCommand();
HandlerList.unregisterAll(this);
try
{
plugin.getConfiguration().reload();
Reflection.getMethod(plugin.getClass(), "load").invoke(plugin);
}
catch(Exception e)
{
e.printStackTrace();
}
}
else
{
commandSender.sendMessage(MessageColor.RED + "You don't have the permission to do that!");
}
}
return true;
}
}

View File

@ -0,0 +1,53 @@
/*
* Copyright (C) 2020 GeorgH93
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package at.pcgamingfreaks.Minepacks.Bukkit.SpecialInfoWorker;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.plugin.java.JavaPlugin;
public abstract class SpecialInfoBase implements Listener
{
private final JavaPlugin plugin;
private final String permission;
protected SpecialInfoBase(final JavaPlugin plugin, final String permission)
{
this.plugin = plugin;
this.permission = permission;
}
@EventHandler
public void onJoin(final PlayerJoinEvent event)
{
if(event.getPlayer().hasPermission(permission))
{
Bukkit.getScheduler().runTaskLater(plugin, () -> {
if(event.getPlayer().isOnline())
{
sendMessage(event.getPlayer());
}
}, 3 * 20L); // Run with a 3 seconds delay
}
}
protected abstract void sendMessage(final Player player);
}

View File

@ -7,7 +7,7 @@
<packaging>pom</packaging>
<properties>
<revision>2.3.5</revision>
<revision>2.3.6-RC1</revision>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>