In HikariCP (and JDBC, really), what's the difference between setting a driver class name and JDBC URL versus setting a data source class name and URL property?
I ask because I'm integrating p6spy and based on what I've found, I need to define the p6spy driver as a driver class name, but when I'm using a database directly (MySQL, MariaDB, H2, etc) I defined them as data sources:
HikariConfig config = new HikariConfig();
config.setDriverClassName("com.p6spy.engine.spy.P6SpyDriver");
config.setJdbcUrl("jdbc:p6spy:mysql://localhost/test");
HikariConfig config = new HikariConfig();
config.setDataSourceClassName("com.mysql.cj.jdbc.MysqlDataSource");
config.addDataSourceProperty("url", "jdbc:mysql://localhost/test");
Am I losing functionality by using the first approach? What's the end result difference between these two definitions?
In one case the connection is created through java.sql.DriverManager
(or maybe even directly using the java.sql.Driver
implementation), and you can only use the configuration properties the driver supports that way.
When using a data source, you're using an implementation of javax.sql.DataSource
, and can only use the configuration properties the data source supports that way. If we're talking about driver-provided DataSource
implementations, there generally is no difference. They usually offer the same (or similar) configuration properties, and produce exactly the same connection as they would return through their Driver
implementation.
In theory, using a DataSource
can have some benefits because of its programmatic configuration (instead of using properties in the URL or Properties
object), and the fact you could have the DataSource
sourced from some external configuration through JNDI or other means.
However, when configured through HikariCP, and specifically in the way as shown in your question, there is no substantial difference.