Updated April 1, 2023
Introduction to Spring Boot DataSource
In Spring boot, we have a datasource which helps us to connect where the data is kept. Datasource helps us to identify the database; in short, it is an identifier we can say which helps us to identify the source from where the data will come and in the context of any application or programming language, data always come from a database, so datasource help us to register and identify the source of data for our application.
In spring boot, we do not need to make extra configurations for datasource because that can be done in either two ways, one by properties file and another one using the explicit configuration with a custom class. Here we will see how we can register a datasource by using both approaches for better understanding and implementation.
Syntax of Spring Boot DataSource
As we already know that we can register a data source object using an application property file; let’s look at the syntax of how we can do this in spring boot.
spring.datasource.driverClassName = your driver class name spring.datasource.url = url for your database spring.datasource.username = your username spring.datasource.password = your password
As you can see in the above line of syntax, we are doing some configuration which needs to be go inside the application.properties file. Also, we have to mention the correct parameter here, which are specific to our database.
Here we will see how we can do configuration using properties file and custom class in java to make it work like normal.
How Spring Boot DataSource Works?
As we already know that spring boot we are not supposed to do a lot of configuration when it comes to datasource because the spring boot framework autoconfigures it. We only have to make a few changes to the application property file to make this work.
We will first see how we can do this using the application file; then, we will see a custom class to configure a datasource.
By using application file: In this approach, we do not have to make many configurations because it will manage by the spring boot frame work, so what we have to do is only mention the required property of the database, which will be needed in order to connect.
We will first see the different properties and discuss each of them in order to write the data source for our application.
a. application.properties file: Inside this file, we will make configurations regarding the name, pass, url and other things which will help the spring boot application to identify our data source using this property. We have to mention everything correctly; otherwise, we will get a runtime error; the database username, password, and other things need to be accurate; otherwise, we will end up with a runtime exception.
Using this property, we can specify the name of the driverClass, which will help spring boot identify which database we are using for our current project. You can use any of the available databases we have. But remember, this driverClassName should be correct, and the spring boot is able to find it; otherwise, an exception will be thrown.
This property contains the information about the database, like what is the name of the database, on which por it is running etc.
If the database does not exist, then this error:
This property allows access to the spring boot application to access our database with the mentioned username. So this user should be already created and exist on the database side. If the spring boot is not able to find it, then again, we will have the runtime exception.
If the username is wrong, then the following error will appear.
This property also allows access for spring boot application to a database if we have any password set for the specific database; also, if we do not provide this property, then we will have so many exceptions at runtime.
Below error of the password is incorrect entered:
For both the property that we have mentioned above, username and password, there is not always the possibility that we have authorization on the database side. Sometimes it may happen that we have not protected our database with any credential. so we do not require to mention this property inside the file; it should be blank; we cannot directly remove the whole.
To make this work, we have to add some more things to our project else it will give us classnotfound or other exceptions at runtime; let’s take a look at the required dependency we need to add in order to properly configure the data source using the application file in paring boot project.
1. If we are using MYSQL, then we have to add the dependency to make spring boot able to find the classes at runtime.
For this, we have MySQL dependency as below:
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency>
2. If you are using the h2 database, you may require this dependency below in our build file.
<dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <version>2.4.1</version> <scope>runtime</scope> </dependency>
3. For different databases, we have different driver classes available, which should be present in the library; else, it will not work just by mentioning the above properties inside the file; at compile-time, everything will be fine, but at runtime, it will throw an exception.
By the use of data source, we can connect to our database and fetch the data we need in our application; also, this is a very important part of the project because we need to save the data of the user somewhere, and that is a database only, which can only be done using the data source in spring boot.
This is a guide to Spring Boot DataSource. Here we discuss the introduction and how spring boot DataSource works? respectively. You may also have a look at the following articles to learn more –