Connection with connection pool
Hi,
I have this class:
publicfinalclass DB2ConnectionFactory{
publicstatic Connection getConnection()throws NamingException, SQLException{
Connection conn = getConnectionFactory().getConnection();
return conn;
}
privatestatic DataSource getConnectionFactory()throws NamingException{
DataSource ds = (DataSource)getInitialContext().lookup("jdbc/SRP");
return ds;
}
privatestatic Context getInitialContext()throws NamingException{
returnnew InitialContext();
}
}
and I use it like this:
publicclass DB2Facadeimplements ReemitirDAO{
private Connection conn;
public DB2Facade()throws SQLException, NamingException{
this.conn = DB2ConnectionFactory.getConnection();
}
publicvoid reemissaoSimples(ChequeRestituicaoVO chequeRestituicao, String username)
throws SQLException, NamingException, IllegalArgumentException{
PreparedStatement pstmt =null;
ResultSet rs =null;
boolean success =false;
try{
conn.setAutoCommit(false);
StringBuffer sqlCRTRMPG0 =new StringBuffer();
sqlCRTRMPG0
.append("INSERT INTO ...");
pstmt.execute();
pstmt.close();
success =true;
}catch (SQLException e){
throw e;
}catch (ParseException e){
thrownew IllegalArgumentException(
"A data n鉶 est?no formato correcto");
}finally{
if(conn !=null){
if(success){
logger.info("COMMIT TRANSACTION -->");
try{ conn.commit( );}
catch( SQLException e ){
try{ conn.rollback( );}
catch( SQLException ex ){throw ex;}
}
}else{
logger.info("ROLLING BACK TRANSACTION -->");
try{ conn.rollback( );}
catch( SQLException e ){throw e;}
}
}
if (rs !=null)
rs.close();
if (pstmt !=null)
pstmt.close();
if (conn !=null)
conn.close();
}
}
}
My question is: should I close the connection each time I use it or should I leave this job to the connecion pool?
thanks in advance,
Manuel Leiria

