I wondered how works method getConnection() at DataSource? I assume that DataSource calling DriverManager.getConnection every time when we call DataSource.getConnection with our properties that we setting in DataSource. Is that true?
Advertisement
Answer
The answers to your question can be deduced from the DataSource
javadoc.
“The
DataSource
interface is implemented by a driver vendor. There are three types of implementations:
- Basic implementation — produces a standard
Connection
object- Connection pooling implementation — produces a
Connection
object that will automatically participate in connection pooling. This implementation works with a middle-tier connection pooling manager.- Distributed transaction implementation — produces a
Connection
object that may be used for distributed transactions and almost always participates in connection pooling. This implementation works with a middle-tier transaction manager and almost always with a connection pooling manager.“
Thus:
I wondered how works method
getConnection()
atDataSource
?
It is vendor specific, and depends on the type of implementation that the vendor provides.
I assume that
DataSource
callingDriverManager.getConnection
every time when we callDataSource.getConnection
with our properties that we setting inDataSource
. Is that true?
Not necessarily. For example, DataSource.getConnection()
could return the same Connection
object each time it is called. Or more plausibly, it could return a new Connection
proxy for an underlying database pool connection that has been recycled. Furthermore, that DriverManager
method is not necessarily called to get the connection.
If you want to know how a specific DataSource
works, you would need to look at the vendor documentation … or alternatively its source code.