Suppose I have the following code
DataSource source = (DataSource) (new InitialContext()).lookup("jdbc/myName");
Connection connnection = source.getConnection()
//use the connection to do some database operations...
at the end, should I still call
connection.close()
to release the resource?
If the connection is from a connection pool, if I don't do anything, the connection should be automatically returned to the pool, right?
On the other hand, if I close it, is there gonna be any adverse effect on the connection pool (ie, after several calls, there won't be any connection left in the pool?)
The answer is yes, and check Closing JDBC Connections in Pool and JDBC Connection Pooling Best Practices for more information.
Yes, you should be closing the connection, if only for quick recovery by the pool.
This article http://www.javaranch.com/journal/200601/JDBCConnectionPooling.html advises placement of a .close() call in a finally block.
精彩评论