transactions : A logical set of operations, either all successful or all fail.
Transaction Characteristics (four characteristics):
- ACID:
- Atomicity: The business is inseparable
- Consistency: Data integrity remains consistent before and after transaction execution.
- Isolation: When a transaction executes, it should not be disturbed by other transactions
- Persistence: Once finished, the data is permanently saved to the database.
- If isolation is not considered:
- Dirty reads: One transaction reads to another transaction uncommitted data
- Non-REPEATABLE READ: One transaction reads to another transaction has committed data (update) causes a transaction to be inconsistent with multiple query results
- Virtual read: One transaction read to another transaction has committed data (insert) causes a transaction to be inconsistent with multiple query results
- isolation level of the transaction:
- Uncommitted read (READ UNCOMMITTED): All of this can happen.
- Read Committed: Avoid dirty reads, but not repeat read, virtual read is possible.
- REPEATABLE READ (REPEATABLE Read): Avoid dirty reads, non-repeatable reads, but virtual reads can occur.
- Serial: Avoid all of the above.
Transaction management in Spring
Tiered development: The transaction is in the service layer.
Spring provides a transaction management API (three interfaces)
Platformtransactionmanager: Platform transaction manager. (The following methods)
- Commit (Transactionstatus status)
- Gettransaction (transactiondefinition definition)
- Rollback (transactionstatus status)
Transactiondefinition: Transaction definition information (isolation, propagation, timeout, read-only) (the following constants)
- ISOLATION_XXX: Transaction ISOLATION level.
- PROPAGATION_XXX: The propagation behavior of a transaction. (Not in JDBC, in order to solve actual development problems.)
- Expiry time:
- Transactionstatus: Transaction Run State
- Do you have a savepoint?
- Whether a new transaction
- Whether the transaction has been committed
The relationship between them : Platformtransactionmanager set up transaction-related information management transactions through Transactiondefinition, in the process of managing transactions, Generates some transaction state: The state is logged by Transactionstatus.
API Explanation:
- Platformtransactionmanager: interface.
Spring provides different Platformtransactionmanager interface implementations for different persistence frameworks
Transactions |
Description |
Org.springframework.jdbc.datasource.DataSourceTransactionManager |
Using spring JDBC or ibatis to persist data |
Org.springframework.orm.hibernate3.HibernateTransactionManager |
Using the Hibernate3.0 version to persist data |
Org.springframework.orm.jpa.JpaTransactionManager |
Use when using JPA for persistence |
Org.springframework.jdo.JdoTransactionManager |
Used when persistence mechanism is JDO |
Org.springframework.transaction.jta.JtaTransactionManager |
Use a JTA implementation to manage transactions, which must be used when a transaction spans multiple resources |
Transactiondefinition:
Isolation_default: The default level. Mysql Repeatable_read Oracle Read_commited
isolation_read_uncommitted
isolation_read_committed
Isolation_repeatable_read
Isolation_serializable
- The propagation behavior of a transaction: (not JDBC transaction management, used to solve actual development problems.) propagation behavior: Resolves the relationship of transactions that are invoked between business layers.
Propagation_required: Supports the current transaction and creates a new one if it does not exist
- If a has a transaction, B uses a transaction, and if a has no transaction, B opens a new transaction. (A, B is in a transaction.) )
Propagation_supports: Supports the current transaction and does not use a transaction if it does not exist
- If a has a transaction, B uses a transaction, and if a has no transaction, B does not use the transaction.
Propagation_mandatory: Supports current transaction, throws exception if not present
- If a has a transaction, B uses a transaction, if a has no transaction, throws an exception.
Propagation_requires_new If a transaction exists, suspends the current transaction, creates a new transaction
- If a has a transaction, B suspends the transaction of a and re-creates a new transaction. (A, B is not in a transaction. Transactions do not affect each other.)
Propagation_not_supported runs in a non-transactional manner, suspending the current transaction if a transaction exists
- A, A is a non-transactional way of running, and a has a transaction that suspends the current transaction.
Propagation_never runs in a non-transactional manner, throws an exception if a transaction exists
propagation_nested if the current transaction exists, the nested transaction executes
- Based on savepoint technology.
A, b A has a transaction, after a is executed, the content after the execution of a transaction is persisted to the SAVEPOINT.B transaction, the user needs to set the transaction commit or rollback.
- Commonly used: (emphasis)
Propagation_required
Propagation_requires_new
propagation_nested
Spring's transaction management:
Spring's transaction management is divided into two categories:
- Programmatic transaction Management:
- Write code manually to complete transaction management.
- Declarative transaction Management:
- No need to write code manually, configure.
Spring's transaction management