This is the 12th day of my participation in the August Challenge

We all know the four characteristics of transactions: Atomicity, consistency, durability, isolation, and to ensure the operation of one or more database called Transaction (Transaction), * * * * it is a the smallest indivisible unit of work, the most common example bank transfer the operation of the two accounts at the same time only failure or success at the same time, the default database engine INNODB support transactions

  1. Atomicity: The smallest unit of a transaction
  2. Consistency: The operation must both succeed or fail
  3. Persistence: Persistent operations, that is, operations are saved to disk
  4. Isolation: Different transactions do not affect each other

Here is a commit flow of a transaction, and there are several states that we can pass through during database operations

  1. BEGIN

  2. START TRANSACTION

If you want to rollback the transaction, perform **rollback. If you want to rollback the transaction, perform **rollback

BEGIN UPDATE 1 UPDATE 2 Commit /rollbackCopy the code

For rollback, we can call it ourselves, or the system will call it automatically if we encounter some exceptions. In our daily development, Spring may already handle it for us or Spring will simplify our scenario definition of transactions. If we are not using a transaction-enabled engine, then rollback is not supported

Let’s take a look at Spring’s transaction propagation mechanism:

  • PROPAGATION_REQUIRED Spring’s default propagating mechanism, if there is a transaction in the outer layer, the current transaction is added to the outer layer, and one is committed, and another is rolled back. If there are no transactions in the outer layer, create a new transaction and execute it
  • PROPAGATION_REQUIRED_NEW This transaction propagation mechanism opens a new transaction, suspends the outer transaction, and resumes the upper transaction when the current transaction completes. If there is no transaction in the outer layer, execute the newly opened transaction. If the outer task fails to roll back, the execution of the inner layer transaction cannot be rolled back. If the inner layer fails, an error is reported.
  • Add a transaction to an outer layer, PROPAGATION_SUPPORTS. If there is no transaction, execute it non-transactionally. Completely dependent on outer transactions
  • PROPAGATION_NOT_SUPPORTED This propagation mechanism does not support transactions, and suspends if a transaction exists in an outer layer, PROPAGATION_NOT_SUPPORTED. After executing the current code, the outer layer transaction is restored, and the current code is not rolled back, regardless of the exception
  • PROPAGATION_NEVER This propagation mechanism does not support outer transactions, meaning that an exception is thrown if there is a transaction in the outer layer
  • PROPAGATION_MANDATORY, as opposed to NEVER, throws an exception if there is no transaction in the outer layer
  • PROPAGATION_NESTED characteristic of the transmission mechanism is to be able to save state save points, the current transaction rollback to a certain point, to avoid all the nested transaction rollback, internal affairs will not affect the external affairs it is only right DataSourceTransactionManager transaction manager work

Specific scenarios use different modes of communication, we have to depend on the business scenario