SSM framework of the transaction and transaction propagation mechanism

SSM end integration framework to learn, and finally when exposed to transaction propagation mechanism, a bit more, right on down

0. It is worth noting pit (SSM transaction rollback failure)

In the spring-mvc.xml file, if you scan configuration package when it is scanning all the packages under the project, such as this (my entire project folder in the top file)

Then your transaction mechanism will be problems, because the spring-mvc is a child container , does not have transactional capabilities , while spring-config is a top-level parent container , there is a transaction function , if you scan processing transactions within the spring-mvc.xml service class, the transaction will lead to functional failure

So, remember to exclude the transaction of business class, just put the above code change

. 1      < context: Scan-Component Base-Package = "Top **." > 
2          < context: the exclude filter- type = "Annotation" expression The = "org.springframework.stereotype.Service" />      
. 3  <-! So provided then you can let the child containers are not loaded Servicer -> 
4      </ context: the Component-Scan >

And transactional capabilities to normal

1. Use the transaction (only introduce Comment way)

In the former method is responsible for interacting with the database add comments @Transactional so this method will open a transaction when called, the end of the transaction at the end of the method

example:

. 1      @Transactional   // transaction annotation, related to changes to the database are preferably plus 
2      public  int createStudent (Student student) {
 . 3          studentMapper.insertSelective (student);   // receiving the student and calls insertSelective studentMapper () method writes database 
. 4          return 0 ;
 . 5      }

 

2. Transaction annotation properties

After the transaction can add comments @Transactional brackets, which is written on some of the properties to change the operating mechanism, if not property, they will check for errors encountered transaction rollback

① ignore the error, the transaction is not rolled back (there are risks, with no specific circumstances)

1 the @Transactional (noRollbackFor = RuntimeException. Class ) // The meaning here is that when RuntimeException error occurred during the operation of the transaction, the transaction continues without rollback

 

② meet specified error also rollback

1 the @Transactional (rollbackFor = RuntimeException. Class ) // specify the error encountered have rolled back, for example, you can customize an error class, set this error is to roll back

 3. The transaction propagation mechanism

What is a transaction spread my understanding of him is:? Nested or cross so the situation occurred between the transaction and the transaction when the mechanism to deal with the logical relationships between them run

Simple words, is the relationship between the handles affairs

Usage: same as above, added after a comment (), write the corresponding field

① default mechanism (REQUIRED)

1 @Transactional(propagation = Propagation.REQUIRED)

This is the system default mechanism, characterized by: before performing this transaction, the system will detect before him whether there has been a transaction that he has already been included in a single transaction, and if so, then join the transaction to run, if there is no , create a new transaction is running.

This logic is running in line with 95% of cases, usually use the most is this, anyway, is the default, it certainly pan with high

②SUPPORTS

1 @Transactional(propagation = Propagation.SUPPORTS)

Features: If it is detected in front of the open transaction, added to the transaction, if the transaction is not in front, follow the way of non-transaction run down

③MANDATORY

1 @Transactional(propagation = Propagation.MANDATORY)

Features: The transaction must have a front opening of the transaction; i.e. if the system transaction before the transaction is not open to the detection, the error will not be generally used for performing independent, the code must be run in other transactions, at the same time. there is also a good means to check whether the transaction surrounded

④REQUIRES_NEW

1 @Transactional(propagation = Propagation.REQUIRES_NEW)

Features: Run time here, will establish a new transaction, if the transaction before it is turned on, it will temporarily suspend the transaction before the pause, wait for the transaction to complete before running after it, will continue to run

⑤NOT_SUPPORTED

1 @Transactional(propagation = Propagation.NOT_SUPPORTED)

Features: When you come here, there is no open check before running the affairs of, any, put it hangs suspended, in accordance with their own and other non-transactional after the completion of operational mode, the difference between the previous run of the transaction (and ④ is that it does not itself creates a new transaction)

⑥NEVER

1 @Transactional(propagation = Propagation.NEVER)

Features: running here, in front of the system will check whether there is to open the transaction, if any, will complain, not in accordance with the implementation of non-transactional

⑦NESTED

1 @Transactional(propagation = Propagation.NESTED)

Features: If the front open transaction, or creates a new transaction nest in which to perform, if not previously turned on the transaction, and create a new one transaction execution

Guess you like

Origin www.cnblogs.com/helldiriver/p/11360540.html