package Datos;

import java.sql.Connection;
import java.sql.SQLException;
import javax.naming.InitialContext;
import javax.sql.DataSource;

public class ConnectionPool {

    private static ConnectionPool pool;
    private static DataSource dataSource;

    private ConnectionPool() {
        try {
            InitialContext ic = new InitialContext();
            dataSource = (DataSource) ic.lookup("java:app/jdbc/Bappsket");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static ConnectionPool getInstance() {
        if (pool == null) {
            pool = new ConnectionPool();
        }
        return pool;
    }

    public Connection getConnection() {
        Connection connection = null;
        try {
            connection = dataSource.getConnection("bappsket", "1234");
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return connection;
    }

    public void freeConnection(Connection c) {
        try {
            c.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
