2018-07-06 13:30:00 +00:00
|
|
|
package com.eu.habbo.database;
|
|
|
|
|
|
|
|
import com.eu.habbo.Emulator;
|
|
|
|
import com.eu.habbo.core.ConfigurationManager;
|
|
|
|
import com.zaxxer.hikari.HikariDataSource;
|
2020-05-03 01:46:07 +02:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
2018-07-06 13:30:00 +00:00
|
|
|
|
2019-05-26 21:14:53 +03:00
|
|
|
public class Database {
|
2018-07-08 21:32:00 +00:00
|
|
|
|
2020-05-03 01:46:07 +02:00
|
|
|
private static final Logger LOGGER = LoggerFactory.getLogger(Database.class);
|
2018-07-08 21:32:00 +00:00
|
|
|
|
2020-05-03 01:46:07 +02:00
|
|
|
private HikariDataSource dataSource;
|
2018-07-06 13:30:00 +00:00
|
|
|
private DatabasePool databasePool;
|
2019-05-26 21:14:53 +03:00
|
|
|
|
|
|
|
public Database(ConfigurationManager config) {
|
2018-07-06 13:30:00 +00:00
|
|
|
long millis = System.currentTimeMillis();
|
|
|
|
|
|
|
|
boolean SQLException = false;
|
|
|
|
|
2019-05-26 21:14:53 +03:00
|
|
|
try {
|
2018-07-06 13:30:00 +00:00
|
|
|
this.databasePool = new DatabasePool();
|
2019-05-26 21:14:53 +03:00
|
|
|
if (!this.databasePool.getStoragePooling(config)) {
|
2020-05-03 01:46:07 +02:00
|
|
|
LOGGER.info("Failed to connect to the database. Please check config.ini and make sure the MySQL process is running. Shutting down...");
|
2018-07-06 13:30:00 +00:00
|
|
|
SQLException = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.dataSource = this.databasePool.getDatabase();
|
2019-05-26 21:14:53 +03:00
|
|
|
} catch (Exception e) {
|
2018-07-06 13:30:00 +00:00
|
|
|
SQLException = true;
|
2020-05-03 01:46:07 +02:00
|
|
|
LOGGER.error("Failed to connect to your database.", e);
|
2019-05-26 21:14:53 +03:00
|
|
|
} finally {
|
2020-05-03 01:46:07 +02:00
|
|
|
if (SQLException) {
|
2018-07-06 13:30:00 +00:00
|
|
|
Emulator.prepareShutdown();
|
2020-05-03 01:46:07 +02:00
|
|
|
}
|
2018-07-06 13:30:00 +00:00
|
|
|
}
|
|
|
|
|
2020-05-03 01:46:07 +02:00
|
|
|
LOGGER.info("Database -> Connected! ({} MS)", System.currentTimeMillis() - millis);
|
2018-07-06 13:30:00 +00:00
|
|
|
}
|
|
|
|
|
2019-05-26 21:14:53 +03:00
|
|
|
public void dispose() {
|
|
|
|
if (this.databasePool != null) {
|
2018-07-06 13:30:00 +00:00
|
|
|
this.databasePool.getDatabase().close();
|
|
|
|
}
|
|
|
|
|
|
|
|
this.dataSource.close();
|
|
|
|
}
|
|
|
|
|
2019-05-26 21:14:53 +03:00
|
|
|
public HikariDataSource getDataSource() {
|
2018-07-06 13:30:00 +00:00
|
|
|
return this.dataSource;
|
|
|
|
}
|
|
|
|
|
2019-05-26 21:14:53 +03:00
|
|
|
public DatabasePool getDatabasePool() {
|
2018-07-06 13:30:00 +00:00
|
|
|
return this.databasePool;
|
|
|
|
}
|
|
|
|
}
|