What is a DataSource in java? Can someone please explain me in simple language?
DataSource implementation classes allow you to use connection pool and loose coupling for connectivity .
Most of the times we are looking for loose coupling for connectivity so that we can switch databases easily.
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.
Data Source is explained very well here. Please go through the link Data Source in Java
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,
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,
getConnection()
and getConnection(String username, String password)
where both return the Connection
object ( read ). According to documentation , the DataSouce
interface is something like DriverManager
. In most applications, it is connection to the database.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.