
package datos;

import excepciones.GenericaExcepcion;
import java.sql.Connection;
import java.sql.SQLException;
// import oracle.ucp.jdbc.PoolDataSource;
// import oracle.ucp.jdbc.PoolDataSourceFactory;
import org.apache.commons.dbcp2.BasicDataSource;

public class ConexionBaseDatos {
    
// ********************************** MySQL ***************************************
      private static final BasicDataSource BASICDATASOURCE = new BasicDataSource();
      private static final String CLASSDRIVER = "com.mysql.jdbc.Driver";
      private static final String URLCONEXION = "jdbc:mysql://localhost/biblioteca";
      private static final String USUARIO = "root";
      private static final String PASSWORD = "";
      private static ConexionBaseDatos instancia;
      private static boolean dataSourceInicializado = false;

   
    static {
            BASICDATASOURCE.setDriverClassName(CLASSDRIVER);
            BASICDATASOURCE.setUrl(URLCONEXION);
            BASICDATASOURCE.setUsername(USUARIO);
            BASICDATASOURCE.setPassword(PASSWORD);
            BASICDATASOURCE.setInitialSize(50);
            BASICDATASOURCE.setMinIdle(2);
            BASICDATASOURCE.setMaxIdle(50);
            BASICDATASOURCE.setMaxTotal(50); 
    }
  
    
    public Connection abrirConexion() throws Exception {
        Connection connection = null;  
        try {        
              if (BASICDATASOURCE.getNumIdle() == 0  &&  dataSourceInicializado)
                  throw new GenericaExcepcion(20);
              connection = BASICDATASOURCE.getConnection();
              dataSourceInicializado = true;
        } catch (SQLException excepcion) {          
            throw new GenericaExcepcion(21);
        }          
        return connection;         
    }
// ********************************************************************************        
/*
// ********************************** Oracle **************************************
      private static final PoolDataSource POOLDATASOURCE = PoolDataSourceFactory.getPoolDataSource();
      private static final String CLASSDRIVER = "oracle.jdbc.pool.OracleDataSource";
      private static final String URLCONEXION = "jdbc:oracle:thin:@localhost:1521:ORCL";
      private static final String USUARIO = "scott";
      private static final String PASSWORD = "tiger";
      private static ConexionBaseDatos instancia;
      private static boolean dataSourceInicializado = false;
      
      
    static {
        try {
            POOLDATASOURCE.setConnectionFactoryClassName(CLASSDRIVER);
            POOLDATASOURCE.setURL(URLCONEXION);
            POOLDATASOURCE.setUser(USUARIO);
            POOLDATASOURCE.setPassword(PASSWORD);
            POOLDATASOURCE.setInitialPoolSize(50);
            POOLDATASOURCE.setMinPoolSize(1);
            POOLDATASOURCE.setMaxPoolSize(50);            
        } catch (SQLException excepcion) {          
            System.out.println("Se ha producido un problema al intentar inicializar el pool de conexiones");
        }           
    }      

    
    public Connection abrirConexion() throws Exception {
        Connection connection = null;  
        try {        
              if (POOLDATASOURCE.getAvailableConnectionsCount() == 0  &&  dataSourceInicializado)
                  throw new GenericaExcepcion(20);
              connection = POOLDATASOURCE.getConnection();
              dataSourceInicializado = true;
        } catch (SQLException excepcion) {          
            throw new GenericaExcepcion(20);
        }          
        return connection;         
    }
// ********************************************************************************
*/    
    public static ConexionBaseDatos getInstancia() {
        if (instancia == null)
            instancia = new ConexionBaseDatos();
        
        return instancia;   
    }    
    
    
    public  void cerrarConexion(Connection connection) throws GenericaExcepcion {
        try {
             if (connection!= null) 
                connection.close();    
        } catch (SQLException excepcion) {          
            throw new GenericaExcepcion(22);
        }    
    }                 
}
