Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do we use a DataSource instead of a DriverManager?

I am reading the Java JDBC specification (vr. 4) and I encountred this statement:

DataSource — this interface was introduced in the JDBC 2.0 Optional Package API. It is preferred over DriverManager because it allows details about the underlying data source to be transparent to the application

What I am trying to understand is what the difference is between a Connection and a DataSource, and why it exists. I mean, the block above says that the details about a datasource is transparent to the application, but wouldn't externalizing database properties such as username, password, url etc in a property file and then use DriverManager work in the same way?

And is the DataSource interface created only to have a common way of returning connections that can be pooled etc? In Java EE, does the application server implement this interface and the applications deployed to have a reference to a datasource instead of a connection?

like image 452
LuckyLuke Avatar asked Mar 04 '13 09:03

LuckyLuke


People also ask

What is difference between DataSource and DriverManager?

DataSource and the DriverManager are the two basic ways to connect to a database. 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.

Why do we use DataSource?

A DataSource object holds the credentials needed to get a connection to the database. Typically, a DataSource holds a user name recognized by the database server, a password for that user, and various settings to customize any future sessions with the database.

What is difference between JNDI and DataSource?

A JNDI DataSource object is a file that contains the configuration details necessary to connect to a database. The DataSource object must be registered on a JNDI server, where it is identified using a JNDI name. You can register your DataSource object directly on your application server via its JNDI service.

What is a DataSource in JDBC?

A DataSource object provides a new way for JDBC clients to obtain a DBMS connection. To create a DataSource object you define it with an entry in the weblogic. properties file. This DataSource entry then points to a connection pool that is also defined in the weblogic.


2 Answers

Better scalability and maintenance

For DriverManager you need to know all the details (host, port, username, password, driver class) to connect to DB and to get connections. Externalizing those in a properties file doesn't change anything about the fact that you need to know them.

Using a DataSource you only need to know the JNDI name. The AppServer cares about the details and is not configured by the client application's vendor, but by an admin where the application is hosted.

Scalability:

Suppose you need to create connections yourself, how would you deal with changing load, sometime you have 10 users sometime you have 1000, you can't just get a connection whenever you need one and later 'release' it so the Database server does not get out of connections, which leads you to connection pooling. DriverManager does not provide it, DataSource does.

If you are going to program a connection pool yourself then you have to use DriverManager, otherwise go with DataSource.

like image 158
A4L Avatar answered Sep 21 '22 20:09

A4L


DriverManager

  • hampers the application performance as the connections are created/closed in java classes.
  • does not support connection pooling.

DataSource

  • improves application performance as connections are not created/closed within a class, they are managed by the application server and can be fetched while at runtime.
  • it provides a facility creating a pool of connections
  • helpful for enterprise applications
like image 44
NaveenKumar1410 Avatar answered Sep 22 '22 20:09

NaveenKumar1410