Welcome Message

Hi, welcome to my website. This is a place where you can get all the questions, puzzles, algorithms asked in interviews and their solutions. Feel free to contact me if you have any queries / suggestions and please leave your valuable comments.. Thanks for visiting -Pragya.

November 8, 2009

DriverManager vs DataSource

DriverManager is just that: a thin manager class for raw jdbc drivers. A DataSource is both more abstract and more powerful: it is a place where you get your database connections from. This means a couple of things. It means that a DataSource can usually be configured and managed by the application server instead of your application. It means that the connections you get from a DataSource can come from a connection pool for performance. It means that the connections may participate in a container-managed distributed transaction without you having to worry about the nitty gritty of it.
And even if your environment doesn't support DataSources, given that org.apache.struts.util.GenericDataSource can convert any old JDBC driver into a pooled DataSource I don't see why anyone would still want to use a DriverManager and a proprietary connection pool

DataSource and the DriverManager are the two basic ways to connect to a database in a JEE application. The DriverManager is older facility, DataSource is newer. It is recommended to use the new DataSource facility to connect to databases and other resources. DataSource facility has several advantages over DriverManager facility. Using DataSource increases portability. The DataSource enables connection pooling and distributed transactions, the DriverManager does not allow such techniques. Properties of a DataSource are kept in a configuration file. Any changes to the data source or database drivers are made in the configuration file. In case of a DriverManager, these properties are hard coded in the application and for any changes we must recompile the code.

No comments: