What is a datasource in java? Can someone please explain me in simple language?
Asked Answered
U

3

10

What is a DataSource in java? Can someone please explain me in simple language?

Unsung answered 3/4, 2017 at 9:39 Comment(1)
Have you actually read the apidoc of javax.sql.DataSource? What is unclear about that?Bashee
P
4

DataSource implementation classes allow you to use connection pool and loose coupling for connectivity.

  1. Most of the times we are looking for loose coupling for connectivity so that we can switch databases easily.

  2. Creating connections can be heavy process and it is not a good idea to let every part of program create its own connections which can lead to resource starvation and slow performance. that's why we use connection pooling. most database drivers provide datasource implementation classes that can be used in connection pool.

Pavlish answered 3/4, 2017 at 10:25 Comment(0)
M
2

DataSource is an abstraction, a way to represent getting access to data from some source with some connection. It frees you to not worry about,

  1. Having to think about how connection is established and will be maintained from many varying sources (think different databases, cloud storage resources)
  2. Not worrying about what will happen if multiple clients request data from the same source and if those clients will get a proper connection object, or null - because the number of connections, there optimization and scheduling is a challenge on its own.
  3. In the same respect, easier to manage connection timeout settings.

Database vendors and data providers need not worry about you learning their specific API if they can go ahead and implement the DataSource interface which describes how your service/server may connect to them, and you need not worrying about connection intricacies and so on.

Here is a good read from the official documentation itself on Oracle.

To answer your main question, I'll summarize in the below 3 points,

  1. An interface to be implemented by a vendor
  2. A factory pattern for connections to the actual data source
  3. Its main functions to be implemented and of concern are just getConnection() and getConnection(String username, String password) where both return the Connection object (read).
Marthamarthe answered 9/9, 2022 at 18:21 Comment(0)
T
-5

According to documentation, the DataSouce interface is something like DriverManager. In most applications, it is connection to the database.

Thurber answered 3/4, 2017 at 9:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.