A thorough understanding of the handwriting implementation of Spring's concept of business design

Source: Internet
Author: User

Objective

A transaction is an abstraction that describes a set of operations, such as a set of operations on a database that either succeeds or fails altogether. Transactions have 4 properties: atomicity (atomicity), consistency (consistency), isolation (isolation), durability (persistence). In the actual development, we apply the most to the transaction in the database operation, especially spring to the database transaction encapsulation management. Spring's support for transactions is really powerful, but essentially: does the transaction take effect depending on whether the database is supported at the bottom (such as MySQL's MyISAM engine does not support transactions, Spring can do!) ), while multiple operations for a transaction need to be on the same connection. Transactions are also often controlled at the business logic level. This blog will use a handwritten demo to analyze how the bottom of the spring transaction can help us to complete transaction management easily!

A thorough understanding of the handwriting implementation of Spring's concept of business design

Let's take a look at the engineering structure:

Connectionholder

In spring, do we sometimes have to configure multiple data source DataSource? It is clear that spring needs to get the pipeline connection for manipulating the database through DataSource, which is somewhat similar to Jndi lookups.

This process is accomplished through the Connectionholder class, and it is clear that there is a problem with multithreading. To avoid multithreading problems, do we use a thread-safe map, such as Concurrenthashmap, in fact, what is our real purpose? is to ensure that a single thread, a transaction of multiple operations to get is a connection, obviously use CONCURRENTHASHMAP simply cannot guarantee!

Spring is smart, she provides a way to solve, look at the code below!

Singlethreadconnectionholder

Originally thread insecure, through threadlocal so encapsulated, immediately become a thread of local variables, not only security, but also to ensure that a thread under the operation to get the connection is the same object! This kind of thought is very ingenious indeed, this is also a way of the lock-free programming idea!

TransactionManager

TransactionManager, this is something we often configure in spring, the big butler!

Useraccountdao, Userorderdao

Here through the 2 DAO, want to simulate a transaction in the account purchase, the next order 2 operations.

UserService

Here, you can clearly see the epitome of spring transaction management!

Test

Here, the main is to simulate the injection of spring and multi-user concurrent requests.

Run results

You can find that multiple operations of one transaction in a thread are using the same connection!

Well, here, can you understand the idea of spring's realization of the business?

A thorough understanding of the handwriting implementation of Spring's concept of business design

Related Article

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.