1

A fan who has worked for 2 years privately messaged a relatively simple question.

Say: "What is the propagation behavior of transactions in Spring?"

He said he could remember some, but basically no configuration was needed in the project, so he forgot it all at once.

As a result, the interview was rejected, which is a pity!

ok, on this question, look at the answers of ordinary people and experts.

Ordinary people:

Ok. . . . . . . .

Expert:

There are several aspects to answer this question.

First, the so-called transaction propagation behavior is how the transaction should be propagated when multiple methods that declare a transaction call each other.

For example, methodA() calls methodB(), and both methods explicitly start the transaction.

carbon-202204111807

So does methodB() start a new transaction, or continue to execute in the methodA() transaction? It depends on the propagation behavior of the transaction.

In Spring, 7 transaction propagation behaviors are defined.

  1. REQUIRED: The default Spring transaction propagation level. If there is a current transaction, join the transaction. If there is no transaction, create a new transaction.
  2. REQUIRE_NEW: Regardless of whether there is a transaction, a new transaction will be opened, and the new and old transactions are independent of each other. The outer transaction throws an exception and rollback will not affect the normal commit of the inner transaction.
  3. NESTED: If a transaction currently exists, execute nested within the current transaction. If there is no current transaction,
    A new transaction is created, similar to REQUIRE_NEW.
  4. SUPPORTS: Indicates that the current transaction is supported. If there is no current transaction, it will be executed in a non-transactional manner.
  5. NOT_SUPPORTED: Indicates that it runs in a non-transactional manner. If there is a current transaction, the current transaction is suspended.
  6. MANDATORY: Force transaction execution, throw an exception if no transaction currently exists.
  7. NEVER: Execute in a non-transactional manner, throwing an exception if there is currently a transaction.

The Spring transaction propagation level generally does not need to be defined, and the default is PROPAGATION_REQUIRED, unless it is necessary to understand in the case of nested transactions.

The above is my understanding of the problem!

Summarize

This question really only needs to understand the nature of transaction propagation behavior and why it is necessary to consider transaction propagation.

You can deduce the answer directly based on your own technical accumulation. It is nothing more than an exhaustive list of possible strategies. How can you deduce 5 types?

This issue of ordinary people VS master interview series ends here.

If you have any technical interview questions that you don't understand, please feel free to private message me at any time.

file

Copyright notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless otherwise stated. Please indicate the source for Mic带你学架构 !
If this article is helpful to you, please help to follow and like, your persistence is the driving force for my continuous creation. Welcome to follow the WeChat public account of the same name to get more technical dry goods!

跟着Mic学架构
810 声望1.1k 粉丝

《Spring Cloud Alibaba 微服务原理与实战》、《Java并发编程深度理解及实战》作者。 咕泡教育联合创始人,12年开发架构经验,对分布式微服务、高并发领域有非常丰富的实战经验。