spring04-transaction

spring xml configuration transaction (transaction using xml configuration, other ioc bean using annotations)


  1. Configuring Transaction Manager

    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!-- 引用数据源bean -->
        <property name="dataSource" ref="dataSource"/>
    </bean>
  2. Notification Configuration affairs

    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="*" propagation="REQUIRED" read-only="false"/>
            <tx:method name="get*" propagation="SUPPORTS" read-only="true"/>
            <tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
        </tx:attributes>
    </tx:advice>
    • Use tx: advice tab to configure the transactional advice
      1. Attributes:
        • Uniquely identifies the transaction: id
        • transaction-manager: to provide a notice to the transaction referenced transaction manager
  3. General configuration of AOP pointcut expression
    <aop:pointcut id="txPc" expression="execution(* cn.ann.service..*.*(..))"/>
  4. Establishing correspondence between the transaction and the starting point notification expressions
    <aop:advisor advice-ref="txAdvice" pointcut-ref="txPc"/>
  5. Configure transaction properties
    • Use tx: internal advice label tx: attributes tab to configure the transaction attributes

      <tx:attributes>
          <tx:method name="*" propagation="REQUIRED" read-only="false"/>
          <tx:method name="get*" propagation="SUPPORTS" read-only="true"/>
          <tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
      </tx:attributes>
      1. name: To configure the transaction method * Why is a wildcard.
      2. isolation: for the specified transaction isolation level. The default value is DEFAULT, represents the default isolation level to use the database.
      3. propagation: propagation behavior for the specified transaction. The default is REQUIRED, it indicates there will be a transaction, additions and deletions to the selection. Query method can be selected SUPPORTS
      4. read-only: used to specify whether a transaction is read-only. Only query methods can be set to true. The default value is false, meaning to read and write
      5. timeout: Timeout time for the specified transaction, the default value is -1, which means never time out. If a value is specified in seconds
      6. rollback-for: When used to specify an exception, when the exception is generated, the transaction is rolled back, have other abnormalities, it does not roll back the transaction. There is no default value. Indicate any exceptions rollback
      7. no-rollback-for: specifies an exception, when the abnormality is generated, no rollback transaction, transaction rollback produce other abnormalities. There is no default value. Indicate any exceptions rollback


Pure spring Affairs configuration notes


  1. The need to create a bean (data source, transaction management, etc.)
  2. Use annotations on configuration class open transaction management: @EnableTransactionManagement
  3. Configuring General pointcut expression:

    @Pointcut("execution(* cn.ann.service..*.*(..))")
    public void txPc(){}
  4. Configuring transaction for each method you need to configure the transaction attributes

    @Transactional(propagation = Propagation.REQUIRED, readOnly = false)
    public void transfer(String source, String target, Double money) {
        ...
    }
    
    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    public Account getAccountByName(String name) throws SQLException {
        ...
    }
    
    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    public Account getAccountById(Integer id) throws SQLException {
        ...
    }


spring programmatic transaction control


  1. Create a transaction manager bean

    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
  2. Create a transaction template bean

    <bean id="txTemplate" class="org.springframework.transaction.support.TransactionTemplate">
        <property name="transactionManager" ref="transactionManager"/>
    </bean>
  3. Write business code

    @Service("accountService")
    public class AccountServiceImpl implements AccountService {
        @Resource(name = "txTemplate")
        private TransactionTemplate txTemplate;
        @Resource(name = "accountDao")
        private AccountDao dao;
    
        @Override
        public void transfer(String source, String target, Double money) {
            txTemplate.execute(status -> {
                try {
                    Account sourceAccount = dao.getAccountByName(source);
                    Account targetAccount = dao.getAccountByName(target);
                    sourceAccount.setAccountMoney(sourceAccount.getAccountMoney() - money);
                    dao.updateAccount(sourceAccount);
    //                int i = 1 / 0;
                    targetAccount.setAccountMoney(targetAccount.getAccountMoney() + money);
                    dao.updateAccount(targetAccount);
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                return null;
            });
        }
    
        @Override
        public Account getAccountById(Integer id) throws SQLException {
            return txTemplate.execute(status -> {
                try {
                    return dao.getAccountById(id);
                } catch (SQLException e) {
                    e.printStackTrace();
                    throw new RuntimeException(e);
                }
            });
        }
    
        @Override
        public Account getAccountByName(String name) throws SQLException {
            return txTemplate.execute(status -> {
                try {
                    return dao.getAccountByName(name);
                } catch (SQLException e) {
                    e.printStackTrace();
                    throw new RuntimeException(e);
                }
            });
        }
    }
  • In the code can see a lot of repeat part: txTemplate.execute code in a lambda expression, so the feeling is not so "shocked" For this reason, the development of the basic need programmatic transaction control.

In the development, or comment on the use of configuration files: In the company there is no mandatory requirement, how convenient how come I generally use the profile configuration issues.
The film Code: here all the spring04

Guess you like

Origin www.cnblogs.com/ann-zhgy/p/11787141.html