Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between DriverManagerDataSource and SimpleDriverDataSource

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);
like image 648
Aliuk Avatar asked Mar 20 '19 10:03

Aliuk


2 Answers

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

like image 160
IMParasharG Avatar answered Sep 28 '22 02:09

IMParasharG


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).

like image 27
stuchl4n3k Avatar answered Sep 28 '22 02:09

stuchl4n3k