How to execute asynchronous query with Spring JPA and PostgreSQL | Spring Boot


In tradition approach, implementing Data Access Layer makes lots of boilerplate code. Spring Data provides us Spring JPA which supports the ways to write interface for repositories and custom finder methods. The implementation will be done automatically by Spring Framework.

The tutorial shows you how to execute asynchronous query with Spring JPA and PostgreSQL using Spring Boot.

Related articles:
Java 8 Streams
How to use Spring JPA MySQL | Spring Boot
How to use Spring JPA with PostgreSQL | Spring Boot
How to get streaming results with Spring JPA, Java 8 Stream and PostgreSQL | Spring Boot
@DataJPATest with Spring Boot

I. Technology

– Java 1.8
– Maven 3.3.9
– Spring Tool Suite – Version 3.8.1.RELEASE
– Spring Boot: 1.5.1.RELEASE

II. Overview
1. Project Structure

– Class Customer corresponds to entity and table customer, it should be implemented Serializable.
CustomerRepository is an interface extends CrudRepository, will be autowired in WebController for implementing repository methods and custom finder methods.
WebController is a REST Controller which has request mapping methods for RESTful requests such as: save, findall, findonebyid, findbylastname.
– Configuration for Spring Datasource and Spring JPA properties in
Dependencies for Spring Boot and PostgreSQL in pom.xml

2. Step to do

– Create Spring Boot project & add Dependencies
– Configure Spring JPA
– Create DataModel Class
– Create Spring JPA Repository Interface
– Create Web Controller
– Create PostGreSQL table
– Run Spring Boot Application & Enjoy Result

III. Practice
1. Create Spring Boot project & add Dependencies

Open Spring Tool Suite, on Menu, choose File -> New -> Spring Starter Project, then fill each fields.
Click Next, in SQL: choose JPA and PostgreSQL, in Web: choose Web.
Click Finish, then our project will be created successfully.

Open pom.xml and check Dependencies:

These dependencies were auto-generated by the configuration we have done before.

2. Configure Spring JPA


3. Create DataModel Class

Under package model, create class Customer.

Content of

Annotation @Entity indicates that Customer is an Entity and @Table specifies the primary table (name customer) for the annotated @Entity.

@ID specifies the primary key and @GeneratedValue indicates generation strategy for value of primary key.

@Column: mapped column (in the table) for persistent fields (in Java class).

We have 2 constructor methods:
protected constructor will be used by Spring JPA.
public constructor is for creating instances.

4. Create Spring JPA Repository Interface

This interface helps us do all CRUD functions for class Customer.
If we the query method is executed asynchronously, just annotate it with @Async annotation and return a kind of Future object:

5. Create Web Controller

The controller receives requests from client, using repository to update/get data and return results.

Content of

For the methods which are annotated by @RequestMapping, we have used some methods of autowired repository which are implemented interface CrudRepository:

6. Create PostGreSQL table

Open pdAdmin III, use SQL Editor and make a query to create customer table:

7. Run Spring Boot Application & Enjoy Result

– Config maven build:
clean install
– Run project with mode Spring Boot App
– Check results:

Request 1
The browser returns Done and if checking database testdb with table customer, we can see some data rows has been added:

Request 2

Request 3

Request 4

IV. Source Code


By grokonez | February 9, 2017.

Last updated on September 11, 2018.

Related Posts

7 thoughts on “How to execute asynchronous query with Spring JPA and PostgreSQL | Spring Boot”

    1. Hi Maxim,

      Apply @Async annotation with a method of a bean will make it execute in a separate thread (caller does not need to wait for the completion of @Async method).

      By default, Spring uses a SimpleAsyncTaskExecutor to run the methods asynchronously. You can also override the Executor to run methods annotated with @Async.


      1. That’s true, the @Async methods will execute in a separate thread, but then in the WebController you’re calling get() which will wait for the asynchronous call to complete and essentially defeats the purpose of asynchronicity. Or do I understand it wrong?

        1. Hi Wojtek,

          You’re right 🙂
          To get benefit, we will work with ‘future’ before calling get() method.


  1. Hi JSA,

    Thanks for your post. I have few questions. I don’t have much knowledge about multi-threading so I’m sorry if I asked you some stupid question.

    1) you are calling repository method from controller (scope request) for each user (HTTP-request) Spring will create separate thread so if two requests at same time updating save row in DB then what will happened?

    2) If we use Service class and Controller call repository from service (scope singleton share with all the resources) then no benefit of multi threading right?

    3) Why you didn’t share about data-source connection pooling it’s very import with this example right ?

    4) I want to override TaskExecutor with my own pool size. what should be my pool size ? If I’m not wrong it depends upon our machine Cores. Please explain.

    5) If possible then please let us know Future.addCallBack example with you above example.


Got Something To Say:

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