Spring core issue review 3: spring transaction propagation mechanism, transaction failure situation, understanding of ioc

1. How does spring simplify development? What are the advantages of spring?

Spring is a framework, but also a container, or a 生态.

IOC是一种理论思想,DI是具体的实现方式

1) Spring simplifies enterprise-level java development through DI, AOP and elimination of boilerplate code
2) In addition to the Spring framework, there is also a huge ecosystem built on the core framework, which extends Spring to different fields, such as web Server, REST, mobile development, and Nosql
3) Low intrusive involvement, extremely low code pollution
4) Independent of various application servers, applications based on the Spring framework can truly implement Write Once, Run Anywhere
5) Spring's ioc container Reduce the complexity of business object replacement and improve the decoupling between components
6) Spring's AOP support allows centralized processing of some common tasks such as security, transactions, logs, etc., thus providing better reuse
7) Spring's ORM and DAO provide good integration with third-party persistence layer frameworks and simplify the underlying database access
8) Spring's high openness does not force applications to completely depend on Spring, and developers can freely choose parts of the Spring framework or all

2. What is the transaction propagation mechanism of spring?

Spring has seven transaction propagation mechanisms:

  1. REQUIRED: The default propagation feature, if there is no current transaction, create a new transaction; if there is a current transaction, join this transaction
  2. SUPPORTS: If there is a transaction currently, join the current transaction; if there is no transaction, execute in a non-transactional manner
  3. MANDATORY: If there is a transaction currently, join the current transaction; if the current transaction does not exist, throw an exception
  4. REQUIRED_NEW: Create a new transaction, suspend the current transaction if it exists
  5. NOT_SUPPORTED: Execute in a non-transactional manner, if there is a current transaction, suspend the current transaction
  6. NEVER: do not use a transaction, if the current transaction exists, an exception is thrown
  7. NESTED: If the current transaction exists, it will be executed in a nested transaction; if there is no current transaction, a new transaction will be created

REQUIRED, REQUIRES_NEW, NESTED comparison :
REQUIRED(spring默认级别): External and internal methods, if any method is abnormal, all will be rolled back
1) Internal throwing exception:
If there is no catch externally, this exception will be thrown upwards, and externally will roll back.
If the external method is caught, the spring transaction manager will throw an UnexpectedRollbackException method upwards for the external method.
Therefore, no matter whether it is caught or not, as long as the internal exception is abnormal, the external will definitely roll back.

2) External throw exception: internal rollback

REQUIRES_NEW: External exception, no internal rollback ; internal exception, decide whether to roll back according to whether the external is caught or not
1) Internal exception:
if the external is caught, the external will not be rolled back
;

2) External throw exception: internal does not roll back

NESTED: External exception, internal rollback ; internal exception, decide whether to rollback according to whether the external catch

Inner and outer methods are nested transactions. The principle is that the internal transaction will create a savepoint in the external transaction, and the internal transaction will roll back to this savepoint point after the exception.

1) Internal exception:
If the external is caught, the external will not be rolled back.
If the external is not caught, the external will be rolled back

2) External throw exception: internal rollback

3. What is the principle of spring transaction implementation?

When using the spring framework, there are two ways to implement transactions. One is 编程式事务that the user controls the processing logic of the transaction through code; the other is 声明式事务to implement it through the @Transactional annotation.

In fact, the operation of the transaction should be controlled by the database, but in order to facilitate the user to operate the business logic, spring has extended the implementation of the transaction function. Generally, programmatic transactions are rarely used, and more are implemented by adding the @Transactional annotation. After adding this annotation, the automatic commit function of the transaction will be turned off and controlled by the spring framework.

Transaction operation is a core embodiment of AOP. When a method is annotated with @Transactional, spring will generate a proxy object based on this class and use this proxy object as a bean . When using the method of this proxy object, if there is transaction processing, the automatic submission will be turned off first, and then the specific business logic will be executed. If there is no task exception in the business logic, the proxy logic will be submitted directly, and if any exception occurs, the rollback operation will be performed.

4. When will the spring transaction fail?

  1. 未被spring管理: The premise of using Spring transactions is that the object must be managed by Spring, and the class where the transaction method is located must be loaded as a bean object
  2. 数据库不支持事务: Taking MySQL as an example, the InnoDB engine supports transactions, while MyISAM, MEMORY, etc. do not support transactions
  3. 事务方法没有被public修饰: There are four access modifiers for java, private, default, protected, and public, but the @Transactional annotation can only be applied to public modified methods
  4. 使用final修饰了方法: If the transaction method is decorated with final, then aop cannot rewrite the method in the proxy class, and the transaction will not take effect. Similarly, static modified methods cannot be turned into transaction methods through proxy.
  5. 同一个类中方法调用: The method in the same class does not have a transaction, if a method with a transaction is called in this method, then the transaction is also invalid. Because the method of the class itself is called, not the method of the proxy object.
  6. 多线程调用: Because the two operations are not in the same thread, the obtained database connections are different, so they are two different transactions, so they will not be rolled back.
  7. 错误的传播行为: Spring defines 7 propagation behaviors. We can specify the propagation behavior parameters through the propagation attribute. Currently, only REQUIRED, REQUIRES_NEW, and NESTED will create new transactions, and others will run in a non-transactional manner or throw an exception
  8. 自己try...catch...掉了异常: If no exception is thrown, Spring thinks the program is normal and will not roll back
  9. 手动抛出了错误的异常: By default, Spring will only roll back RuntimeException and Error. For ordinary Exception, it will not roll back. If you want to trigger the rollback of other exceptions, you need to configure it on the annotation, such as: @Transactional(rollbackFor = Exception.class)
    10 嵌套事务回滚多了.: That is, an exception occurs in the inner transaction, because there is no try, catch, resulting in the outer transaction page A rollback was made.

5. What are the scopes of beans supported by spring?

  1. singleton: default scope, singleton bean, only one bean instance in each Spring IOC container
  2. prototype: Prototype mode, every time the getBean method is used to obtain the bean defined by the prototype, a new Bean instance will be generated;
  3. request: Create a bean instance for each request request. After the request is completed, the bean will become invalid and then be recycled by Spring's garbage collector;
  4. session: Basically similar to the scope of request, the same session session shares a bean instance, and different session sessions use different bean instances. This scope will only be valid when Spring is used in web applications;
  5. global-session: Global scope, all sessions share the same bean instance, so if we want to declare a storage variable shared by all sessions, then this global variable should be stored in global-session.

6. Talk about the understanding, principle and implementation of spring ioc?

Total :

ioc is a theoretical idea, and DI is a concrete implementation.

The original object is controlled by the user. With spring ioc, the entire object can be handed over to spring to help us manage it.

DI: Dependency Injection, which injects the value of the corresponding attribute into a specific object, @Autowired, populateBean completes the injection of the attribute value

Container: store objects, use the map structure to store, generally there is a three-level cache in spring, and singletonObjects store complete bean objects. The entire life cycle of a bean, from creation to use to destruction, is all managed by the container.

Points :
1. Create a container (beanFactory, DefaultListableBeanFactory), set some parameters (BeanPostProcessor, subclass of Aware interface) and other attributes to the bean factory.
2. Load and analyze the bean object, and prepare the definition object beanDefinition of the bean object to be created (xml or annotation analysis process)
3. The processing of beanFactoryPostProcessor, here is the extension point: PlaceHolderConfigurSupport, ConfigurationClassPostProcessor
4. The registration function of BeanPostProcessor is convenient for subsequent The bean object completes the specific extension function
5. Instantiate the BeanDefinition object into a specific bean object through reflection
6. The initialization process of the bean object (custom object attribute assignment, container object attribute assignment Aware interface, call beanPostProcessorBeforeInitialization, call init-method method, calling beanPostProcessorAfterInitialization)
7. Generate a complete bean object, which can be obtained directly through the getBean method
8. Destruction process

Guess you like

Origin blog.csdn.net/xueping_wu/article/details/126441375