1

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?

Eyal
  • 3,412
  • 1
  • 44
  • 60
Jack
  • 5,540
  • 13
  • 65
  • 113

2 Answers2

2

DataSource (javax.sql.DataSource) represents an abstract concept of something you can get database connections from.

So, DataSource itself doesn't define any details of how connections are managed, and different implementations of DataSource may 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 case close() actually closes connections.

  • An implementation backed by a connection pool (such as Apache DBCP or c3p0) returns existing connections from the pool. Connection object returned by such an implementation is a proxy, and its close() 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.

axtavt
  • 239,438
  • 41
  • 511
  • 482
  • Hi Axtavt,Thanks your answer,I'm sorry for appending a little question,how does connectionpool keep every connection available,I think that by refreshing the connection or periodically ping the database? – Jack Oct 11 '12 at 09:16
  • @Jack: It depends on implementatin of connection pool. For Apache DBCP see the documentation link above and http://stackoverflow.com/questions/4950396/tomcat-configuration-using-dbcp – axtavt Oct 11 '12 at 09:24
0

The close() call on a connection from a datasource doesn't necessarily close the database connection. It would merely return the connection to the pool for reuse. The way this is done is, the actual connection to the database is decorated with a PooledConnection sort of class and the close() method on this PooledConnection is overridden to just mark the connection as available.

Vikdor
  • 23,934
  • 10
  • 61
  • 84
  • The `PooledConnection` represents the physical connection in the pool, it creates a logical `Connection` (eg a proxy to a normal, physical `Connection` that does something else on `Close()`). This logical connection is then handed out to the user by the datasource. – Mark Rotteveel Oct 11 '12 at 18:28