0

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, a DataSource 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.

1 Answer 1

1

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.

3
  • 1
    Also documented on 6.3 Configuration Properties. Thanks for finding the fully-qualified class name. That is a big help. I’m still looking for the Connector/J Javadoc. Jul 12, 2020 at 16:35
  • FYI, the reason to use DataSource is not connection pooling, the reason is to externalize the database connection configuration information, to be accessed at deployment runtime via JNDI rather than hard-coding within my app. Using DataSource is recommended by Oracle/Sun as the preferred means of getting a connection. Mar 1, 2021 at 2:18
  • @BasilBourque Although true, almost all use cases benefit from using a connection pool, and then you use third-party solutions that offers a DataSource (like HikariCP, DBCP, c3p0 and others), or built-in in a Java EE/Jakarta EE application server (though those can use a DataSource, ConnectionPoolDataSource or XADataSource of the driver as factory for its connections). Mar 1, 2021 at 8:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.