Difference between DriverManagerDataSource and SimpleDriverDataSource
Asked Answered
V

2

7

In Spring, what is the difference between using DriverManagerDataSource and SimpleDriverDataSource for creating a new datasource given its driverClassName, url, username and password?

For example with DriverManagerDataSource you can do something like:

DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(driverClassName);
dataSource.setUrl(url);
dataSource.setUsername(username);
dataSource.setPassword(password);

While with SimpleDriverDataSource you can do:

SimpleDriverDataSource dataSource = new SimpleDriverDataSource();
Class<? extends Driver> driver = (Class<? extends Driver>) Class.forName(driverClassName);
dataSource.setDriverClass(driver);
dataSource.setUrl(url);
dataSource.setUsername(username);
dataSource.setPassword(password);
Volute answered 20/3, 2019 at 10:5 Comment(0)
L
3

From the described point of view there is little difference. The main difference is conceptual and comes into play in OSGi environemnts - citing the DriverManagerDataSource javadoc:

Within special class loading environments such as OSGi, this class is effectively superseded by SimpleDriverDataSource due to general class loading issues with the JDBC DriverManager that be resolved through direct Driver usage (which is exactly what SimpleDriverDataSource does).

Lamaism answered 20/3, 2019 at 10:23 Comment(0)
I
3

DriverManagerDataSource — Simple implementation of the standard JDBC DataSource interface, configuring the plain old JDBC DriverManager via bean properties, and returning a new Connection from every getConnection call.

SimpleDriverDataSource — Similar to DriverManagerDataSource except that it provides direct Driver usage which helps in resolving general class loading issues with the JDBC DriverManager within special class loading environments such as OSGi.

More Info

Iliad answered 20/3, 2019 at 10:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.