Learning Spring-Data-Jpa (XXII) --- Transaction

1、@EnableTransactionManagement

  This annotation is used to enable Spring's annotation-driven transaction management capabilities for @Configuration class. After configuring Spring will be responsible for registering the necessary components for annotation-driven transaction management to power. For example TransactionInterceptor based notification agent or AspectJ, and when you call the Add method @Transactional will weave into the interceptor stack.

  proxyTargetClass: whether to create subclasses based agent (CGLIB), rather than agent-based (JDK dynamic proxies) standard Java interface, the default is false. Only when the mode is set to AdviceMode.PROXY if applicable. Note that setting this property to true will affect all the necessary proxy Spring-managed bean, not just marked as @Transactional the bean. For example, comment tags @Async bean will be upgraded to other subclasses agent.
  mode: the way the transaction notification. The default mode is the agent AdviceMode.PROXY. It should be noted that the agency model only allows agents to intercept calls. Local calls in the same class can not be intercepted in this way; @Transactional local call this class will be ignored. For more advanced interceptor mode, you can consider switching to AdviceMode.ASPECTJ, if the mode is set to AdviceMode.ASPECTJ, proxyTargetClass property value is ignored. Also note that, in this case, spring-aspects module JAR must appear on the class path, in this case does not involve a proxy; local calls will be blocked.
  order: execution order of the plurality of transaction processing application specific connection point of notification. The default value is Ordered.LOWEST_PRECEDENCE.

 

  Under normal circumstances, we need to open transaction management in our class configuration @EnableTransactionManagement add comments. This SpringBoot also automatically configured for us. spring-boot-autoconfigure the spring.factories inside the default load org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration View source which can be seen already added this comment by adding @Transactional annotation configuration. We can know by looking, SimpleJpaRepository each method are all matters. For read operations, the transaction readOnly configured to true. Other operating configuration is a common @Transactional, in order to apply the default transaction configuration.

     

2、@Transactional

  Attribute describes a single transaction method or class. At the class level, this annotation to declare the class and all its subclasses method as the default. Note that it does not apply to ancestor classes on the class hierarchy;

    value / transactionManager: Specifies the transaction manager. the name of the bean. When a plurality of data sources is provided.
    propagation: transaction propagation behavior. The default is Propagation.REQUIRED.
    isolation: transaction isolation level. The default is Isolation.DEFAULT. The basic need to set.
    timeout: Set transaction timeout in seconds, never default timeout.
    readOnly: Set whether the transaction is read-only. If a valid reading from the transaction, set up, it will make the appropriate run-time optimization is true. The default is false.
    rollbackFor: specify which exceptions were encountered transaction rollback. By default, the rollback on a RuntimeException and Error.
    rollbackForClassName: features such as rollbackFor, just here to fill a string.
    noRollbackFor: specify which exceptions encountered transaction is not rolled back.
    noRollbackForClassName: features such as noRollbackFor, just here to fill a string.

3, Propagation propagation behavior Affairs

  The propagation of affairs means that: If, before the start of the current transaction, a transaction context already exists, then there are several options you can specify execution behavior of a transactional approach.
    Propagation enumeration class There are seven propagation behavior
      REQUIRED: support for the current transaction, if does not exist, create a new business.
      SUPPORTS: support for the current transaction, and if not, perform non-transactional matters.
      MANDATORY: support for the current transaction, throw an exception if it does not exist.
      REQUIRES_NEW: create a new transaction, if there is a transaction, the current transaction is suspended.
      NOT_SUPPORTED: performing non-transactional operations, if so, to suspend the current transaction.
      NEVER: perform non-transactional operations, if there is a transaction, an exception is thrown.
      NESTED: If the current transaction exists, create a transaction to run as the current nested transaction transaction; if no transaction, the value is equivalent to REQUIRED.

 

Source address: https: //github.com/caofanqi/study-spring-data-jpa

Guess you like

Origin www.cnblogs.com/caofanqi/p/12174049.html