Releasing the connection
Keeping the database connection alive requires a significant number of resources, such as memory and CPU, so it is a good idea to close the connection and release the allocated resources as soon as you no longer need them. In the case of pooling, the Connection
object, when closed, is returned to the pool and consumes fewer resources.
Before Java 7, a connection was closed by invoking the close()
method in a finally
block:
try {
Connection conn = getConnection();
//use object conn here
} finally {
if(conn != null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
...