Meaningless Drivel is fun!
The moose likes JDBC and Relational Databases and the fly likes DataSource VS Connection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "DataSource VS Connection" Watch "DataSource VS Connection" New topic

DataSource VS Connection

satishbsk kumar

Joined: Jul 24, 2009
Posts: 15
Hi All,
What is the difference between connection obtained by using DataSource and connection got from DriverManager.Which one is effective.
Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874

As far as connection itself is concerned , there shouldn't be any difference. However Datasource has several advantages over DriverManager , which should be preferred.

Can be used as JNDI service
enables to maintain Connection pools.
Jaydeep Vaishnav

Joined: Sep 08, 2009
Posts: 16
When we have a scenario of multiple hits on the database at the same time, Datasource is preferrable over the DriverManager. Driver manager reduces the overall performance and can be a bad experience on the client side. While datasource uses connection pooling and manages resouces effectively. Many details are hidden in the statement but as far as the answer to your question is concerned, DataSource should be preffered.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 17421

Typically, a Connection returned from a DataSource aggregates on a DriverManager Connection opened by the DataSource itself. Almost all the methods on the aggregating connection are passed along to the fronted Connection, with one exception. The close() method returns the Connection to the DataSource connection pool instead of actually closing the connection. That way the Connection will be ready to use when it is again retrieved from the pool. Rigorous implementations will also ensure that before the Connection is handed out again it's reset to a known state, but it's better not to assume that.

This is while it's critical to intercept all JDBC errors and make sure you have explicitly closed all connections, including those where the exception would otherwise fly by the close done by non-error code. Otherwise you'll leak connections.

An IDE is no substitute for an Intelligent Developer.
satishbsk kumar

Joined: Jul 24, 2009
Posts: 15
Thanks all for your valuable suggestions..
I agree. Here's the link:
subject: DataSource VS Connection
jQuery in Action, 3rd edition