Spring handles transactions

Spring handles affairs

Different ways of accessing the database have different transaction processing mechanisms, objects, and methods.
Spring provides a unified model for transaction processing, which can use unified steps and methods to complete transaction processing of a variety of different database access counts.

Implementation points

Spring's transaction model and steps are fixed.

Declare transaction manager object

The internal commit and rollback of the transaction all use the transaction manager object to complete the commit and rollback instead of us.

The transaction manager object is an interface and its many implementation class
interfaces : PlatformTransactionManager, which defines important transaction methods, commit, rollback
implementation classes : Spring creates the transaction processing classes corresponding to each database access technology

  • mybatis access to the database-----created is DataSourceTransactionManager
  • hibernate access to the database-----created is HibernateTransactionManager
  • ……

At this time, use <bean> in the spring configuration file to declare the implementation class of the transaction manager

Describe transaction type

Transaction isolation level :

  • DEFAULT: The default transaction isolation level of the database is used. MySql defaults to REPEATABLE_READ
  • READ_UNCOMMITTED: Read has not been submitted. Did not solve any concurrency issues
  • READ_COMMITTED: Read has been submitted. To solve Tibetan independence, there are non-repeatable readings and phantom readings.
  • PEPEATABLE_READ: Repeatable reading. Solve dirty reads, non-repeatable reads, and phantom reads
  • SERIALIZABLE: Serialization. There is no concurrency problem.

The transaction timeout time
indicates the longest execution time of a method, and it will be rolled back after timeout. The unit is second and an integer value. The default is -1 which means there is no timeout time.

Propagation behavior of affairs
Control business methods whether there are affairs and what kind of affairs.
There are 7 propagation behaviors, which indicate how transactions are used between methods when your business method is called. Mainly know three

  • PROPAGATION_REQUIRED : The specified method must be executed within a transaction. If there is a transaction currently, it will be added to the current transaction; if there is no transaction currently, a new transaction will be created. This kind of propagation behavior is the most common, Spring's default transaction propagation behavior.
    E.g:
    Insert picture description here

  • PROPAGATION_REQUIRES_NEW
    always creates a new transaction. If there is a transaction, the current transaction will be suspended until the new transaction is completed.
    Insert picture description here

  • PROPAGATION_SUPPORTS
    method does not affect whether there are transactions, such as query
    Insert picture description here

Timing of transaction commit and rollback

  • The business method is executed successfully and no exception is thrown. After the method is executed, the commmit of the transaction manager is called
  • The business method throws a runtime exception , calls the rollback of the transaction manager, and the check line exception is submitted by default

Summary of spring affairs

1. The transaction management is transaction management and its implementation class
2. Spring's transaction is a unified model
  1) Specify the transaction manager implementation class to be used, using <bean>
  2) Specify which classes and methods need to be added to the transaction Function
  3) The isolation level, propagation behavior, timeout required by the execution method

Guess you like

Origin blog.csdn.net/qq_36976201/article/details/109137478