Datasource
About
A Datasource is a fundamental concept in Java Database Connectivity (JDBC) and is the primary means for an application to obtain a connection to a relational database. It abstracts the details of the database connection and provides an interface to manage connections efficiently. Spring uses the DataSource abstraction for interacting with databases through JDBC and is a core part of the JDBC module.
What is a Datasource?
A Datasource represents a source of database connections in a Java application. Unlike DriverManager, which directly handles database connections, a DataSource is preferred because it provides better management capabilities, such as connection pooling, which improves performance by reusing database connections instead of creating new ones every time a connection is requested.
A DataSource object can be configured to provide connections to a specific database, manage connection pooling, and handle transaction management.
Types of Datasource Implementations
In the context of Spring JDBC, there are several DataSource implementations available:
BasicDataSource (Apache Commons DBCP): A simple DataSource that supports basic connection pooling.
HikariCP: A high-performance JDBC connection pool, which is the default in Spring Boot.
C3P0: Another connection pool provider, though it is less commonly used now compared to HikariCP.
Proxool: An alternative to the above for connection pooling.
Setting Up DataSource in Spring
Spring provides various ways to configure a DataSource in our application, including both XML-based configuration and Java-based configuration.
1. Java Configuration (Spring Boot / Spring Java Config)
Spring Boot automatically configures a DataSource if you provide the necessary properties in the application.properties
or application.yml
file.
In a Spring application that is not using Spring Boot, we can configure a DataSource using Java-based configuration like so:
2. XML Configuration (Traditional Spring XML Config)
In Spring, we can also configure a DataSource using XML. This was more common in earlier versions of Spring but is still useful in legacy applications.
DataSource and Connection Pooling
One of the primary features of a DataSource is that it can manage a connection pool. Connection pooling involves maintaining a pool of database connections that can be reused by multiple clients or threads, rather than opening a new connection for every operation. This results in significant performance improvements, especially in high-traffic applications.
HikariCP, the default connection pool in Spring Boot, is known for being lightweight and fast.
We can configure the size of the connection pool to suit the needs of your application (e.g., setting a maximum number of connections).
Example with HikariCP in Spring Boot:
JDBC Template with DataSource
The JdbcTemplate is Spring's high-level abstraction for interacting with a database. The JdbcTemplate internally uses a DataSource to manage connections, making database operations easier and more efficient.
Example:
In the example above, the JdbcTemplate
uses the provided DataSource to get database connections, execute the SQL query, and map the results to a list of User
objects.
Last updated
Was this helpful?