Tuesday, August 14, 2007

jdbc connection , datasource connection, connection pooling

Both are database connections. A connection is a handle to database. When you are directly creating a connection by calling Drivermanager.getConnection(..) , you are creating a connection by yourself and when closing close() on it, the link to database is lost. On the other hand when you get a connection from a datasource, when you call the close() on it, it will not close the link to database, but will return to a connection pool where it can be reused by some other classes. It is always better to use a connection pool because creating connections are expensive

DriverManager.getConnection() literally creates, that is, builds a connection to the database using the values you previously supplied when you loaded the driver.

A connection pool is an object that contains several already made connections to the database, and simply provides you with one of those existing connections. The confusion arises because a) the method name is frequently the same and b) the connection pool object calls the driver's getConnection() method to create several connections before lending any of them out.

In other words:

DriverManager.getConnection() builds a connection to the database.

ConnectionPool.getConnection() fetches an existing connection.

If you use DriverManager.getConnection(), you are indeed bypassing the Connection Pool entirely.

No comments:

Topics