Spring的事务管理

来源:互联网 发布:c语言最大的数据类型 编辑:程序博客网 时间:2024/06/08 10:08

一.事务概念

1.什么是事务

2.事务的特性

3.不考虑隔离性产生读问题

4.解决读问题

  • 设置隔离级别

二.Spring事务管理api

1.Spring事务管理两种方式

  • 编程式事务管理(不用)
  • 声明式事务管理
    • 基于xml配置文件实现
    • 基于注解实现

2.Spring事务管理的api介绍

这里写图片描述

  • Spring针对不同的dao层框架,提供接口不同的实现类
    这里写图片描述

(1).首先配置事务管理器

  • 搭建转账环境
   <!-- 第一步 配置事务管理器 -->         <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">                  <!-- 注入dataSource -->                  <property name="dataSource" ref="dataSource"></property>         </bean>
  • 创建数据库表,添加数据

这里写图片描述

  • 创建service和dao类,完成注入关系
<?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">        <!-- 配置c3p0连接池 -->         <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">                 <!-- 注入属性值 -->                   <property name="driverClass" value="com.mysql.jdbc.Driver"></property>                    <property name="jdbcUrl" value="jdbc:mysql:///spring_day03"></property>                     <property name="user" value="root"></property>                      <property name="password" value="T471912619"></property>         </bean> <bean id="orderService" class="cn.st.web.service.OrderService">                 <property name="orderDao" ref="orderDao"></property>         </bean>         <bean id="orderDao" class="cn.st.web.dao.OrderDao">                   <property name="jdbcTemplate" ref="jdbcTemplate"></property>         </bean>         <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">                  <property name="dataSource" ref="dataSource"></property>         </bean> </beans>        

service层又叫业务逻辑层
dao层,单纯对数据库操作层,在dao层不添加业务

(2).需求:小王 转账 1000 给 小马

小王少1000
小强多1000

public class OrderService {     private OrderDao orderDao;    public void setOrderDao(OrderDao orderDao) {        this.orderDao = orderDao;    }    public void lessMoney() {          orderDao.lessMoney();    }    public void moreMoney() {        orderDao.moreMoney();    }}
public class OrderDao {     private JdbcTemplate jdbcTemplate;    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {        this.jdbcTemplate = jdbcTemplate;    }    public void lessMoney() {         String sql="update account set salary=salary-? where username=?";         jdbcTemplate.update(sql,1000,"小王");    }    public void moreMoney() {         String sql="update account set salary=salary+? where username=?";         jdbcTemplate.update(sql,1000,"小马");    }}
public class TxDemo {    @Test    public void fun1() {        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("bean1.xml");        OrderService orderService=(OrderService)applicationContext.getBean("orderService");        orderService.lessMoney();        orderService.moreMoney();    }}

这里写图片描述

(3).产生问题

  • 如果小王少了1000之后,出现异常,小马不会多1000,钱丢失了
public class TxDemo {    @Test    public void fun1() {        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("bean1.xml");        OrderService orderService=(OrderService)applicationContext.getBean("orderService");        orderService.lessMoney();        System.out.println(10/0);//比如这个地方出现个异常        orderService.moreMoney();    }}

(4).解决

  • 添加事务解决,出现异常进行回滚操作
原创粉丝点击