Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class SQLDatabase extends IDatabase<Connection>{
- private final String host, database, username, password;
- private final int port;
- private HikariDataSource hikariDataSource;
- /**
- * Initialisation des informations concernants la connection.
- *
- * @param host
- * @param database
- * @param username
- * @param password
- */
- public SQLDatabase(String host, int port, String database, String username, String password) {
- this.host = host;
- this.port = port;
- this.database = database;
- this.username = username;
- this.password = password;
- registerModule(AccountModule.class);
- registerModule(PlayerInfoModule.class);
- }
- @Override
- public void connect() {
- final HikariConfig hikariConfig = new HikariConfig();
- hikariConfig.setJdbcUrl("jdbc:mysql://" + this.host + ":"+port+"/" + this.database + "?useUnicode=yes");
- hikariConfig.setUsername(username);
- hikariConfig.setPassword(password);
- hikariConfig.setMaxLifetime(4 * 60 * 60 * 1000);
- hikariConfig.setLeakDetectionThreshold(300000L);
- hikariConfig.setMaximumPoolSize(4);
- hikariConfig.setConnectionTimeout(20000L);
- hikariConfig.addDataSourceProperty("useSSL", false);
- this.hikariDataSource = new HikariDataSource(hikariConfig);
- /*if (!isConnected()) {
- try {
- //this.connection = DriverManager.getConnection("jdbc:mysql://" + this.host + ":"+port+"/" + this.database + "?autoReconnect=true&useUnicode=yes", this.username, this.password);
- } catch (SQLException exception) {
- exception.printStackTrace();
- }
- } else {
- // TODO: Finir le Logger
- }*/
- }
- @Override
- public void disconnect() {
- if (!isConnected()) {
- this.hikariDataSource.close();
- } else {
- // TODO: Finir le Logger
- }
- }
- @Override
- public boolean isConnected() {
- return hikariDataSource != null && !hikariDataSource.isClosed();
- }
- @Override
- public Connection getResource() {
- try{
- return hikariDataSource.getConnection();
- }catch (SQLException e){
- e.printStackTrace();
- }
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement