1、abstraction
This section describes some semantics of transaction propagationin Spring. Please note that this section is not an introduction to transaction propagation proper; rather it details some of the semantics regarding transaction propagation in Spring.In Spring-managed transactions, be aware of the difference between physical and logical transactions, and how the propagation setting applies to this difference.
2、required
PROPAGATION_REQUIRED
When the propagation setting is
PROPAGATION_REQUIRED
, a
logical transaction scope is created for each method upon which the setting is applied. Each such logical transaction scope can determine rollback-only status individually,with an outer transaction scope being logically independent from the inner transaction scope. Of course, in case of standard
PROPAGATION_REQUIRED
behavior, all these scopes will be mapped to the same physical transaction. So a rollback-only marker set in the inner transaction scope does affect the outer transaction's chance to actually commit (as you would expect it to).
However, in the case where an inner transaction scope sets the rollback-only marker, the outer transaction has not decided on the rollback itself, and so the rollback (silently triggered by the inner transaction scope) is unexpected. A corresponding
UnexpectedRollbackException
is thrown at that point. This is
expected behavior so that the caller of a transaction can never be misled to assume that a commit was performed when it really was not. So if an inner transaction (of which the outer caller is not aware) silently marks a transaction as rollback-only, the outer caller still calls commit. The outer caller needs to receive an
UnexpectedRollbackException
to indicate clearly that a rollback was performed instead.
3、requiresNew
PROPAGATION_REQUIRES_NEW
PROPAGATION_REQUIRES_NEW
, in contrast to
PROPAGATION_REQUIRED, uses a
completely independent transaction for each affected transaction scope. In that case, the underlying physical transactions are different and hence can commit or roll back independently, with an outer transaction not affected by an inner transaction's rollback status.
4、nested
PROPAGATION_NESTED
uses a
single physical transaction with multiple savepoints that it can roll back to. Such partial rollbacks allow an inner transaction scope to trigger a rollback
for its scope, with the outer transaction being able to continue the physical transaction despite some operations having been rolledback. This setting is typically mapped onto JDBC savepoints, so will only work with JDBC resource transactions.
See Spring's
DataSourceTransactionManager
.
5、supports
supports the current transaction, or running if none exists transaction.6、mandatory
supports the current transaction and throws an exception if none exists transaction.7、notSupported
current transaction is suspended , or running if none exists transaction.8、never
throws an exception if exists transaction.ref doc:
1. http://docs.spring.io/spring/docs/3.0.x/spring-framework-reference/html/transaction.html