eBook – Guide Spring Cloud – NPI EA (cat=Spring Cloud)
announcement - icon

Let's get started with a Microservice Architecture with Spring Cloud:

>> Join Pro and download the eBook

eBook – Mockito – NPI EA (tag = Mockito)
announcement - icon

Mocking is an essential part of unit testing, and the Mockito library makes it easy to write clean and intuitive unit tests for your Java code.

Get started with mocking and improve your application tests using our Mockito guide:

Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Reactive – NPI EA (cat=Reactive)
announcement - icon

Spring 5 added support for reactive programming with the Spring WebFlux module, which has been improved upon ever since. Get started with the Reactor project basics and reactive programming in Spring Boot:

>> Join Pro and download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Jackson – NPI EA (cat=Jackson)
announcement - icon

Do JSON right with Jackson

Download the E-book

eBook – HTTP Client – NPI EA (cat=Http Client-Side)
announcement - icon

Get the most out of the Apache HTTP Client

Download the E-book

eBook – Maven – NPI EA (cat = Maven)
announcement - icon

Get Started with Apache Maven:

Download the E-book

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

eBook – RwS – NPI EA (cat=Spring MVC)
announcement - icon

Building a REST API with Spring?

Download the E-book

Course – LS – NPI EA (cat=Jackson)
announcement - icon

Get started with Spring and Spring Boot, through the Learn Spring course:

>> LEARN SPRING
Course – RWSB – NPI EA (cat=REST)
announcement - icon

Explore Spring Boot 3 and Spring 6 in-depth through building a full REST API with the framework:

>> The New “REST With Spring Boot”

Course – LSS – NPI EA (cat=Spring Security)
announcement - icon

Yes, Spring Security can be complex, from the more advanced functionality within the Core to the deep OAuth support in the framework.

I built the security material as two full courses - Core and OAuth, to get practical with these more complex scenarios. We explore when and how to use each feature and code through it on the backing project.

You can explore the course here:

>> Learn Spring Security

Course – LSD – NPI EA (tag=Spring Data JPA)
announcement - icon

Spring Data JPA is a great way to handle the complexity of JPA with the powerful simplicity of Spring Boot.

Get started with Spring Data JPA through the guided reference course:

>> CHECK OUT THE COURSE

Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – Summer Sale 2026 – NPI EA (cat= Baeldung)
announcement - icon

Yes, we're now running our only Summer Sale. All Courses are 30% off until 20th July, 2026:

>> EXPLORE ACCESS NOW

Course – Summer Sale 2026 – NPI (cat=Baeldung)
announcement - icon

Yes, we're now running our only Summer Sale. All Courses are 30% off until 20th July, 2026:

>> EXPLORE ACCESS NOW

1. Introduction

In this article, we’ll examine the compatibility between the @Transactional and @Async annotations of the Spring framework.

2. Understanding @Transactional and @Async

The @Transactional annotation creates an atomic code block from many others. So, if one block is completed exceptionally, all the parts roll back. Hence, the newly created atomic unit is only completed successfully with a commit when all its parts are successful.

Creating transactions allows us to avoid partial failures in our code, improving data consistency.

On the other hand, @Async tells Spring that the annotated unit can run in parallel with the calling thread. In other words, if we call an @Async method or class from a thread, Spring runs its code in another thread with a different context.

Defining asynchronous code can improve execution time performance by executing units in parallel with the calling thread.

There are scenarios in which we need both performance and consistency in our code. With Spring, we can mix @Transactional and @Async to achieve those two goals, as long as we pay attention to how we use the annotations together.

In the following sections, we’ll explore different scenarios.

3. Can @Transactional and @Async Work Together?

Asynchronous and transactional code might introduce problems like data inconsistency if we don’t implement them properly.

It’s fundamental to pay attention to Spring’s transactional context and data propagation between contexts to fully take advantage of @Async and @Transactional and avoid pitfalls.

3.1. Creating the Demo Application

We’ll use the transfer functionality from a banking service to illustrate the use of transactions and asynchronous code.

In short, we can implement money transfers by removing money from one account and adding it to another. We can picture that as database operations like selecting the involved accounts and updating their money balance:

public void transfer(Long depositorId, Long favoredId, BigDecimal amount) {
    Account depositorAccount = accountRepository.findById(depositorId)
      .orElseThrow(IllegalArgumentException::new);
    Account favoredAccount = accountRepository.findById(favoredId)
      .orElseThrow(IllegalArgumentException::new);

    depositorAccount.setBalance(depositorAccount.getBalance().subtract(amount));
    favoredAccount.setBalance(favoredAccount.getBalance().add(amount));

    accountRepository.save(depositorAccount);
    accountRepository.save(favoredAccount);
}

We start by finding the involved accounts using findById() and throw an IllegalArgumentException if the given ID doesn’t find the account.

Then, we update the retrieved account with the new amount. Finally, we save the newly updated account using CrudRepository‘s save() method.

In this simple example, there are a few potential failures. For instance, we might not find the favoredAccount and fail with an exception. Or, the save() operation completes for depositorAccount but fails for favoredAccount. Those are defined as partial failures because what happened before the failure can’t be undone.

Hence, partial failures create data consistency problems if we don’t manage our code properly with transactions. For instance, we might remove money from an account without effectively passing it to another.

3.2. Calling @Transactional From @Async

If we call the @Transactional method from the @Async method, Spring correctly manages the transaction and propagates its context, ensuring data consistency.

For instance, let’s call a @Transactional transfer() method from an @Async caller:

@Async
public void transferAsync(Long depositorId, Long favoredId, BigDecimal amount) {
    transfer(depositorId, favoredId, amount);

    // other async operations, isolated from transfer
}
@Transactional
public void transfer(Long depositorId, Long favoredId, BigDecimal amount) {
    Account depositorAccount = accountRepository.findById(depositorId)
      .orElseThrow(IllegalArgumentException::new);
    Account favoredAccount = accountRepository.findById(favoredId)
      .orElseThrow(IllegalArgumentException::new);

    depositorAccount.setBalance(depositorAccount.getBalance().subtract(amount));
    favoredAccount.setBalance(favoredAccount.getBalance().add(amount));

    accountRepository.save(depositorAccount);
    accountRepository.save(favoredAccount);
}

The transferAsync() method runs in parallel with the calling thread in a different context since it’s @Async.

Then, we call the transactional transfer() method to run the crucial business logic. In that case, Spring correctly propagates the transferAsync() thread context to transfer(). Hence, we don’t lose any data in that interaction.

The transfer() method defines a set of crucial database operations that must be rolled back if something fails. Spring only handles the transfer() transaction, which isolates all code outside the transfer() body from the transaction. Therefore, Spring only rolls back the transfer() code if something fails.

Calling a @Transactional from an @Async method can be useful for improving performance by executing operations in parallel with the calling thread without having data inconsistencies in specific internal operations.

3.3. Calling @Async From @Transactional

Spring currently uses ThreadLocal to manage the current thread transaction. Hence, it doesn’t share thread contexts between different threads of our application.

Thus, if a @Transactional method calls an @Async method, Spring doesn’t propagate the same thread context of the transaction.

To illustrate, let’s add a call to the async printReceipt() method inside transfer():

@Async
public void transferAsync(Long depositorId, Long favoredId, BigDecimal amount) {
    transfer(depositorId, favoredId, amount);
}
@Transactional
public void transfer(Long depositorId, Long favoredId, BigDecimal amount) {
    Account depositorAccount = accountRepository.findById(depositorId)
      .orElseThrow(IllegalArgumentException::new);
    Account favoredAccount = accountRepository.findById(favoredId)
      .orElseThrow(IllegalArgumentException::new);

    depositorAccount.setBalance(depositorAccount.getBalance().subtract(amount));
    favoredAccount.setBalance(favoredAccount.getBalance().add(amount));

    printReceipt();
    accountRepository.save(depositorAccount);
    accountRepository.save(favoredAccount);
}
@Async public void printReceipt() { // logic to print the receipt with the results of the transfer }

The transfer() logic is the same as earlier, but now we call printReceipt() to print the transfer result. Since printReceipt() is @Async, Spring runs its code on a different thread with another context.

The problem is that the receipt information depends on correctly executing the entire transfer() method. Additionally, printReceipt() and the rest of the transfer() code that saves into the database runs on different threads with different data, making the application behavior unpredictable. For example, we may print the result of a money transfer transaction that wasn’t successfully saved into the database.

Thus, to avoid that kind of data consistency problem, we must avoid calling an @Async method from a @Transactional since thread context propagation doesn’t occur.

3.4. Using @Transactional at the Class Level

Defining a class with @Transactional makes all its public methods available for Spring transaction management. Thus, the annotation creates transactions for all methods at once.

One thing that might occur when using @Transactional at the class level is mixing it with @Async in the same method. In practice, we’re creating a transactional unit around that method that runs in a thread different from the calling thread:

@Transactional
public class AccountService {
    @Async
    public void transferAsync() {
        // this is an async and transactional method
    }

    public void transfer() {
        // transactional method
    }
}

In the example, the transferAsync() method is transactional and async. Thus, it defines a transactional unit and runs on a different thread. Hence, it’s available for transaction management but not in the same context as the calling thread.

Thus, if something fails, the code inside transferAsync() rolls back because it’s @Transactional. However, since that method is also @Async, Spring doesn’t propagate the calling context to it. Thus, in the failure scenario, Spring doesn’t roll back any code outside of trasnferAsync() like when we call a sequence of transactional-only methods. Therefore, this falls into the same data integrity problem as calling a @Async from @Transactional.

The class-level annotation is handy for writing less code to create a class that defines a sequence of fully transactional methods.

However, this mixed transactional and async behavior might create confusion when troubleshooting the code. For instance, we expect all code in a sequence of transactional-only method calls to be rollbacked when a failure occurs. However, if a method of that sequence is also @Async, the behavior is unexpected.

4. Conclusion

In this tutorial, we’ve learned when it’s safe to use @Transactional and @Async annotations together from a data integrity perspective.

In general, calling @Transactional from a @Async method guarantees data integrity since Spring properly propagates the same context.

On the other hand, when calling @Async from @Transactional, we might fall into data integrity problems.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Baeldung Pro – NPI EA (cat = Baeldung)
announcement - icon

Baeldung Pro comes with both absolutely No-Ads as well as finally with Dark Mode, for a clean learning experience:

>> Explore a clean Baeldung

Once the early-adopter seats are all used, the price will go up and stay at $33/year.

eBook – HTTP Client – NPI EA (cat=HTTP Client-Side)
announcement - icon

The Apache HTTP Client is a very robust library, suitable for both simple and advanced use cases when testing HTTP endpoints. Check out our guide covering basic request and response handling, as well as security, cookies, timeouts, and more:

>> Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

Course – LS – NPI EA (cat=REST)

announcement - icon

Get started with Spring Boot and with core Spring, through the Learn Spring course:

>> CHECK OUT THE COURSE

Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – Summer Sale 2026 – NPI EA (cat= Baeldung)
announcement - icon

Yes, we're now running our only Summer Sale. All Courses are 30% off until 20th July, 2026:

>> EXPLORE ACCESS NOW

Course – Summer Sale 2026 – NPI (All)
announcement - icon

Yes, we're now running our only Summer Sale. All Courses are 30% off until 20th July, 2026:

>> EXPLORE ACCESS NOW

eBook Jackson – NPI EA – 3 (cat = Jackson)
2 Comments
Oldest
Newest
Inline Feedbacks
View all comments