Question :
In the MySQL Connector/J 8.0 Developer Guide (JDBC driver for MySQL), I have not found any mention of an implementation for the DataSource
interface in JDBC.
To quote the DataSource
Javadoc:
An alternative to the
DriverManager
facility, aDataSource
object is the preferred means of getting a connection.
So I would expect an implementation to be provided with Connector/J. For example, for this Postgres JDBC driver, I can find the implementation of DataSource
in the class PGSimpleDataSource
, as discussed on Stack Overflow.
➥ Is there no implementation of DataSource
for Connector/J? Or did I miss it?
There is this page, 6.1 Driver/Datasource Class Name. But, despite the title, mentions only the driver class name, not the class name for a DataSource
implementation.
Answer :
The driver has com.mysql.cj.jdbc.MysqlDataSource
(there is also a ConnectionPoolDataSource
and an XADataSource
implementation). The actual implementation of MysqlDataSource
uses the JDBC driver directly, similar to what DriverManager
would do.
The MySQL Connector/J hardly mentions data sources. In fact, the only reference I could find to MysqlDataSource
was in Changes in the Connector/J API. I’m not sure if it is a conscious choice by the MySQL documentation team, but it usually makes little sense to use MysqlDataSource
directly in your code (it doesn’t provide connection pooling), and MysqlConnectionPoolDataSource
and MysqlXADataSource
are not for direct usage nor are they a connection pool (instead they serve as a factory for connections for a Java EE data source that provides connection pooling and/or distributed transactions).
A lot ways of using JDBC involves using third-party data sources providing a connection pool (eg HikariCP, DBCP, c3p0), and those usually use DriverManager
directly (though some also allow you to provide a data source implementation), or through a data source of a JavaEE/JakartaEE application server. It is usually better to use those than to rely on the data source implementation of a driver.