DataSource.getConnection() vs DriverManager.getConnection()
Fast recall
Quick revision
Core answer
Whether you know DataSource.getConnection borrows from a pool while DriverManager creates a fresh physical connection each call.
Assuming both behave the same because they return a Connection, and missing that close() means different things.
Note that close() means 'return to pool' for a pooled DataSource but 'destroy the connection' for DriverManager.
Interview-ready guide
Interview answer
HikariDataSource is a pooling implementation, so getConnection borrows a logical connection and close returns it. A vendor's simple DataSource can open a fresh physical connection and close can end it. The application still receives java.sql.Connection in both cases. DataSource is preferred in applications because it is configurable, injectable, and can be managed independently of call sites—not because the interface itself promises reuse.
DataSource.getConnection() vs DriverManager.getConnection()
Direct Answer
DriverManager locates a registered JDBC Driver from a URL and asks it for a connection. DataSource is a configurable connection factory obtained directly or through a container. A DataSource may create physical connections, provide pooled logical connections, or support distributed transactions; pooling is not guaranteed by the DataSource interface. close behavior depends on the implementation.
Common Mistakes & Pitfalls
Assuming both behave the same because they return a Connection, and missing that close() means different things.
Check your understanding
Answer these without looking back at the article.
- What does close() do for a pooled connection?
- Why is DriverManager.getConnection expensive under load?
- How does a DataSource reuse connections?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.