Spring transaction annotation @Transactional

Spring transaction annotation @Transactional
transaction management generally have two kinds of programmatic and declarative programming style is to write things in the process directly in code, type and reputation is or configured in the xml file by way of comment, relatively programmatic very convenient.

The comment by way @Transactional are common. We can use @EnableTransactionManagement annotations to enable transaction management capabilities, the notes can be added to the startup class or individually configured to add a class to handle.

1, Transactional annotation attribute
name when there are multiple TransactionManager in the configuration file, you can use this property to specify which transaction manager selection.
the propagation of propagation of the transaction, the default is REQUIRED.
isolation isolation transaction, uses as defaults DEFAULT.
timeout transaction timeout, the default value is -1. If you exceed the time limit but the transaction has not been completed, it automatically rolls back the transaction.
read-only transaction is read-only transaction specified, the default is false; not required to ignore the transaction process, such as reading data, can be read-only set to true.
rollback-for triggering exception type can be used to specify the transaction rollback, if there is need to specify multiple exception types, may be separated by commas types.
no-rollback- for throwing an exception type no-rollback-for specified does not roll back the transaction.
  propagation properties (propagation of affairs)

REQUIRED support services currently existing, if it is not a transaction, create a new business.
  MANDATORY of an existing support services, if it is not a transaction, an exception is thrown.
  NESTED create a nested transaction in the current transaction, if the transaction has not, then simply create a new business.
  REQUIRES_NEW suspend the current transaction, create a new transaction, if the transaction has not, you simply create a new business.
  NEVER run a transaction is not mandatory, if there is a current transaction, an exception is thrown.
  NOT_SUPPORTED forced transaction is not running, if there is a current transaction, then the transaction is pending.
  SUPPORTS support the current transaction, if the transaction is not then do not run in a transaction.
2, Transactional application
@Transactional can be added to the method, this method shows an arrangement of the transaction may also be added to the class level.

You can also add to the class level. When the @Transactional annotation on the class level, represent all public methods of the class configured with the same transaction attribute information.

When the class level configuration of @Transactional, method level also equipped with @Transactional, the application to method level attribute information to the transaction management services, namely transaction attributes will override class-level method-level configuration information.

3, Transactional working principle of
declarative transaction management consists of three components:

Section Affairs

Transaction Manager

EntityManager Proxy itself
cut Affairs

Cut transaction is a "around (Surround)" section in the front and back annotation of business methods can be called. Implement the aspect of a specific class is TransactionInterceptor. Cut transaction has two primary responsibilities:

In the 'before', section provide a point of call, to determine the method is called business should be run within the scope of the ongoing transaction, or start a new independent business.

When 'after', section need to determine the transaction is committed or rolled back continues to run.

In the 'before', transaction advice itself does not contain any decision logic, delegating decision whether to start a new transaction to complete the transaction manager.

Transaction Manager

The transaction manager need to address the following two questions:

Whether the new Entity Manager should be created?

Whether we should start a new business?

When the decision to cut 'before' logic is invoked these needs Affairs. The transaction manager decisions based on the following two points:

Whether the transaction is in progress

propagation property transaction methods (such as REQUIRES_NEW always want to start a new transaction)

If the transaction manager you sure you want to create a new transaction, it will:

Create a new entity manager

entity manager is bound to the current thread

Obtaining a connection from the database connection pool

Connect bound to the current thread

Use ThreadLocal variable entity manager and database connections are bound to the current thread. When they run the transaction is stored in the thread, when they are no longer being used, the transaction manager to decide whether to remove them. If any part of the program requires the current entity manager and database connections are available from the thread.

EntityManager proxy

When a business method invocation similar entityManager.persist () method, which is not called directly by the entity manager, but the agency business method calls, because things Manager entity manage bound to the thread, the agent to get the current entity manager from thread .

4, Notes
  4.1 @Transactional annotation methods applied to the public, in order to carry out transaction management. Because aop will intercept whether public method:

Copy the code
// AbstractFallbackTransactionAttributeSource class

protected TransactionAttribute computeTransactionAttribute(Method method,
    Class<?> targetClass) {
        // Don't allow no-public methods as required.
        if (allowPublicMethodsOnly() && !Modifier.isPublic(method.getModifiers())) {
        return null;

}
  }
Copy the code
  4.2 propagation properties

The following three propagation can not start a transaction. Incorrect configuration of the three propagation, transaction rollback may not occur.

TransactionDefinition.PROPAGATION_SUPPORTS: If the current transaction exists, it is added to the transaction; if no transaction, non-transactional way places continue to run.
TransactionDefinition.PROPAGATION_NOT_SUPPORTED: non-transaction run, if the current transaction exists, put the current transaction pending.
TransactionDefinition.PROPAGATION_NEVER: non-transaction run, if the current transaction exists, an exception is thrown.
  4.3 rollbackFor attribute
  By default, if unchecked exceptions thrown in a transaction (inherited from RuntimeException exception) or Error, the Spring rolls back the transaction; In addition, Spring will not roll back the transaction.
  You can develop other types of exceptions thrown in by rollbackFor things to support the transaction is rolled back, for example:

@Transactional (propagation = Propagation.REQUIRED, rollbackFor = MyException.class)
  if thrown in the target process is abnormal abnormal subclass rollbackFor specified, the transaction will also be rolled back.

4.4 In the default proxy mode, only the target method is called by the external, can be intercepted Spring's transaction interceptor. In a direct call to the same two methods in a class, it will not be intercepted Spring's transaction interceptors

Such as the following two methods in the same class plus control transaction, wherein the transaction is not in effect the method, a method is to write it to another column, then the current class calls

Copy the code

@Transactional(propagation = Propagation.REQUIRED)
@Override
public void save() {



    method();

    。。。业务处理

    if (true) {
        throw new RuntimeException("save 抛异常了");
    }
}

@Transactional(propagation = Propagation.REQUIRES_NEW)
public void method() {
    。。。业务处理
}
复制代码
Published 22 original articles · won praise 6 · views 368

Guess you like

Origin blog.csdn.net/weixin_45519387/article/details/103940219