Pessoal,
estou tentando fazer conexão Pool dataSource com o JayBird2.2 (banco Firebird),
e estou tendo um probleminha.
Fiz umas classes ai abaixo para testar.
As vezes ocorre esta erro quando tenta fechar, não é sempre.
Então fiz um programinha com um for para testar melhor.
O erro está aqui abaixo, e logo abaixo as classes ja prontinhas.
Obrigado,
Giovani
Erro:
“Connection org.firebirdsql.pool.PooledConnectionHandler@489c42 was closed.
See the attached exception to find the place where it was closed”
/* CLASSE PAIS */
public class Pais {
private int codigoPais = 0;
private String nomePais = null;
public void setCodigoPais(int codigoPais) {
this.codigoPais = codigoPais;
}
public int getCodigoPais() {
return codigoPais;
}
public void setNomePais(String nomePais) {
this.nomePais = nomePais;
}
public String getNomePais() {
return nomePais;
}
public String toString() {
return nomePais;
}
public boolean equals(Object o) {
if (o instanceof Pais) {
Pais pais = (Pais) o;
if (pais.getCodigoPais()== this.codigoPais){
return true;
} else {
return false;
}
} else {
return false;
}
}
}
/* CLASSE PAIS DAO */
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class PaisDB {
private final static String GET_PAIS_CODIGO = "SELECT * FROM paises WHERE cd_pais = ?";
public Pais getPorCodigo(int codigo) throws SQLException {
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
Pais pais = null;
try {
conn = ConexaoBanco.getConexao();
stmt = conn.prepareStatement(GET_PAIS_CODIGO);
stmt.setInt(1, codigo);
rs = stmt.executeQuery();
while (rs.next()) {
pais = new Pais();
pais.setCodigoPais(rs.getInt("cd_pais"));
pais.setNomePais(rs.getString("nm_pais"));
}
if (pais == null){
throw new SQLException("ATENÇÃO! Registro não encontrado");
}
} catch (SQLException e) {
throw new SQLException(e.getMessage());
} finally {
ConexaoBanco.close(conn,stmt,rs);
}
return pais;
}
}
/* CLASSE DE CONEXÃO COM O BANCO */
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.firebirdsql.pool.FBWrappingDataSource;
public class ConexaoBanco {
public static Connection getConexao () throws SQLException {
Connection con = null;
FBWrappingDataSource dataSource = new FBWrappingDataSource();
dataSource.setDatabase ("localhost/3050:D:/Banco/REGIOES.FDB");
dataSource.setDescription ("An example database of employees");
try {
dataSource.setLoginTimeout(10);
con = dataSource.getConnection ("sysdba", "masterkey");
} catch (SQLException e) {
String errorMsg = "Erro ao obter a conexão";
throw new SQLException(errorMsg+ " \n " + e.getMessage());
}finally{
}
return con;
}
public static void close(Connection con) throws SQLException {
try {
if (con != null) {
con.close();
}
} catch (SQLException e) {
String errorMsg = "SQL Não foi possivel fechar a conexão com o banco";
throw new SQLException(errorMsg+ " \n " + e.getMessage());
} catch (IllegalStateException e) {
String errorMsg = "Não foi possivel fechar a conexão com o banco";
throw new SQLException(errorMsg+ " \n " + e.getMessage());
}
}
public static void close(Connection con, Statement stmt ) throws SQLException {
try {
if (stmt != null) {
stmt.close();
}
} catch (SQLException e) {
String errorMsg = "Não foi possivel fechar o Statement com o banco";
throw new SQLException(errorMsg+ " \n " + e.getMessage());
} finally {
close(con);
}
}
public static void close(Connection con, Statement stmt, ResultSet rs) throws SQLException {
try {
if (rs != null) {
rs.close();
}
} catch (SQLException e) {
String errorMsg = "Não foi possÍVel fechar o ResultSet com o banco";
throw new SQLException(errorMsg+ " \n " + e.getMessage());
} finally {
close(con,stmt);
}
}
}
/********************************************************************************************************/
/* CLASSES PRINCIPAIS */
import java.sql.SQLException;
public class CadastroPais {
public static void main(String[] args) throws SQLException {
PaisDB paisDAO = new PaisDB();
Pais pais = new Pais();
int codigo = 1;
try {
pais = paisDAO.getPorCodigo(codigo);
System.out.println(pais.getNomePais());
} catch (SQLException e) {
System.out.print(e.getMessage());
}
}
}
/* ou esta abaixo para testar melhor, o erro */
import java.sql.SQLException;
public class CadastroPais {
public static void main(String[] args) throws SQLException {
PaisDB paisDAO = new PaisDB();
Pais pais = new Pais();
for (int i=0; i<= 3000;i++){
int codigo = 1;
try {
pais = paisDAO.getPorCodigo(codigo);
System.out.println(i + " - " + pais.getNomePais());
} catch (SQLException e) {
System.out.print(e.getMessage());
}
}
}
}