Spring--Spring5事物管理(2)

88 阅读2分钟

「这是我参与2022首次更文挑战的第16天,活动详情查看:2022首次更文挑战

事务操作(声明式事务管理参数配置)

@Transactional相关参数

image-20220110230154250

propagation:事务传播行为

(1)事物方法?

事物方法就是对数据库表数据进行变化的操作,如增加删除数据的方法就是事物方法

(2)事物传播行为?

指的就是当一个事物方法被另一个事务方法调用时,这个事务改如何进行

例如当A事物方法调用B事物方法时,B是继续在A事物方法中运行呢,还是为自己新开一个事物运行呢,这就是B事物方法传播行为决定的~

这里视频讲的一笔带过了,想了解详细的话要去补一下mysql基础!

视频这里主要是讲这些在spring中怎么配置

image-20220110231007145

Spring框架事务传播行为有7种

主要了解这两种

REQUIRED

如果add方法本身有事务,调用update方法之后,update使用当前add方法里面事务

如果add方法本身没有事务,调用update方法之后,创建新事务

REQUIRED_NEW

使用add方法调用updato方法,如果add无论是否有事务,都创建新的事务

image-20220110232940154

timeout:超时时间

(1)事务需要在一定时间内进行提交,如果不提交进行回滚

(2)默认值是-1,设置时间以秒单位进行计算

readOnly:是否只读

(1)读:查询操作,写:添加修改删除操作

(2) readOnly 默认值false,表示可以查询,可以添加修改删除操作

(3)设置readOnly值是true,设置成true之后,只能查询

rollbackFor:回滚

(1)设置出现哪些异常进行事务回滚

noRollbackFor:不回滚

(1)设置出现哪些异常不进行事务回滚

事物操作(XML声明式事务管理)

<?xml version="1.0" encoding="UTF-8"?><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/beans http://www.springframework.org/schema/beans/spring-beans.xsd                          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"><!--    组件扫描-->    <context:component-scan base-package="com.caq"></context:component-scan>    <!-- 数据库连接池 -->    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"          destroy-method="close">        <property name="url" value="jdbc:mysql:///user_db" />        <property name="username" value="root" />        <property name="password" value="root" />        <property name="driverClassName" value="com.mysql.jdbc.Driver" />    </bean><!--    JdbcTemplate对象-->    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"><!--        注入dataSource-->        <property name="dataSource" ref="dataSource"></property>    </bean><!--    1.创建事务管理器-->    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><!--        注入数据源-->        <property name="dataSource" ref="dataSource"></property>    </bean><!--    2.配置通知-->    <tx:advice id="txadvice">        <tx:attributes><!--            指定那种规则的方法上面添加事物-->            <tx:method name="accountMoney" propagation="REQUIRED"/><!--            <tx:method name="account*"/>-->        </tx:attributes>    </tx:advice><!--    3.配置切入点和切面-->    <aop:config><!--        给UserService类里的所有方法都运行这个规则-->        <aop:pointcut id="pt" expression="execution(* com.caq.spring5.service.UserService.*(..)"/><!--    配置切面-->        <aop:advisor advice-ref="txadvice" pointcut-ref="pt"></aop:advisor>    </aop:config></beans>
@Autowiredprivate UserDao userDao;public void accountMoney(){    userDao.reduceMoney();    int i = 10/0;    userDao.addMoney();}@Test    public void test2(){        ApplicationContext context = new ClassPathXmlApplicationContext("bean2.xml");        UserService userService = context.getBean("userService", UserService.class);        userService.accountMoney();    }

事物会发生回滚,数据库表数据不变

事务操作(完全注解声明式事务管理)

注解类

package com.caq.spring5.config;import com.alibaba.druid.pool.DruidDataSource;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;import org.springframework.jdbc.core.JdbcTemplate;import org.springframework.jdbc.datasource.DataSourceTransactionManager;import org.springframework.transaction.annotation.EnableTransactionManagement;import javax.sql.DataSource;@Configuration //配置类@ComponentScan(basePackages = "com.caq")    //扫描com.caq包下所有@EnableTransactionManagement    //开启事物public class TxConfig {    //创建数据库连接池    @Bean    public DruidDataSource getDruidDataSource() {        DruidDataSource dataSource = new DruidDataSource();        dataSource.setDriverClassName("com.mysql.jdbc.Driver");        dataSource.setUrl("jdbc:mysql:///user_db");        dataSource.setUsername("root");        dataSource.setPassword("root");        return dataSource;    }    //    JdbcTemplate对象    @Bean    public JdbcTemplate getJdbcTemplate(DataSource dataSource) {//        到ioc容器中根据数据类型找到dataSource        JdbcTemplate jdbcTemplate = new JdbcTemplate();        //        注入dataSource        jdbcTemplate.setDataSource(dataSource);        return jdbcTemplate;    }    //声明事物管理器    @Bean    public DataSourceTransactionManager dataSourceTransactionManager(DataSource dataSource) {        DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();        transactionManager.setDataSource(dataSource);        return transactionManager;    }}

测试

@Test
public void test3(){
    ApplicationContext context = new AnnotationConfigApplicationContext(TxConfig.class);
    UserService userService = context.getBean("userService", UserService.class);
    userService.accountMoney();

}

事物会发生回滚,数据库表数据不变