I have a little doubt about Tomcat Connection Pool using jndi/dbcp:
When I do something like it:
why I always need to close the current Connection? I was think that if I do it the connection will be closed, but I realize that it returns to the pool, right? How does it function? I call a method to close a connection and instead close it, it return to the pool.
We're pleased to have you here with us on the Ranch, but there are a few rules that need to be followed, and one is that proper names are required. Please take a look at the JavaRanch Naming Policy and adjust your display name to match it.
In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.
Well, polymorphism (dynamic method invocation) could be your answer. When you call ds.getConnection() you get an object that implements the Connection interface. What actually happens within the close() method of that implementation is up to the concrete implementation. I've not used DBCP or Tomcat connection pooling. Are you sure that what you're saying is correct?
Personally I would prefer to see a PooledConnection.release() or a DBConnectionPool.returnConnection(con) method, which would help to prevent such confusion as you're experiencing.
The close() method on a Connection retrieved from a pool releases that connection back to the pool. A close() on a non-pool connection does whatever the driver implementors felt necessary. Regardless, it's always wise to formally terminate communications, and not only on Connections. Statements should be closed as well. Normally I don't explicitly close ResultSets, since they will (per documentation) close their resultsets, but for a long-term holding, it's handy to know I can.
The best place to close Statements and Connections is in a "finally" clause. Otherwise thrown exceptions may cause resources to leak.
An IDE is no substitute for an Intelligent Developer.