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.

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="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.bhutanio</groupId>
    <artifactId>learning-spring-boot</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>learning-spring-boot</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

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 application.properties file, add the following lines of code.

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/learning-spring
spring.datasource.username=root
spring.datasource.password=
spring.datasource.initialization-mode=always
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect
spring.jpa.hibernate.ddl-auto=update

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.

Questions?

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, hibernate.hbm2ddl.auto.

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.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *