Search in sources :

Example 1 with DBInitException

use of com.djrapitops.plan.exceptions.database.DBInitException in project Plan by plan-player-analytics.

the class SQLiteDB method ensureConstructorIsAvailable.

private void ensureConstructorIsAvailable() {
    if (connectionConstructor != null) {
        return;
    }
    try {
        Class<?> connectionClass = driverClassLoader.loadClass("org.sqlite.jdbc4.JDBC4Connection");
        connectionConstructor = connectionClass.getConstructor(String.class, String.class, Properties.class);
    } catch (ClassNotFoundException | NoSuchMethodException e) {
        throw new DBInitException("Failed to initialize SQLite Driver", e);
    }
}
Also used : Properties(java.util.Properties) DBInitException(com.djrapitops.plan.exceptions.database.DBInitException)

Example 2 with DBInitException

use of com.djrapitops.plan.exceptions.database.DBInitException in project Plan by plan-player-analytics.

the class MySQLDB method setupDataSource.

/**
 * Setups the {@link HikariDataSource}
 */
@Override
public void setupDataSource() {
    if (driverClassLoader == null) {
        logger.info("Downloading MySQL Driver, this may take a while...");
        downloadDriver();
    }
    Thread currentThread = Thread.currentThread();
    ClassLoader previousClassLoader = currentThread.getContextClassLoader();
    // Set the context class loader to the driver class loader for Hikari to use for finding the Driver
    currentThread.setContextClassLoader(driverClassLoader);
    try {
        HikariConfig hikariConfig = new HikariConfig();
        String host = config.get(DatabaseSettings.MYSQL_HOST);
        String port = config.get(DatabaseSettings.MYSQL_PORT);
        String database = config.get(DatabaseSettings.MYSQL_DATABASE);
        String launchOptions = config.get(DatabaseSettings.MYSQL_LAUNCH_OPTIONS);
        // REGEX: match "?", match "word=word&" *-times, match "word=word"
        if (launchOptions.isEmpty() || !launchOptions.matches("\\?(((\\w|[-])+=.+)&)*((\\w|[-])+=.+)")) {
            launchOptions = "?rewriteBatchedStatements=true&useSSL=false";
            logger.error(locale.getString(PluginLang.DB_MYSQL_LAUNCH_OPTIONS_FAIL, launchOptions));
        }
        hikariConfig.setDriverClassName("com.mysql.cj.jdbc.Driver");
        hikariConfig.setJdbcUrl("jdbc:mysql://" + host + ":" + port + "/" + database + launchOptions);
        String username = config.get(DatabaseSettings.MYSQL_USER);
        String password = config.get(DatabaseSettings.MYSQL_PASS);
        hikariConfig.setUsername(username);
        hikariConfig.setPassword(password);
        hikariConfig.addDataSourceProperty("connectionInitSql", "set time_zone = '+00:00'");
        hikariConfig.setPoolName("Plan Connection Pool-" + increment);
        increment();
        hikariConfig.setAutoCommit(true);
        try {
            hikariConfig.setMaximumPoolSize(config.get(DatabaseSettings.MAX_CONNECTIONS));
        } catch (IllegalStateException e) {
            logger.warn(e.getMessage() + ", using 1 as maximum for now.");
            hikariConfig.setMaximumPoolSize(1);
        }
        hikariConfig.setMaxLifetime(TimeUnit.MINUTES.toMillis(25L));
        hikariConfig.setLeakDetectionThreshold(TimeUnit.SECONDS.toMillis(29L));
        this.dataSource = new HikariDataSource(hikariConfig);
    } catch (HikariPool.PoolInitializationException e) {
        throw new DBInitException("Failed to set-up HikariCP Datasource: " + e.getMessage(), e);
    } finally {
        unloadMySQLDriver();
    }
    // Reset the context classloader back to what it was originally set to, now that the DataSource is created
    currentThread.setContextClassLoader(previousClassLoader);
}
Also used : HikariDataSource(com.zaxxer.hikari.HikariDataSource) HikariPool(com.zaxxer.hikari.pool.HikariPool) HikariConfig(com.zaxxer.hikari.HikariConfig) DBInitException(com.djrapitops.plan.exceptions.database.DBInitException)

Example 3 with DBInitException

use of com.djrapitops.plan.exceptions.database.DBInitException in project Plan by plan-player-analytics.

the class SQLDB method init.

@Override
public void init() {
    List<Runnable> unfinishedTransactions = closeTransactionExecutor(transactionExecutor);
    this.transactionExecutor = transactionExecutorServiceProvider.get();
    setState(State.PATCHING);
    setupDataSource();
    setupDatabase();
    for (Runnable unfinishedTransaction : unfinishedTransactions) {
        transactionExecutor.submit(unfinishedTransaction);
    }
    // See executeTransaction method below.
    if (getState() == State.CLOSED) {
        throw new DBInitException("Failed to set-up Database");
    }
}
Also used : PluginRunnable(net.playeranalytics.plugin.scheduling.PluginRunnable) DBInitException(com.djrapitops.plan.exceptions.database.DBInitException)

Example 4 with DBInitException

use of com.djrapitops.plan.exceptions.database.DBInitException in project Plan by plan-player-analytics.

the class DBSystem method enable.

@Override
public void enable() {
    try {
        db.init();
        logger.info(locale.getString(PluginLang.ENABLED_DATABASE, db.getType().getName()));
    } catch (DBInitException e) {
        Throwable cause = e.getCause();
        String message = cause == null ? e.getMessage() : cause.getMessage();
        throw new EnableException(db.getType().getName() + " init failure: " + message, cause);
    }
}
Also used : EnableException(com.djrapitops.plan.exceptions.EnableException) DBInitException(com.djrapitops.plan.exceptions.database.DBInitException)

Aggregations

DBInitException (com.djrapitops.plan.exceptions.database.DBInitException)4 EnableException (com.djrapitops.plan.exceptions.EnableException)1 HikariConfig (com.zaxxer.hikari.HikariConfig)1 HikariDataSource (com.zaxxer.hikari.HikariDataSource)1 HikariPool (com.zaxxer.hikari.pool.HikariPool)1 Properties (java.util.Properties)1 PluginRunnable (net.playeranalytics.plugin.scheduling.PluginRunnable)1