DataSource configuration in Spring Boot

DataSource configuration in Spring Boot

DataSource Configuration in Spring Boot: In this post, I am going to share what is a datasource in Spring Boot and how to create and customize it.

Table of Contents

What is DataSource?

A datasource is a factory for connections to any physical data source. An alternative to the DriverManager facility. It uses a URL along with some credentials to establish a database connection.

A datasource may be used to achieve the following:

  • To create a standard Connection object
  • To create a connection for connection pooling
  • To create a connection for distributed transactions

DataSource Configuration in Spring Boot

I will show how to do datasource configuration by creating a new project using Spring Initializr. In my new project, I am including Spring Web, Spring Data JPA, and MySQL Driver dependencies or Spring Boot starters.

 Spring Boot DataSource Configuration Example
Creating a new project

Here is my pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="" xmlns:xsi=""
        <relativePath/> <!-- lookup parent from repository -->
    <description>Demo project for Spring Boot</description>




Code language: HTML, XML (xml)

If you include Spring Data JPA dependency and try to build your project without configuring datasource, your project build will fail.

The error would look something like this.

Error if you don't configure datasource
Error if you don’t configure datasource

Configuring DataSource in a new Spring Boot project

Here is an example of configuring a MySQL database connection using MySQL JDBC Driver.

In your file, add the following lines of code.

Code language: JavaScript (javascript)

Understanding the properties

If you look carefully, the code block is self-explanatory if you’ve worked with database connections in any other web-development frameworks.


The first line specifies the driver used to connect to the datasource.

The second line provides the database URL. In the example shown above, the database name is indicated as learning-spring.

The next two lines provide the username and the password of the database connection.

spring.datasource.initialization-mode can be set to either always or never. It provides the specification of whether Spring Boot should detect your database and execute those scripts on startup.

The spring.jpa.hibernate.ddl-auto property is Spring Data JPA specific and is their way to specify a value that will eventually be passed to Hibernate under the property it knows,

The values createcreate-dropvalidate, and update basically, influence how the schema tool management will manipulate the database schema at startup.

For instance, in the sample code shown above, the value used is update. In this case, on startup, constraints, and schema can be modified without deleting the earlier versions.

Further information on spring.datasource.initialization-mode and spring.jpa.hibernate.ddl-auto can be accessed from here.

Leave a Reply

Discover more from BHUTAN IO

Subscribe now to keep reading and get access to the full archive.

Continue reading

Scroll to Top