Updated April 1, 2023
Definition of Spring Boot Datasource Configuration
Spring boot datasource configuration is nothing but the factory of connection which was used in a physical data source. Spring boot datasource uses the database credential to set up connections between the database server, it is alternative to the facility of Driver Manager. Basically, the driver manager is used to obtain the standard object of connection which was used in the connection string. We can also use spring boot datasource connection in connection pooling.
Overview of Spring Boot Datasource Configuration
- Spring boot will automatically configure the connection pool by using apache tomcat, HikariCP, or by using common DBCP, we can choose it by using the classpath.
- We can use the spring boot datasource in the connection object which was standard. We can also use spring boot datasource connection in connection pooling which was used in distributed transactions.
We can define by using two ways are as follows.
- Properties config
- Java config
- Data source auto-configuration is checking for the class of data source or classpath which was configured by the data source bean.
- We need to add spring boot starter data JPA (spring-boot-starter-data-jpa) dependency to develop a project by using spring boot datasource configuration.
- While adding spring boot starter data JPA dependency (spring-boot-starter-data-jpa) it will add all the dependencies including all databases jdbc driver. For example, we need to add MySQL-connector-java dependency for connecting to the MySQL database server, after adding spring boot starter data JPA dependency it will automatically add a mysql-connector-java dependency, we have no need to add it separately.
- We have provided the external configuration by using the application.properties file.
- the properties file is used to decouple the configuration from the code of an application. Using application. properties file we can import the datasource configuration by using the system configuration provider.
- In the project, we can create a datasource bean by using DataSourceBuilder class, which was annotated by @configuration.
- As well as spring boot datasource is also used a connection pool. We can also do it by creating JNDI in our project of spring boot datasource.
- To define connection pooling by using datasource configuration spring boot is first verifying the available driver class.
Datasource configuration
Below is the example which is as follows.
-
Create project template using spring initializer and give name to project –
In the below step, we have provided project group name as com. example, artifact name as springbootdatasourceconfiguration, project name as springbootdatasourceconfiguration, and selected java version as 8.
Group – com. example
Artifact name – springbootdatasourceconfiguration
Name – springbootdatasourceconfiguration
Spring boot – 2.6.0
Project – Maven
Project Description – Project for springbootdatasourceconfiguration
Java – 8
Dependencies – spring data JPA
Package name – com.example.springbootdatasourceconfiguration
-
After generating project extract files and open this project by using spring tool suite –
After generating the project by using spring initializer in this step we are extracting the jar file and opening the project by using the spring tool suite.
-
After opening project using spring tool suite check the project and its files –
In this step, we are checking all the project template files. We also need to check the maven dependencies and system libraries.
-
Add dependency packages –
In this step, we are adding the required dependency to our project.
Code:
<dependency> -- Start of dependency tag.
<groupId>org.springframework.boot</groupId> -- Start and end of groupId tag.
<artifactId>spring-boot-starter-data-jpa</artifactId>-- Start and end of artifactId tag.
</dependency>-- End of dependency tag.
-
Configure application. properties file –
Code:
# PostgreSQL
spring.datasource.url = jdbc:postgresql://localhost:5432/datasource
spring.datasource.username = postgres
spring.datasource.password = postgres
spring.datasource.driver-class-name = com.postgresql.jdbc.Driver
-
Create datasource bean –
In this step, we have created the datasource bean of the spring boot datasource configuration project. We have created the class name as datasourceConfig.
Code –
@Configuration
public class datasourceConfig {
@Bean
public DataSource getDataSource ()
{
DataSourceBuilder dsBuilder = DataSourceBuilder.create ();
dsBuilder.driverClassName ("com.postgresql.jdbc.Driver");
dsBuilder.url ("jdbc:postgresql://localhost:5432/datasource");
dsBuilder.username ("postgres");
dsBuilder.password ("postgres");
return (DataSource) dsBuilder.build ();
}
}
-
Define JNDI for datasource –
In the below step, we have defined the JNDI name for our project.
Code –
spring.datasource.jndi-name = java:jboss/datasources/datasource
-
Configure connection pooling for spring boot datasource configuration –
In this step, we have configured the connection pooling for the project.
Code –
spring.datasource.dbcp2.initial-size = 100
spring.datasource.dbcp2.max-idle = 100
spring.datasource.dbcp2.default-query-timeout = 1000
spring.datasource.dbcp2.default-auto-commit = true
-
Run the spring boot datasource configuration application –
In this step, we have run the project using the spring boot app.
Two Test Configuration additional
- To test the additional configuration we have configured the two datasource in a single class.
- While using autowired spring boot will use primary datasource to use secondary datasource we need to use @Qualifier annotation.
- The below example shows two test configurations are as follows.
-
Configuration of two test additional –
Code:
@Bean(name = "myDataSource")
@Primary
public DataSource myDataSource()
{
DataSourceBuilder dsBuilder = DataSourceBuilder.create();
dsBuilder.url("jdbc:mysql://localhost/dataDB");
dsBuilder.username("mysql");
dsBuilder.password("mysql");
return (DataSource) dsBuilder.build();
}
-
Run the application –
Data Source Configuration File
- To configure spring boot datasource configuration we are using the dataSourceConfiguration_r3_roles_db.xml, this is only the configuration option available to configure the data source.
- We cannot change this configuration file to any other configuration file. While upgrading the system we required old configuration files.
- Below are the other configuration files for the application server are as follows.
- XML – This file contains the user info regarding creating, modify and read users in AS ABAP system.
- XML – This file contains only UME users from the AS ABAP system. Using this file we can only create and modify new users in the local java database.
- XML – This file contains the user info regarding creating, modify and read users in AS ABAP system.
- The file dataSourceConfiguration_r3_roles_db.xml is equivalent to the dataSourceConfiguration_r3_roles_db.xml file. That file contains the difference only in upgrade compatibility.
Conclusion
Spring boot will use the algorithm name as opinionated to configure and scam the datasource configuration of spring boot. The opinionated algorithm allows us to easily get a datasource configuration that was fully configured by default.
Recommended Articles
This is a guide to Spring Boot Datasource Configuration. Here we discuss the definition, overviews, Two Test Configuration additional examples with code implementation. You may also have a look at the following articles to learn more –