Things properties in Spring

Characteristics of things

  • Atomicity: either all of them are executed, or all of them are not executed
  • Consistency: The database changes from one consistent state to another consistent state
  • Isolation: things cannot interfere with each other
  • Persistence: Once something is submitted, it will always be persisted to the database storage

Properties of things

  • Isolation attribute
  • Propagation properties
  • Read-only attribute
  • Timeout attribute
  • Abnormal attributes

Isolation property: Solve the problems of dirty reads, phantom reads, and non-repeatability that are prone to occur in high concurrency scenarios.

Dirty reading: two things, t1, t2; t1 modifies the salary, from the original 5000 to 8000, but has not yet submitted it. At this time, t2 queries the salary, and the result is 8000. At this time, t1 requires Things were submitted, but an exception occurred, and the data was rolled back to 5000. At this time, t2 obtained the dirty data that was not submitted by t1, referred to as dirty read.
Non-repeatable reading: two things, t1, t2; t1 gets a salary of 5000, at this time t1 thing has not been submitted yet, t2 thing has adjusted the salary to 8000, and submitted it, and then found when t1 thing gets salary again The result is inconsistent with the data obtained last time, which results in non-repeatable reading. (Note that in the same thing, the data obtained multiple times are inconsistent)
Phantom reading: two things, t1, t2; t1 queries 10 records according to sql, at this time t1 has not yet been submitted, t2 has two new entries in the table Pieces of data, and submitted. At this time, when the t1 thing gets data again, it finds that there are two more (what is the situation?), at this time a phantom reading occurs (note that the number of pieces of data obtained multiple times in the same thing Inconsistent)

Isolation properties are mainly divided into the following categories

  1. Isolation=READ_COMMITTED: Only the submitted things can be read to solve the dirty read problem
  2. Isolation=REPEATABLE_READ: Row lock: When things operate on a certain data, the current row will be locked, and other things will enter a waiting stage to solve the problem of non-repeatable read
  3. Isolation=SERIALIZABLE: Table lock: When things operate on a certain table, the current table is locked. If other things want to query the table data, they need to wait for the submission of previous things to solve the phantom reading problem.

MYSQL default isolation level: REPEARABLE_READ
ORACLE default isolation level: READ_COMMITTED, does not support REPEATABLE_READ

Propagation attributes: to solve the problem of nesting of things, the external big things contain many small things, and the small things may affect the big things on the outer layer, leading to the loss of things

The propagation attributes are mainly divided into the following categories:

  1. propagation.requre: If there is nothing in the outer layer, add something and add it. If there is something outside, add an external thing, usually used to add, delete, or modify
  2. propagation.supports: If there is nothing outside, no new things will be opened up, if there are things outside, then external things will be added, usually used for query operations
  3. propagation.requier_new: If there is something external, suspend the external thing and start a new thing
  4. propagation.not_supported: do not open things, if there are external things, then suspend external things
  5. propagation.never: do not open things, if open, throw an exception

Read-only attribute: for query-only methods, improve query efficiency, eg:readonly=true

Timeout attribute: control the waiting time of things, eg: timeout=2


The way to add things in spring

@Transactional, the main parameters are as follows

  1. Isolation: Code isolation mechanism, specific usage method: isolation=Isolation. Specific isolation mechanism
  2. Propagation: the propagation mechanism of things, the specific method of use: propagation=Propagation. The specific propagation mechanism of things
  3. readonly: Whether it is read-only, how to use it: readonly=true | false
  4. timeout: The waiting time of things, how to use it: timeout=-1 |? (Unit: s)

Guess you like

Origin blog.csdn.net/qq_41454044/article/details/114069005