SSM study notes 5 (declarative transaction control)

Declarative transaction control

1. Programmatic transaction control related objects

1.1 PlatformTransactionManager

The PlatformTransactionManager interface is spring's transaction manager, which provides the methods we commonly use to manipulate transactions.

Insert picture description here

note:

PlatformTransactionManager is an interface type, and different Dao layer technologies have different implementation classes. For example, when Dao layer technology is jdbc or mybatis: org.springframework.jdbc.datasource.DataSourceTransactionManager

When the Dao layer technology is hibernate: org.springframework.orm.hibernate5.HibernateTransactionManager

1.2 TransactionDefinition

TransactionDefinition is the definition information object of the transaction, which has the following methods:
Insert picture description here

2 XML-based declarative transaction control

2.1 What is declarative transaction control

Spring's declarative transaction, as its name suggests, is to use a declarative approach to handle transactions. The declaration mentioned here refers to the declaration in the configuration file, and the declarative transaction processing in the Spring configuration file is used instead of the code processing transaction.

The role of declarative transaction processing

  • Transaction management does not invade the developed components. Specifically, business logic objects will not realize that they are in transaction management, and in fact they should, because transaction management is a system-level service, not part of business logic. If you want to change the transaction management plan, You only need to reconfigure in the definition file

  • When transaction management is not required, you can remove the transaction management service as long as you modify the setting file without changing the code and recompiling, which is extremely convenient to maintain

Note: The bottom layer of Spring's declarative transaction control is AOP.

2.2 Implementation of declarative transaction control

Declarative transaction control clear matters:

  • Who is the cut point?

  • Who is the notification?

  • Configuration aspect?

  • ①Introduce tx namespace

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">


②Configuration transaction enhancement

<!--平台事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"></property>
</bean>

<!--事务增强配置-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
    <tx:attributes>
        <tx:method name="*"/>
    </tx:attributes>
</tx:advice>

③Configuration transaction AOP weaving

<!--事务的aop增强-->
<aop:config>
    <aop:pointcut id="myPointcut" expression="execution(* com.itheima.service.impl.*.*(..))"/>
    <aop:advisor advice-ref="txAdvice" pointcut-ref="myPointcut"></aop:advisor>
</aop:config>

④Test transaction control transfer business code

@Override
public void transfer(String outMan, String inMan, double money) {
    
    
    accountDao.out(outMan,money);
    int i = 1/0;
    accountDao.in(inMan,money);
}

3 Annotation-based declarative transaction control

3.1 Use annotations to configure declarative transaction control

  1. Write AccoutDao
@Repository("accountDao")
public class AccountDaoImpl implements AccountDao {
    
    
    @Autowired
    private JdbcTemplate jdbcTemplate;
    public void out(String outMan, double money) {
    
    
        jdbcTemplate.update("update account set money=money-? where name=?",money,outMan);
    }
    public void in(String inMan, double money) {
    
    
        jdbcTemplate.update("update account set money=money+? where name=?",money,inMan);
    }
}
  1. Write AccoutService
@Service("accountService")
@Transactional
public class AccountServiceImpl implements AccountService {
    
    
    @Autowired
    private AccountDao accountDao;
    @Transactional(isolation = Isolation.READ_COMMITTED,propagation = Propagation.REQUIRED)
    public void transfer(String outMan, String inMan, double money) {
    
    
        accountDao.out(outMan,money);
        int i = 1/0;
        accountDao.in(inMan,money);
    }
}
  1. Write applicationContext.xml configuration file
<!—之前省略datsSource、jdbcTemplate、平台事务管理器的配置-->
<!--组件扫描-->
<context:component-scan base-package="com.itheima"/>
<!--事务的注解驱动-->
<tx:annotation-driven/>

3.2 Annotation configuration declarative transaction control analysis

① Use @Transactional to modify the classes or methods that need transaction control, and the available attributes for annotations are the same as the xml configuration methods, such as isolation level, propagation behavior, etc.

②The annotation is used on the class, then all methods under the class use the same set of annotation parameter configuration.

③In terms of methods, different methods can use different transaction parameter configurations.

④The annotation drive of the transaction to be opened in the Xml configuration file <tx:annotation-driven />

3.3 Knowledge points

Key points of configuration for declarative transaction control

  • Platform transaction manager configuration (xml mode)

  • Configuration of transaction notification (@Transactional annotation configuration)

  • Transaction annotation-driven configuration tx:annotation-driven/

Guess you like

Origin blog.csdn.net/weixin_45394002/article/details/113176414