/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package DataLayer;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.apache.commons.dbcp2.BasicDataSource;
import DataLayer.settings;

/**
 * @author install1
 */
public class DBCPDataSource {
    private static BasicDataSource ds = new BasicDataSource();

    static {
        try {
            Properties prop = new Properties();
            String fileName = "app.config";
            try (FileInputStream fis = new FileInputStream(fileName)) {
                prop.load(fis);
            } catch (FileNotFoundException ex) {
            } catch (IOException ex) {
            }
            ds.setDriver(new com.mysql.cj.jdbc.Driver());
            ds.setUrl(prop.getProperty("app.url"));
            ds.setUsername(prop.getProperty("app.username"));
            ds.setPassword(prop.getProperty("app.password"));
            ds.setMaxTotal(-1);
            ds.setMinIdle(5);
            ds.setMaxIdle(-1);
            ds.setMaxOpenPreparedStatements(100);
            System.out.println("called BasicDataSource ");
        } catch (SQLException ex) {
            Logger.getLogger(DBCPDataSource.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static Connection getConnection() throws SQLException {
        return ds.getConnection();
    }

    private DBCPDataSource() {
    }
}
