About a few little things spring in transaction management

Transaction Management 1.Spring

  • As an enterprise-class application framework, Spring defines an abstraction layer on top of different transaction management API. The application developer does not have to understand the underlying transaction management API, you can use Spring's transaction management mechanisms.
  • Spring supports both programmatic transaction management, transaction management also supports declarative.
  • Programming Services Management: The transaction management code embedded in the business process to control the transaction commit and rollback. When programmatic transaction management, transaction management must include additional code in every transaction operation.
  • Declarative transaction management: In most cases better than using programmatic transaction management. It keeps transaction management code is separated from the business methods, declaratively to implement transaction management. Transaction management as a crosscutting concerns, can be modularly by AOP method. Spring declarative transaction management support through SpringAOP framework.

The transaction manager 2.Spring

  • Spring abstract a set of transaction mechanism from different transaction management API. Developers do not understand the underlying transaction API, you can use these transaction mechanism. With this transaction mechanism, transaction management code can be independent of the specific transaction technology.
  • Spring core transaction management abstraction TransactionManager management method of encapsulating a set of independent art. Regardless of the kind of Spring's transaction management strategy (programmatic or declarative), the transaction manager is required.
  • DataSourceTransactionManager: the application only needs to handle a data source and stored through JDBC.
  • JtaTransactionManager: JTA transaction management on JavaEES application server.
  • HibernateTransactionManager: Hibernate frame with access to the database.
  • Organizer statement SpringIOC container to form a common Bean.

    Notice declarative transaction management 3. affairs

  • Transaction management is a crosscutting concerns.
  • To enable declarative transaction management in Spring 2.x, you can be defined by tx Schema in Element declares the transaction notice. To do this you must first be added to the Schema definitions Go to the root element.
  • After declaring the transaction notification, you need to associate it with the starting point, because the transaction is in the notification Elements of external declarations, which can not generate index directly associated with the entry point, it must be Enhancer elements declared in a notice associated with the entry point.
  • Because Spring AOP is agent-based approach, the index can only enhance public methods. Therefore, only public methods to transaction management through Spring AOP.
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
         <property name="dataSource" ref="dataSource"></property>
    </bean>
    
    <tx:advice id="bookShopTxAdvice" transaction-manager="transactionManager"></tx:advice>
    
    <aop:config>
        <aop:pointcut id="bookShopOperation" expression="execution(* *.BookShopService.*(..))"></aop:pointcut>
        <aop:advisor advice-ref="bookShopTxAdvice" pointcut-ref="bookShopOperation"/>
    </aop:config>

4. @Transactional annotation declarative transaction management

  • In addition to the statement in the configuration file with the entry point for Bean, notifications and booster of the transaction, Spring also allows simple annotation to mark transactions with @Transactional method.
  • In order to define the method to support transaction processing, you can add annotations to @Transactional method. According to Spring AOP proxy-based, marked only public methods.
  • You can add @Transactional annotation on the method or the class level. When this annotation is applied to the class, all public methods of this class will be defined to support transaction processing.
  • Only needs to be enabled in the configuration file Bean Element for, and specify the transaction processor on it.
  • If the name of the transaction processor is transactionManager, you can transaction-manager attribute elements may be omitted. This element will automatically detect the name of the transaction processor.
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    
    <context:component-scan base-package="com.desparado.service"></context:component-scan>
    
    <tx:annotation-driven/>

The transaction propagation properties

  • When the transaction method is invoked by another transaction method, you must specify how the transaction should spread, for example: the method may continue to operate the existing transaction, or it may open a new transaction and run their own affairs.
  • The propagation of the transaction may be specified by the transport properties, Spring defines seven kinds of propagation behavior.
Propagation properties description
REQUIRED If there is a transaction in operation, the current method runs in the internal affairs, otherwise, it starts a new transaction and run their own affairs.
REQUIRED_NEW The current method must start a new transaction and run within its own transaction, if the transaction is running, it should be suspended
SUPPORTS If there is a transaction in operation, the current method will run in that transaction, otherwise it may not be allowed in a transaction
NOT_SUPPORTED The current method should not be run in a transaction, if the transaction is running, it will hang
MANDATORY The current method must run within a transaction, if the transaction did not have a running, throwing an exception
NEVER The current method should not be run in a transaction, if the transaction has run, ran out abnormal
NESTED If there is a transaction in operation, the current method should be run in a nested transaction this transaction, otherwise, it starts a new transaction and run within its own transaction
  • Configuring transaction propagation properties
    configured in the properties of the annotation in @Transactional
@Transactional(propagation=Propagation.REQUIRES_NEW)

In xml can Configured in

<tx:advice id="bookShopTxAdvice" transaction-manager="transactionManager">
    <tx:attributes>
          <tx:method name="purchase" propagation="REQUIRES_NEW"/>
    </tx:attributes>
</tx:advice>
    

6. Set transaction isolation properties

  • You can set the isolation level isolation property when used in @Transactional @Transactional annotation declarative transaction management
@Transactional(propagation=Propagation.REQUIRES_NEW,isolation=Isolation.READ_COMMITTED)
  • In Spring 2.x transaction notifications, you can <tx: Specifies the isolation level method "element
<tx:advice id="bookShopTxAdvice" transaction-manager="transactionManager">
    <tx:attributes>
          <tx:method name="purchase" propagation="REQUIRES_NEW" 
              isolation="READ_COMMITTED"/>
    </tx:attributes>
</tx:advice>

7. Set the transaction is rolled back property

  • By default, only unchecked exceptions (RuntimeException and Error type of exception) will cause a rollback, two exceptions do not inspected.
  • Rollback transaction rules may be defined by @Transactional annotation rollbackFor and noRollbackFor properties, these two attributes are declared as Class [] type and therefore can specify multiple exception classes for these two properties.
    - rollbackFor: rollback must be met.
    - noRollbackFor: a set of exception classes, the face must not roll

    @Transactional(propagation=Propagation.REQUIRED_NEW,
                  isolation=Isolation.READ_COMMITTED,
                  rollbackFor=(IOException.class,SQLException.class),
                  noRollbackFor=ArithmeticException.class)  
    public void purchase(String isbn,String username){}
  • In Spring 2.x transaction notifications, you can <tx: Specifies the rollback rule method "primitive species, if there is more than one abnormal, separated by commas
<tx:advice id="bookShopTxAdvice" transaction-manager="transactionManager">
   <tx:attributes>
         <tx:method name="purchase" propagation="REQUIRES_NEW" 
             isolation="READ_COMMITTED"
             rollback-for="java.io.IOException,java.sql.SQLException" 
             no-rollback-for="java.lang,ArithmeticException"/>
   </tx:attributes>
</tx:advice>

8. timeout and read-only attributes

  • Because the transaction may obtain locks in rows and tables, so long transaction will take up resources, and the impact on overall performance.
  • If a transaction reads only the data but not modify, the database engine can be optimized for this transaction.
  • Timeout transaction attributes: Prior to force a rollback of the transaction can be kept long. This prevents resource-intensive long-running transaction.
  • Read-only transaction attributes: that this transaction reads only the database but does not update the database, which can help to optimize transactional database engine.
  • Timeout and read-only attributes can be defined @Transactional annotation, the timeout in seconds property.
@Transactional(propagation=Propagation.REQUIRED_NEW,
                isolation=Isolation.READ_COMMITTED,
                rollbackFor=(IOException.class,SQLException.class),
                noRollbackFor=ArithmeticException.class,
                readOnly=true,tomeout=30)  
public void purchase(String isbn,String username){}

- In Spring 2.x transaction advice, the timeout and read-only attributes can be Elements specified

<tx:advice id="bookShopTxAdvice" transaction-manager="transactionManager">
    <tx:attributes>
          <tx:method name="purchase" propagation="REQUIRES_NEW" 
              isolation="READ_COMMITTED"
              rollback-for="java.io.IOException,java.sql.SQLException" 
              no-rollback-for="java.lang,ArithmeticException"  
              read-only=true
              timeout=30/>
    </tx:attributes>
</tx:advice>

Guess you like

Origin www.cnblogs.com/jack1995/p/10953054.html