File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes DataSource VS Connection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "DataSource VS Connection" Watch "DataSource VS Connection" New topic
Author

DataSource VS Connection

satishbsk kumar
Greenhorn

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.
Regards,
Satish
Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874



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

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

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: 16137
    
  21

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.


Customer surveys are for companies who didn't pay proper attention to begin with.
satishbsk kumar
Greenhorn

Joined: Jul 24, 2009
Posts: 15
Thanks all for your valuable suggestions..
 
jQuery in Action, 2nd edition
 
subject: DataSource VS Connection