/*
 * 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 br.com.elotech.websaude.integracao.esus.model.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * @author elotech
 */
public class Conexao {

  protected static final String DRIVER = "org.postgresql.Driver";
  public static Connection CONN;

  public synchronized static boolean abrirConexao(String[] args) {
    try {
      Class.forName(DRIVER);
      CONN = DriverManager.getConnection("jdbc:postgresql://" + args[0] + "?currentSchema=social", args[1], args[2]);
      CONN.createStatement().execute("set search_path to 'social'");
      CONN.setAutoCommit(false);

      return true;
    } catch (ClassNotFoundException | SQLException ex) {
      Logger.getLogger(Conexao.class.getName()).log(Level.SEVERE, null, ex);
    }
    return false;
  }

  public synchronized static void fecharConexao() {
    if (CONN != null) {
      try {
        if (!CONN.isClosed()) {
          CONN.close();
        }
      } catch (SQLException ex) {
        Logger.getLogger(Conexao.class.getName()).log(Level.SEVERE, null, ex);
      }
    }
  }

}
