I’m using org.apache.commons.dbcp.BasicDataSource as my datasource implementation, my code geting connection and closing the connection like this:
Connection conn = dataSource.getConnection();
when I finished the connection work I will close it
conn.close();
My question is: the conn.close() is really close, so when the connection be closed like conn.close(), how is datasource doing. I heard that the datasource connection close is not really close, just is release, but I can’t find the release API from datasource class. I want to know how does datasource manage the creation, close and release of database connection.
By the way a little question: how does datasource refresh the connection, I mean if the connections of the datasource haven’t been used for one year, how does datasource keep the connections available?
DataSource(javax.sql.DataSource) represents an abstract concept of something you can get database connections from.So,
DataSourceitself doesn’t define any details of how connections are managed, and different implementations ofDataSourcemay manage connections in different ways:A naive implementation (such as Spring’s
DriverManagerDataSource) may create a new connection each time you request it, and in this caseclose()actually closes connections.An implementation backed by a connection pool (such as Apache DBCP or c3p0) returns existing connections from the pool.
Connectionobject returned by such an implementation is a proxy, and itsclose()method is overriden to return connection to the pool instead of closing it.If you want to know how exactly your connection pool manages connections, check documentation of your connection pool implementation.