Spring003--spring Transaction Management (MOOC)

Source: Internet
Author: User
Tags connection pooling savepoint

Spring transaction Management one. Transaction Review 1.1. What is a transaction

A transaction is a logical set of operations that either succeed or fail altogether.

Abnormal situation occurs, need to guarantee: "1" Zhang San the money out, John Doe receive money. "2" Zhang San Money was not successfully transferred out, John Doe also did not receive money.

1.2. Transaction characteristics

There are 4 major features of transactions: atomicity, consistency, isolation, persistence.

1.2.1. Atomic Nature

Atomicity means that a transaction is an inseparable unit of work, and the operations in the transaction either occur or do not occur.

Physics emphasizes that atoms are the smallest unit, indivisible. If Zhang San to Lee four turn money, Zhang three turn out and John Doe into this group of operations have atomicity, indivisible.

1.2.2. Consistency

Consistency means that the integrity of the data must be consistent before and after the transaction.

such as: Zhang San has 2000 yuan, Li four has 2000 yuan. amounted to $4000. Zhang San to Li four turn 1000 yuan, whether the transfer is successful or not, the total must be 4000 yuan. Ensure consistency before and after data

1.2.3. Isolation of

Isolation refers to when multiple users concurrently access the database, one user's transaction cannot be disturbed by other users ' transactions, and data between multiple concurrent transactions is isolated from each other.

As shown in: Transaction a modifies the Zhang San Account record, and transaction B also modifies the Zhang San account. The problem occurs: Duplicate modifications or transaction a changes are overwritten by transaction B.

It is therefore necessary to ensure that transaction A is not disturbed by transaction B.

Solution: The database sets the isolation level of some transactions, which can be controlled by the transaction isolation level of the database to not interfere with another transaction.

1.2.4. Durability

Persistence refers to the fact that once a transaction is committed, its changes to the database data are permanent, and should not have any effect on the database if it fails.

Two. A set of API2.1 for spring transaction management. Spring interface

Above, the Platformtransactionmanager transaction manager is an interface for truly managing transactions. It contains information like the commit of a transaction, rollback, and so on.

There is a connection between transaction manager & Transaction definition information & transaction-specific operational state:

2.2. Platformtransactionmanager Interface (Platform transaction manager)

Spring provides different Platformtransactionmanager interface implementations for different persistence layer frameworks

2.3. Transactiondefinition defines the transaction isolation level (isolation, propagation, timeout, read-only) isolation

If transaction isolation is not considered, security issues can be raised as follows:

1. Dirty read:

One transaction reads data that has been overwritten by another transaction but has not yet been committed. If the data is rolled back, the data read is not valid. Causes inconsistent query results.

2. Non-repeatable READ:

One transaction reads data that is overwritten and committed by another transaction, resulting in a different result than reading the same data multiple times.

3. Phantom read/virtual read:

After a transaction has read several rows of records, another transaction inserts some records, and the phantom reads occur. Later in the query, the first transaction will find some records that were not originally found.

The isolation level is used to solve the problem of dirty reading, non-repeatable reading and phantom reading.

4 Isolation levels for transactions: Low Level-"high (read_uncommited->serializable)

Note: The serializable is serial and cannot occur concurrently. As a result, no dirty reads, no repetition, and no phantom reading can occur.

Spring provides the default isolation level, which is what isolation level the database uses, and what the default is.

MySQL default with Repeatable_read isolation level

Oracle default with read_commited isolation level

2.4. Transactiondefinition defines the propagation behavior of the transaction (isolation, propagation, timeout, read-only) 1. What is the propagation (propagation) behavior of a transaction?

The propagation behavior of the transaction is mainly used to solve some problems,

A complex situation arises: call SERVICE1.AAA () and SERVICE2.BBB () to complete a business.

At this point service1.aaa () &service2.bbb () have transactions, then which of the transactions apply? The propagation behavior of the applied transaction.

Propagation behavior of transactions: used to resolve calls between business layer methods and how transactions are passed.

2. 7 propagation behaviors of a transaction (Class 3, key 1th)

Description

1. Propagation_required: The current transaction is supported, and if present, a new transaction is created with the current transaction and does not exist.

As above Service1.aaa () and SERVICE2.BBB (). If SERVICE2.BBB () defines the propagation behavior as propagation_required,

When SERVICE1.AAA () has a transaction, SERVICE2.BBB () uses the SERVICE1.AAA () transaction. That is, they are in the same transaction. Either the execution succeeds or both are rolled back.

When SERVICE1.AAA () does not have a transaction, SERVICE2.BBB () creates a new transaction. Then SERVICE2.BBB () transaction rollback does not affect SERVICE1.AAA

The following three transactions are of the same set of transaction types:

Propagation_required: Supports the current transaction and creates one if it does not exist. Propagation_supports: Supports the current transaction and does not use a transaction if it does not exist. Propagation_mandatory: Supports the current transaction and throws an exception if it does not exist.

Same point:

is to support the current transaction, that is, if SERVICE1.AAA has a transaction, the SERVICE1.AAA transaction is used.

Different points:

Propagation_required: When SERVICE1.AAA does not have a transaction, a new transaction is created.

Propagation_supports: When SERVICE1.AAA does not have a transaction, the transaction is not used (that is, the transaction does not take effect at the beginning service.bbb).

Propagation_mandatory: Throws an exception when SERVICE1.AAA does not have a transaction.

2. Propagation_requires_new: If a transaction exists, the current transaction is suspended and a new transaction is created.

As above Service1.aaa () and SERVICE2.BBB (). If SERVICE2.BBB () defines the propagation behavior as propagation_requires_new,

When SERVICE1.AAA () has a transaction, the SERVICE1.AAA transaction is suspended and service2.bbb a new transaction. That is, they are not in the same transaction.

The following three transactions are of the same set of transaction types:

Propagation_requires_new: Creates a new transaction and suspends the current transaction if a transaction is currently present. Propagation_not_supported: Executes the operation in a non-transactional manner, suspending the current transaction if a transaction is currently present. Propagation_never: Executes in a non-transactional manner and throws an exception if a transaction is currently present.

Same point:

Nested methods are not in the same transaction, and if there is a transaction currently (SERVICE1.AAA), the transaction is suspended.

Different points:

Propagation_requires_new: Creates a new transaction. (SERVICE2.BBB create a new transaction)

Propagation_not_supported: Do not use transactions. (SERVICE2.BBB does not use transactions)

Propagation_never: Throws an exception. (SERVICE2.BBB throws an exception)

3. propagation_nested: Nested transaction Execution (complex) if current transaction exists

When SERVICE1.AAA () has a transaction, when the SERVICE1.AAA () is executed, a savepoint (savepoint) is used to set the transaction. When executing service2.bbb (), if there is no exception, commit it together,

If there is an exception, depending on the custom setting, you can roll back to the transaction savepoint or roll back to the initial state. You can control it yourself.

2.3. Transactionstatus interface (transaction status) three. The transfer environment is built 3.1. Spring supports two ways of transaction management: 1. Programmatic transaction Management

(1). Rarely used in practical applications.

(2). Manually Manage transactions via Transactiontemplate

2. Configuring declarative transactions using XML

(1). Recommended in development (minimal code intrusion)

(2). Spring's declarative transactions are implemented through AOP .

3.2. Transfer Environment Preparation 1. Database Preparation

2. Create a Web project

The corresponding jar packages and configuration files are introduced.

Jar Package:

(1). Connecting a database requires the introduction of a database driver package, connection pooling (C3P0)-related jar packages.

(2). Spring Related JAR Packages

(3). Log Package

Configuration file:

(1). Log4j.properties: Log records

(2). Jdbc.properties: Database Connection Configuration file

(3). applicationContext.xml:Spring Core File

Create a package, write business logic:

(1) Definition Interface/accountservice: Business Layer Interface

(2) Define implementation class/accountserviceimpl: Business layer Implementation class, invoking DAO layer

(3) Operational Database DAO layer interface

(4) DAO layer Interface implementation class

(5) Spring completes some related configuration

"1". Connect to Database (import external properties file) & C3P0 Connection Pool

"2" Configure the Business layer class

"3" Configuration DAO Class: Directly into the DAO inside the connection pool can be. Just create a connection pool for it, and it will create a JDBC template for us.

At this point, you can write in the DAO implementation class to manipulate the database.

(6) Create a test class

Impersonation Service implementation class exception, still write success. Deduct money and add money operation should be in the same transaction. The appropriate transaction management is required.

Four. Spring's programmatic transaction management five. Spring's declarative transaction management

Spring003--spring Transaction Management (MOOC)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.