package org.academiadecodigo.bootcamp.persistence;

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

/**
 * MIT License
 * (c) 2017 Ricardo Constantino
 */

public class ConnectionManager {

    private static final String dbUrl = "jdbc:mysql:///cadetlogin?useSSL=false";
    private static final String user = "root";
    private static final String pass = "";

    private static ConnectionManager connectionManager;
    private Connection connection = null;

    public static ConnectionManager getInstance() {

        if (connectionManager == null) {
            synchronized (ConnectionManager.class) {
                if (connectionManager == null) {
                    connectionManager = new ConnectionManager();
                }
            }
        }

        return connectionManager;

    }

    public Connection getConnection() {
        try {
            if (connection == null || connection.isClosed()) {
                connection = DriverManager.getConnection(dbUrl, user, pass);
            }
        } catch (SQLException ex) {
            System.out.println("Failure to connect to database : " + ex.getMessage());
        }
        return connection;

    }

    public void close() {
        System.out.println("closing connection to db");
        try {
            if (connection != null) {
                connection.close();
                connection = null;
            }
        } catch (SQLException ex) {
            System.out.println("Failure to close database connections: " + ex.getMessage());
        }
    }

}
