---
title: "Discussion on \"Implementing Automatic Retry for Database Deadlocks in Spring Boot using Spring Retry\" | Hashnode"
description: "Discussion on \"Implementing Automatic Retry for Database Deadlocks in Spring Boot using Spring Retry\". Overview\n\nLack of experience in handling deadlocks in databases can lead to panic during the most critical moments when a production-level system faces a high influx of customers. No matter how clearly you write business logic or how thoroughly you c..."
url: "https://hashnode.com/posts/implementing-automatic-retry-logic-for-database-deadlocks-in-spring-boot-using-spring-retry/65a625f497a98f4d89f778b1"
---

[Feed](https://hashnode.com/feed) /

Discussion

# [Implementing Automatic Retry for Database Deadlocks in Spring Boot using Spring Retry](https://jsonobject.com/implementing-automatic-retry-logic-for-database-deadlocks-in-spring-boot-using-spring-retry?utm_source=hashnode&utm_medium=feed)

Overview Lack of experience in handling deadlocks in databases can lead to panic during the most critical moments when a production-level system faces a high influx of customers. No matter how clearly you write business logic or how thoroughly you c...

Jan 16, 2024 · 4 min read · jsonobject.com  
Updated Apr 12, 2024  
Read the article

-   [#springboot](https://hashnode.com/tag/springboot)
-   [#spring-data-jpa](https://hashnode.com/tag/spring-data-jpa)

## Written by

[Taehyeong Lee](https://hashnode.com/@jsonobject)

@jsonobject · Seoul, Korea

  
[View profile](https://hashnode.com/@jsonobject)

Simple is best

## Responses 2

Markdown supported · ⌘↵ to post

[HyeongSu](https://hashnode.com/@amecoder)

[Jan 16, 2024](https://hashnode.com/posts/implementing-automatic-retry-logic-for-database-deadlocks-in-spring-boot-using-spring-retry/65a625f497a98f4d89f778b1/comment/65a64c5368bd11c4ade3b79e)

Thanks to you, I was able to think deeply about lock and isolation. Thank you!

You have set the @Transactional annotation on the Repository interface. I usually prefer to declare it in the Class method of the Service layer... Is it a better practice to declare it in the Repository?

[Taehyeong Lee](https://hashnode.com/@jsonobject)

[Jan 16, 2024](https://hashnode.com/posts/implementing-automatic-retry-logic-for-database-deadlocks-in-spring-boot-using-spring-retry/65a625f497a98f4d89f778b1/comment/65a64c5368bd11c4ade3b79e#reply-65a66add84f4ade3e636a37d)

Of course, it is most commonly used in concrete classes at the Service level :) In this example, I wanted to introduce you to the lowest level where you can use @Transactional in your project, so I showed you an example of specifying it in the @Repository interface. I make it a habit to specify @Repository and @Service, because that way I can cope with both transactions from real-time APIs with low execution time, and logic that has hours of execution time and intentionally doesn't use service-level transactions.

[HyeongSu](https://hashnode.com/@amecoder)

[Jan 17, 2024](https://hashnode.com/posts/implementing-automatic-retry-logic-for-database-deadlocks-in-spring-boot-using-spring-retry/65a625f497a98f4d89f778b1/comment/65a64c5368bd11c4ade3b79e#reply-65a738bf76f96775fb687fb9)

[Taehyeong Lee](https://hashnode.com/@jsonobject) Thank you for the response. I also plan to develop a habit of doing so. By establishing a solid foundation, it seems possible to preemptively prevent deadlocks that may occur in unexpected areas.

[HyeongSu](https://hashnode.com/@amecoder)

[Jan 16, 2024](https://hashnode.com/posts/implementing-automatic-retry-logic-for-database-deadlocks-in-spring-boot-using-spring-retry/65a625f497a98f4d89f778b1/comment/65a6446acd3d656b75b71db5)

Thanks to you, I was able to contemplate about lock and isolation.

In source code, you implemented the @Transactional annotation in the Repository Interface, but according to Spring documentation, they recommend setting it at the class level.

how do you think about it?

[docs.spring.io/spring-framework/reference/data-ac…](https://docs.spring.io/spring-framework/reference/data-access/transaction/declarative/annotations.html#:~:text=The%20Spring%20team,a%20rollback%20scenario.)

\---- I discovered this after making the comment ---- It seems that the Repository with Data JPA interface is an exception where it's permissible to use Transactional.

[Taehyeong Lee](https://hashnode.com/@jsonobject)

[Jan 16, 2024](https://hashnode.com/posts/implementing-automatic-retry-logic-for-database-deadlocks-in-spring-boot-using-spring-retry/65a625f497a98f4d89f778b1/comment/65a6446acd3d656b75b71db5#reply-65a66e84094772a9b1b2db89)

It also works well to specify @Transactional in a custom method of the Spring Data JPA interface, as in this example. This brings a lot of convenience to the developer, as they don't have to re-write each custom query with @Transactional in an outer contained class. In typical real-time business logic, most of the time the transaction propagation setting will use the default value of Propagation.REQUIRED, so you can also naturally carry over transactions created at higher Service levels.
