事务

事务

  • 事务是数据库操作追基本单元,是逻辑上一组的操作,要么都成功,如果有一个操作失败则都失败
  • 经典场景(银行转账)1,a转100给b 2.a少100,b多100
  • 事务的十大特性(ACID): 1.原子性 2.一致性 3.隔离性 4.持久性

事务操作

  • 事务添加到JavaEE三层中的Service中
  • 在Spring中进行事务管理操作(编程式[代码]和声明式[xml和注解])

编程式

        try{
            // 开启异常
            // id1 少 money元
            tempUserDao.reduce(id1,money);
            // 模拟异常
            int i = 10/0;
            // id2 多 money元
            tempUserDao.add(id2,money);
        }catch (Exception e){
            // 出现异常,事务回滚
        }

声明式(注解)

<?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/tx http://www.springframework.org/schema/tx/spring-tx.xsd
                           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

        <context:component-scan base-package="com.hncj"></context:component-scan>

        <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
            <property name="url" value="jdbc:mysql://***.**.**.**:3306/jpa"></property>
            <property name="username" value="root"></property>
            <property name="password" value="123456"></property>
            <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        </bean>

        <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
            <property name="dataSource" ref="dataSource"></property>
        </bean>

        <!-- 创建事务管理器 -->
        <bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <!-- 注入数据源 -->
                <property name="dataSource" ref="dataSource"></property>
        </bean>
        <!-- 开启事务注解 -->
        <tx:annotation-driven transaction-manager="dataSourceTransactionManager"></tx:annotation-driven>

</beans>
  • @Transactional可以添加到类上或方法上
    @Transactional
    public void usermoney(Integer id1,Integer id2,Integer money) throws Exception {
        // 开启异常
        // id1 少 money元
        tempUserDao.reduce(id1,money);
        // 模拟异常
        int i = 10/0;
        // id2 多 money元
        tempUserDao.add(id2,money);
    }

声明式事务管理参数配置

propagation(事务传播行为)

islation(事务隔离级别)

  • 事务特有性成为隔离性,多事务之前不会产生影响,不考虑隔离性会产生各种问题

timeout(事务超时时间)

  • 需要一定时间内提交,如果超时就会回滚事务

readOnly(事务是否只读)

  • readOnly 默认值 false,表示可以查询,可以添加修改删除操作
  • 设置 readOnly 值是 true,设置成 true 之后,只能查询

rollbackFor

  • 设置出现哪些异常进行事务回滚

noRollBackFor

  • 设置出现哪些异常不进行事务回滚

事务管理(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/tx http://www.springframework.org/schema/tx/spring-tx.xsd
                           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

        <context:component-scan base-package="com.hncj"></context:component-scan>

        <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
            <property name="url" value="jdbc:mysql://***.**.**.**:3306/jpa"></property>
            <property name="username" value="root"></property>
            <property name="password" value="123456"></property>
            <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        </bean>

        <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
            <property name="dataSource" ref="dataSource"></property>
        </bean>

        <!-- 创建事务管理器 -->
        <bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <!-- 注入数据源 -->
                <property name="dataSource" ref="dataSource"></property>
        </bean>
        <!-- 开启事务注解 -->
        <tx:annotation-driven transaction-manager="dataSourceTransactionManager"></tx:annotation-driven>

        <!-- 配置通知 -->
        <tx:advice id="txadvice">
            <!-- 配置通知 -->
            <tx:attributes>
                <tx:method name="usermoney" propagation="REQUIRED"/>
                <tx:method name="usermoney" isolation="SERIALIZABLE"/>
                <tx:method name="usermoney" read-only="false"/>
                <tx:method name="usermoney" timeout="-1"/>
            </tx:attributes>
        </tx:advice>
        
        <!-- 配置切入点和切面 -->
        <aop:config>
            <!-- 切入点 -->
            <aop:pointcut id="pt" expression="execution(* com.hncj.service.TempUserService.*(..))"/>
            <!--  配置切面 -->
            <aop:advisor advice-ref="txadvice" pointcut-ref="pt"></aop:advisor>
        </aop:config>
</beans>

事务(完全注解开发)

package com.hncj;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

/**
 * @Author guochongling
 * @Date 2021/3/4 1:38 下午
 * @Version 1.0
 */
@Configurable // 标记配置类
@EnableTransactionManagement //  开启事务管理
@ComponentScan(basePackages = "com.hncj") // bean扫描
public class Config {

    // 创建连接
    @Bean
    public DruidDataSource getDruidDataSource(){
        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setDriverClassName("com.mysql.jdbc.Driver");
        druidDataSource.setUrl("jdbc:mysql://106.15.73.43:3306/jpa");
        druidDataSource.setUsername("root");
        druidDataSource.setPassword("123456");
        return druidDataSource;
    }

    // 创建jdbc模板类(方法中的参数是datasource,没有在类中new是因为,已经有了,这样使用可以自动的去容器中匹配)
    @Bean
    public JdbcTemplate getJdbcTemplate(DruidDataSource druidDataSource){
        JdbcTemplate jdbcTemplate = new JdbcTemplate();
        jdbcTemplate.setDataSource(druidDataSource);
        return jdbcTemplate;
    }

    //创建 事务管理器(方法中的参数是datasource,没有在类中new是因为,已经有了,这样使用可以自动的去容器中匹配)
    @Bean
    public DataSourceTransactionManager getdataSourceTransactionManager(DruidDataSource druidDataSource){
        DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager();
        dataSourceTransactionManager.setDataSource(druidDataSource);
        return dataSourceTransactionManager;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

临水而愚

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值