Skip to content

Instantly share code, notes, and snippets.

@rodrigovilar
Created August 7, 2019 16:16
Show Gist options
  • Save rodrigovilar/1bb15b6d788147e845b7640e454ff180 to your computer and use it in GitHub Desktop.
Save rodrigovilar/1bb15b6d788147e845b7640e454ff180 to your computer and use it in GitHub Desktop.
package br.ufpb.dcx.aps;
import java.io.BufferedInputStream;
import java.net.URL;
import java.util.Properties;
public class ConexaoBD {
private static ConexaoBD[] instances = null;
private static Integer qtdConexoes = 1;
private static Integer indice = 0;
private ConexaoBD() {}
public static ConexaoBD getInstance() {
if(instances == null) {
//Carregar qtdConexoes
qtdConexoes = carregarDados("configuracao.properties");
package br.ufpb.dcx.aps;
import java.io.BufferedInputStream;
import java.net.URL;
import java.util.Properties;
public class ConexaoBD {
private static ConexaoBD[] instances = null;
private static Integer qtdConexoes = 1;
private static Integer indice = 0;
private ConexaoBD() {}
public static ConexaoBD getInstance() {
if(instances == null) {
//Carregar qtdConexoes
qtdConexoes = carregarDados("configuracao.properties");
instances = new ConexaoBD[qtdConexoes];
for (int i = 0; i < instances.length; i++) {
instances[i] = new ConexaoBD();
}
}
indice = indice++ % qtdConexoes;
return instances[indice];
}
public static int carregarDados(String arquivo) {
Properties prop = new Properties();
try {
URL resource = ConexaoBD.class.getResource(arquivo);
if(resource == null) {
throw new RuntimeException("Nao pode achar recurso: " + arquivo);
}
prop.load(new BufferedInputStream(resource.openStream()));
} catch(Exception ex ) {
System.err.println(ex.getMessage());
}
String qtdConexoesStr = prop.getProperty("qtdConexoes", qtdConexoes.toString());
return Integer.parseInt(qtdConexoesStr);
}
}
instances = new ConexaoBD[qtdConexoes];
for (int i = 0; i < instances.length; i++) {
instances[i] = new ConexaoBD();
}
}
indice = indice++ % qtdConexoes;
return instances[indice];
}
public static int carregarDados(String arquivo) {
Properties prop = new Properties();
try {
URL resource = ConexaoBD.class.getResource(arquivo);
if(resource == null) {
throw new RuntimeException("Nao pode achar recurso: " + arquivo);
}
prop.load(new BufferedInputStream(resource.openStream()));
} catch(Exception ex ) {
System.err.println(ex.getMessage());
}
String qtdConexoesStr = prop.getProperty("qtdConexoes", qtdConexoes.toString());
return Integer.parseInt(qtdConexoesStr);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment