Home > Java > javaTutorial > When and Why Should You Adjust the Default Isolation and Propagation Parameters in @Transactional?

When and Why Should You Adjust the Default Isolation and Propagation Parameters in @Transactional?

DDD
Release: 2024-11-03 19:56:02
Original
674 people have browsed it

When and Why Should You Adjust the Default Isolation and Propagation Parameters in @Transactional?

Isolation and Propagation Parameters in @Transactional

In Spring's @Transactional annotation, two critical parameters define the behavior of database transactions: isolation and propagation. This article explores when and why you should consider adjusting their default values.

Propagation

Propagation defines how transactions relate to each other. Common options include:

  • REQUIRED: Runs the code within an existing transaction or creates a new one if none exists.
  • REQUIRES_NEW: Always creates a new transaction, suspending any existing ones.

Default Value: REQUIRED

Isolation

Isolation defines the data contract between transactions. It prevents certain data inconsistencies by specifying the level of visibility into data changes made by other transactions. Key isolation levels are:

  • READ_UNCOMMITTED: No protection against dirty reads.
  • SERIALIZABLE: Strongest isolation, ensuring no data conflicts.

Default Value: Varies depending on the database (e.g., REPEATABLE_READ for MariaDB)

Real-World Example

Consider the issue of dirty reads, where a transaction can read uncommitted changes made by another transaction.

                                       Thread 1          Thread 2
                                               |              |
                                             Write(x)           |
                                               |              |
                                               |             Read(x)
                                               |              |
                                             Rollback           |
                                                 |             |
                                                   Value (x) is now dirty (incorrect)
Copy after login

In this scenario, to prevent dirty reads, you could set the isolation level to READ_COMMITTED and the propagation level to REQUIRED. This combination ensures that transactions only read data that has been committed by other transactions.

Customizing Transactions

In the following example, the provideService method always runs within a new transaction, ensuring that any changes made by other concurrent tasks do not interfere with its execution:

<code class="java">@Transactional(propagation=Propagation.REQUIRES_NEW)
public void provideService() {
    repo1.retrieveFoo();
    repo2.retrieveFoo();
}</code>
Copy after login

Testing Transaction Behavior

To verify the behavior of different propagation levels, you can use a Java test:

<code class="java">@Test
public void testProvideService() {
    TransactionStatus status = transactionManager.getTransaction(new DefaultTransactionDefinition());
    fooService.provideService();
    transactionManager.rollback(status);
    // Assert repository values are unchanged ...
}</code>
Copy after login

With REQUIRES_NEW, fooService.provideService() would not be rolled back since it operated within a separate transaction. With REQUIRED, everything would be rolled back.

The above is the detailed content of When and Why Should You Adjust the Default Isolation and Propagation Parameters in @Transactional?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template