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

[5231 byte] By [manuel.leiriaa] at [2007-11-27 8:24:06]
# 1
Depends on the connection pool, but I would say yes, you should close it. You should also close rs and statements prior to closing the connection.Kaj
kajbja at 2007-7-12 20:13:04 > top of Java-index,Java Essentials,Java Programming...
# 2
yes, thanks.I've made some experiences and found out that if I leave the connection open it blows up the pool in a few transactions.
manuel.leiriaa at 2007-7-12 20:13:04 > top of Java-index,Java Essentials,Java Programming...
# 3

> yes, thanks.

>

> I've made some experiences and found out that if I

> leave the connection open it blows up the pool in a

> few transactions.

Of course. What else would you expect? How would the pool be able to know that you are done with the connection if you don't signal it in any way?

kajbja at 2007-7-12 20:13:04 > top of Java-index,Java Essentials,Java Programming...
# 4

But this class (public class DB2Facade implements ReemitirDAO ) has it is doesn't work well because, if with the same instance call twice a method, in the second call a get a closed connection exception.

The

private static Connection conn;

should be a method variable instead of a instance variable and get the connection for each method instead of getting it in the constructor

manuel.leiriaa at 2007-7-12 20:13:04 > top of Java-index,Java Essentials,Java Programming...
# 5
or may be leave it as a instance variable but instead of getting the connection (conn = DB2ConnectionFactory.getConnection();) in the constructor, get it inside each method
manuel.leiriaa at 2007-7-12 20:13:04 > top of Java-index,Java Essentials,Java Programming...
# 6
> or may be leave it as a instance variable but instead> of getting the connection (conn => DB2ConnectionFactory.getConnection();) in the> constructor, get it inside each methodHe would have to make it thread local in that case.
kajbja at 2007-7-12 20:13:04 > top of Java-index,Java Essentials,Java Programming...
# 7
only if he wants to conserve the calls within a single thread ...
ejpa at 2007-7-12 20:13:04 > top of Java-index,Java Essentials,Java Programming...
# 8
> only if he wants to conserve the calls within a> single thread ...True, but he would have other problems if all methods used the attribute when creating connections, it wouldn't be thread safe in any way.
kajbja at 2007-7-12 20:13:04 > top of Java-index,Java Essentials,Java Programming...
# 9

really? if the attribute was declared inside the methods? on the stack? of the current thread?

I suppose possibly so if the connection pool returned the same connection and the connection itself wasn't thread-safe. Which would be a bit asinine of it.

But don't ask me, I'm no expert

ejpa at 2007-7-12 20:13:04 > top of Java-index,Java Essentials,Java Programming...
# 10

> really? if the attribute was declared inside the

> methods? on the stack? of the current thread?

>

> I suppose possibly so if the connection pool returned

> the same connection and the connection itself

> wasn't thread-safe. Which would be a bit asinine of

> it.

>

> But don't ask me, I'm no expert

Look at what I quoted:

> or may be leave it as a instance variable but instead

All methods would try to assign the connection that they get to the same instance variable.

kajbja at 2007-7-12 20:13:04 > top of Java-index,Java Essentials,Java Programming...
# 11

> > only if he wants to conserve the calls within a

> > single thread ...

>

> True, but he would have other problems if all methods

> used the attribute when creating connections, it

> wouldn't be thread safe in any way.

I see your point. Better declare the conn variable as method variable.

thanks

manuel.leiriaa at 2007-7-12 20:13:04 > top of Java-index,Java Essentials,Java Programming...