Spring事务操作-事务引入

本文介绍了Spring事务操作,从模拟异常开始,探讨了在没有使用Spring时如何处理异常,接着详细阐述了Spring声明式事务管理,特别是注解声明式事务的配置与使用,并通过例子展示了事务管理对异常处理的效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目录

Spring事务操作-事务引入

1.模拟异常

2.测试异常

3.没有使用spring框架的时候异常该如何处理

4.使用spring框架的时候异常该如何处理

5.在spring 进行声明式事务管理,底层使用AOP

6.spring 事务管理API

7.事务操作(注解声明式事务管理)

(1)在spring的配置文件中配置事务管理器

(2)在spring 配置文件中,开启事务注解

(3)在service 类上面(或者service类里面的方法上面) 添加事务注解

(4)@Transactional,这个注解添加到类上面,也可以添加到方法上面

(5)测试:


 

 上一章的代码,如果正常执行,   不会出现问题

 

如果在代码执行过程中出现异常,会出现问题

 

因此,我们引入事务这个概念

 

Spring事务操作-事务引入

 

1.模拟异常

在业务逻辑层中的service层中模拟异常

package org.example.spring.service;

import org.example.spring.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserService {

    @Autowired
    private UserDao userDao;

//    转账方法调用
    public void accountMoney(){
        //lucy少100
        userDao.reduceMoney();

        //模拟异常
        int i=10/0;

        //mary多100
        userDao.addMoney();
    }

}

 

2.测试异常

原来的数据库值:

后来的数据库值:

分析:lucy的钱少了,mary 的钱却没有增加

 

3.没有使用spring框架的时候异常该如何处理

package org.example.spring.service;

import org.example.spring.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserService {

    @Autowired
    private UserDao userDao;

//    转账方法调用
    public void accountMoney(){
//            1.开启事务
        try{
//            2.进行业务操作
            //lucy少100
            userDao.reduceMoney();

            //模拟异常
            int i=10/0;

            //mary多100
            userDao.addMoney();

//            3.没有发生异常,提交事务

        }catch(Exception e){
//            4.如果发生了异常,从第3跳到第4,进行事务回滚


        }


    }

}

 

4.使用spring框架的时候异常该如何处理

(1)事务添加到javaEE三层结构里面的service层 (业务逻辑层)

(2)在spring 进行事务管理操作有两种方式:

【第一种】编程式事务管理:相当于3. 里面的操作

【第二种】声明式事务管理(常用):

1.基于注解方式(常用)

2.基于xml 配置方式

 

5.在spring 进行声明式事务管理,底层使用AOP

 

6.spring 事务管理API

(1)spring 提供了一个接口,代表是事务管理器(事务操作都封装在里面)

这个接口针对不同的框架提供不同的实现类,例如以下的红框就是整合JDBC框架的子类 实现类

 

7.事务操作(注解声明式事务管理)

(1)在spring的配置文件中配置事务管理器

<?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"
       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">

<!--    开启组件扫描-->
    <context:component-scan base-package="org.example">

    </context:component-scan>
<!--数据库连接池-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
        <property name="url" value="jdbc:mysql://localhost:3306/user_db?useSSL=false&amp;useUnicode=true&amp;characterEncoding=UTF-8"/>
        <property name="username" value="root"/>
        <property name="password" value="sise"/>
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    </bean>

<!--    创建jdbcTemplate对象-->
    <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>
</beans>

 

(2)在spring 配置文件中,开启事务注解

引入名称空间tx

开启事务注解

 

 

(3)在service 类上面(或者service类里面的方法上面) 添加事务注解

 

(4)@Transactional,这个注解添加到类上面,也可以添加到方法上面

如果把这个注解添加类上面,这个类里面所有的方法都添加事务

如果把这个注解添加方法上面,仅仅为这个方法添加事务

package org.example.spring.service;

import org.example.spring.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class UserService {

    @Autowired
    private UserDao userDao;

//    转账方法调用
    public void accountMoney(){
//1.开启事务
//        try{
//            2.进行业务操作
            //lucy少100
            userDao.reduceMoney();

            //模拟异常
            int i=10/0;

            //mary多100
            userDao.addMoney();

//            3.没有发生异常,提交事务

//        }catch(Exception e){
//            4.如果发生了异常,从第3跳到第4,进行事务回滚


//        }


    }

}

 

 

(5)测试:

原来数据:

后来数据(不变):

只是在控制台提示报错信息:

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值