Sometimes do one thing is very easy, but one thing is difficult to maintenance, now a lot of technology, in fact many of them are in processing in the event of a problem, how to remedy, if many technical difficulties is not complete one thing, but an error has occurred what to save, the transaction is the practice of this idea, the following about the affairs of the Spring.

I learned it in code, so I just pasted it.

/** * REQUIRED is the default transaction propagation * if the thread context has a transaction, the thread context's transaction is used * if not, the specified transaction is used * because it is the default, so it can not write *@param user* /
@Transactional(propagation = Propagation.REQUIRED)
public void requiredExample(String user) {
    // ...
}
/** * SUPPORTS transaction propagation * if the thread context has a transaction, use the thread context transaction * If not then this method does not use transactions, should affect inside *@param user* /
@Transactional(propagation = Propagation.SUPPORTS)
public void supportsExample(String user) {
    // ...
}
/** * MANDATORYS transaction propagation * Throws an exception if the thread context has no transactions *@param user* /
@Transactional(propagation = Propagation.MANDATORY)
public void mandatoryExample(String user) {
    // ...
}
/** * transaction propagation of NEVER * Throws an exception if the thread context has a transaction *@param user* /
@Transactional(propagation = Propagation.NEVER)
public void neverExample(String user) {
    // ...
}

/** * NOT_SUPPORTED transaction propagation * Suspends the transaction if there is a transaction in the thread context, and then executes this method without using a transaction *@param user* /
@Transactional(propagation = Propagation.NOT_SUPPORTED)
public void notSupportedExample(String user) {
    // ...
}
/** * Transaction propagation for REQUIRES_NEW * If the thread context has a transaction, suspend the transaction and create a new transaction to execute the method *@param user* /
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void requiresNewExample(String user) {
    // ...
}
If the thread context has a transaction, create a savepoint to which rollback will be rolled back@param user* /
@Transactional(propagation = Propagation.NESTED)
public void nestedExample(String user) {
    // ...
}
Copy the code

In addition to spring’s transaction isolation level, the isolation level of the database can also be defined in Spring, which is the same as the database.

And to say a little bit more

/** % if a check or uncheck exception is thrown, it will be rolled back */
@Transactional(rollbackFor = Exception.class)
public void exceptionExample(String user) throw exception {
    // ...
}
Copy the code

This code will be rolled back if caught in the service layer

/** % only check exceptions are rolled back */
@Transactional(rollbackFor = Exception.class)
public void exceptionExample(String user) throw exception {
    // ...
}
Copy the code

So plus

Transactional(rollbackFor = exception.class) is very important