EDUCBA

EDUCBA

MENUMENU
  • Blog
  • Free Courses
  • All Courses
  • All in One Bundle
  • Login
Home Software Development Software Development Tutorials Spring Tutorial Spring Boot Datasource Configuration

Spring Boot Datasource Configuration

Updated April 1, 2023

Spring Boot Datasource Configuration

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.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

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.

  1. Properties config
  2. 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

1

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

2

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

Spring Boot Datasource Configuration 3

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

Spring Boot Datasource Configuration 4

  • 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

 Spring Boot Datasource Configuration 5

  • 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 ();
}
}

Spring Boot Datasource Configuration 6

  • 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

Spring Boot Datasource Configuration 7

  • 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

Spring Boot Datasource Configuration 8

  • Run the spring boot datasource configuration application –

In this step, we have run the project using the spring boot app.

9

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.
  1. 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();
}

10

  • Run the application –

11

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 –

  1. Spring Boot Scheduler
  2. Spring Boot Initializr
  3. Spring Boot Maven
  4. Spring Boot Cache
MICROSOFT POWER BI Course Bundle - 8 Courses in 1
34+ Hours of HD Videos
8 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
CYBER SECURITY & ETHICAL HACKING Course Bundle - 13 Courses in 1 | 3 Mock Tests
64+ Hours of HD Videos
13 Courses
3 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
MICROSOFT AZURE Course Bundle - 15 Courses in 1 | 12 Mock Tests
62+ Hour of HD Videos
15 Courses
12 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
KALI LINUX Course Bundle - 6 Courses in 1
20+ Hours of HD Videos
6 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
Primary Sidebar
Popular Course in this category
SPRING BOOT Course Bundle - 5 Courses in 1 | 6 Mock Tests
 20+ Hours of HD Videos
5 Courses
6 Mock Tests & Quizzes
  Verifiable Certificate of Completion
  Lifetime Access
4.5
Price

View Course
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more