Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Transactions | Working with Databases
Spring Boot Backend
course content

Course Content

Spring Boot Backend

Spring Boot Backend

1. Backend Development Basics
2. Spring Boot Basics
3. RESTful API
4. Working with Databases
5. Testing Backend Applications

book
Transactions

A transaction essentially gives us a choice: either all the operations within it are completed successfully, or none of them are executed at all.

Imagine a bank transfer operation, where money is moved from one account to another. This process involves two steps:

  1. Deducting money from one account;
  2. Depositing money into another account.

If the transaction fails after deducting money but before depositing it, the funds could be "lost." A transaction ensures that both operations are either fully completed or fully reversed.

Basics of Transaction Management

The @Transactional annotation: This is used to declare methods or classes that should be executed within the context of a transaction.

When a method with this annotation is called, Spring starts a new transaction. If the method completes successfully, the transaction is committed; otherwise, it is rolled back.

Let’s provide an example based on the real-life scenario mentioned earlier.

java

BankService

copy
1234567891011121314151617
@Service public class BankService { private AccountRepository accountRepository; @Transactional public void transferMoney(Long fromAccountId, Long toAccountId, double amount) { Account fromAccount = accountRepository.findById(fromAccountId); Account toAccount = accountRepository.findById(toAccountId); fromAccount.withdraw(amount); toAccount.deposit(amount); accountRepository.save(fromAccount); accountRepository.save(toAccount); } }

When the transferMoney method is marked with the @Transactional annotation, it means that all changes happening within this method will be executed within a single transaction.

When we call fromAccount.withdraw(amount) followed by toAccount.deposit(amount), both of these actions must be successfully completed. If, for instance, an error occurs during the toAccount.deposit(amount) operation, the transaction will automatically roll back the changes made during the fromAccount.withdraw(amount) step.

This ensures that either both operations are executed and the money is transferred, or, in the event of an error, neither operation is performed, preventing any loss of funds. The transaction guarantees that the database will never be left in an inconsistent state.

Practical Application of Transactions

Summary

A transaction is a set of database operations treated as a single unit, ensuring all operations succeed together or are fully rolled back in case of failure. You can learn more about transactions here.

What is a `transaction` in the context of a database?

What is a transaction in the context of a database?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 4. Chapter 6
We're sorry to hear that something went wrong. What happened?
some-alt