<commit_before>package com.excilys.cdb.persistences;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;

public enum Connector {
    INSTANCE;

    private Connection connection;
    private Properties properties;

    /**
     * Seek DB properties and loads it.
     */
    Connector() {
        try {
            properties = new Properties();
            properties.load(this.getClass().getClassLoader().getResourceAsStream("local.properties"));
        } catch (Exception e) {
            throw new ConnectorException("Error : Properties cannot be loaded.", e);
        }
    }

    /**
     * Connects to the DB corresponding to the properties.
     */
    private void connect() throws ClassNotFoundException, SQLException{
        Class.forName(properties.getProperty("db.driver"));
        connection = DriverManager.getConnection(
                properties.getProperty("db.url"),
                properties.getProperty("db.username"),
                properties.getProperty("db.password"));
    }
    
    private void connectWithHikari() throws ClassNotFoundException, SQLException {
        HikariConfig config = new HikariConfig();
        Class.forName(properties.getProperty("db.driver"));
        config.setJdbcUrl(properties.getProperty("db.url"));
        config.setUsername(properties.getProperty("db.username"));
        config.setPassword(properties.getProperty("db.password"));
        config.addDataSourceProperty("cachePrepStmts", properties.getProperty("dataSource.cachePrepStmts"));
        config.addDataSourceProperty("prepStmtCacheSize", properties.getProperty("dataSource.prepStmtCacheSize"));
        config.addDataSourceProperty("prepStmtCacheSqlLimit", properties.getProperty("dataSource.prepStmtCacheSqlLimit"));
        config.setMaximumPoolSize(Integer.parseInt(properties.getProperty("dataSource.maximalPoolSize")));
        
        HikariDataSource dataSource = new HikariDataSource(config); 
        connection = dataSource.getConnection();
        dataSource.close();
    }

    /**
     * Disconnects from the DB.
     */
    public void disconnect() {
        try {
            if(connection != null && !connection.isClosed()){
                connection.close();
            }
        } catch (SQLException e) {
            throw new ConnectorException("Error : Database connection has not been correctly closed.", e);
        }
    }

    /**
     * Gets the connection to the DB, reconnected if disconnected.
     * @return connection to the db
     */
    public Connection getConnection() {
        try {
            if (connection == null || connection.isClosed()) {
                connectWithHikari();
            }
        } catch (ClassNotFoundException | SQLException e) {
            throw new ConnectorException("Error : Database connection cannot be done.", e);
        }

        return connection;
    }

    /**
     * Gets the state of the connection.
     * @return state of the connection
     */
    public boolean isDisconnected() {
        try {
            return (connection == null || connection.isClosed());
        } catch (SQLException e) {
            throw new ConnectorException("Error : Connector has encountered a problem during the connection check.", e);
        }
    }
}<commit_msg>[BUG/FIX] Connection closed too early<commit_after>package com.excilys.cdb.persistences;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;

public enum Connector {
    INSTANCE;

    private Connection connection;
    private HikariDataSource dataSource;
    private Properties properties;

    /**
     * Seek DB properties and loads it.
     */
    Connector() {
        try {
            properties = new Properties();
            properties.load(this.getClass().getClassLoader().getResourceAsStream("local.properties"));
        } catch (Exception e) {
            throw new ConnectorException("Error : Properties cannot be loaded.", e);
        }
    }

    /**
     * Connects to the DB corresponding to the properties.
     */
    private void connect() throws ClassNotFoundException, SQLException{
        Class.forName(properties.getProperty("db.driver"));
        connection = DriverManager.getConnection(
                properties.getProperty("db.url"),
                properties.getProperty("db.username"),
                properties.getProperty("db.password"));
    }
    
    private void connectWithHikari() throws ClassNotFoundException, SQLException {
        HikariConfig config = new HikariConfig();
        Class.forName(properties.getProperty("db.driver"));
        config.setJdbcUrl(properties.getProperty("db.url"));
        config.setUsername(properties.getProperty("db.username"));
        config.setPassword(properties.getProperty("db.password"));
        config.addDataSourceProperty("cachePrepStmts", properties.getProperty("dataSource.cachePrepStmts"));
        config.addDataSourceProperty("prepStmtCacheSize", properties.getProperty("dataSource.prepStmtCacheSize"));
        config.addDataSourceProperty("prepStmtCacheSqlLimit", properties.getProperty("dataSource.prepStmtCacheSqlLimit"));
        config.setMaximumPoolSize(Integer.parseInt(properties.getProperty("dataSource.maximalPoolSize")));
        
        dataSource = new HikariDataSource(config); 
        connection = dataSource.getConnection();
    }

    /**
     * Disconnects from the DB.
     */
    public void disconnect() {
        try {
            if(connection != null && !connection.isClosed()){
                connection.close();
                dataSource.close();
            }
        } catch (SQLException e) {
            throw new ConnectorException("Error : Database connection has not been correctly closed.", e);
        }
    }

    /**
     * Gets the connection to the DB, reconnected if disconnected.
     * @return connection to the db
     */
    public Connection getConnection() {
        try {
            if (connection == null || connection.isClosed()) {
                connectWithHikari();
                //connect();
            }
        } catch (ClassNotFoundException | SQLException e) {
            throw new ConnectorException("Error : Database connection cannot be done.", e);
        }

        return connection;
    }

    /**
     * Gets the state of the connection.
     * @return state of the connection
     */
    public boolean isDisconnected() {
        try {
            return (connection == null || connection.isClosed());
        } catch (SQLException e) {
            throw new ConnectorException("Error : Connector has encountered a problem during the connection check.", e);
        }
    }
}
{'fraction_non_alphanumeric': 0.1054534934181976, 'fraction_numerical': 0.0, 'mean_word_length': 2.6293963254593176, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6740849', 'n_tokens_mistral': 1724, 'n_tokens_neox': 1707, 'n_words': 399}