spring学习笔记(事务)

一、事务的概念

  • 事务特性:4个特性
  • 事务并发问题:脏读、幻读、不可重复读
  • 事务的隔离级别 :1 2 4 8
  • 事务操作对象: TransactionManager

spring封装了事务管理的代码:打开事务,提交事务,回滚事务。
因为在不同平台,操作事务的代码各不相同,spring提供了一个接口PlatformTransactionManager 接口,里面有许多实现类:如JDBC–DataSourceTransactionManager,在spring中事务管理最为核心的对象:TransactionManager。

事务的隔离级别:1.读未提交;2.读已提交;3.可重复读;4.串行化
是否只读:true(只读) false
事务传播行为:决定业务方法之间调用,事务该如何处理。有七种,默认为PROPAGION_REQUIRED:支持当前事务,如果不存在,就新建一个。
二、代码演示
首先准备好数据库

id      name      money
1       xxs       900
2       nn        1100

1.dao层

public interface AccountDao {

    void IncreaseMoney(Integer id,Double money);

    void DecreaseMoney(Integer id,Double money);
}
public class AccountDaoImpl  extends JdbcDaoSupport implements AccountDao{

    @Override
    public void IncreaseMoney(Integer id, Double money) {
        String sql = "update tx_account set money=money+? where id=?";
        getJdbcTemplate().update(sql, money,id);
    }

    @Override
    public void DecreaseMoney(Integer id, Double money) {
        String sql = "update tx_account set money=money-? where id=?";
        getJdbcTemplate().update(sql, money,id);
    }
}
  1. service层
public class AccountServiceImpl  implements AccountService{ 
    private AccountDao aDao;
    @Override
    public void chage(Integer FromId, Integer toId, Double money) {
        aDao.IncreaseMoney(toId, money);
        aDao.DecreaseMoney(FromId, money);
    }

    public void setaDao(AccountDao aDao) {
        this.aDao = aDao;
    }
}

3.配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns="http://www.springframework.org/schema/beans" 
    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-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd ">
    <!--读取配置文件  -->
    <context:property-placeholder location="classpath:db.properties"/>
    <bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
        <property name="driverClass" value="${jdbc.driverClass}"></property>
        <property name="user" value="${jdbc.user}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>

    <bean name="accountdao" class="com.tz.tx.AccountDaoImpl">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <bean name="accountdaoService" class="com.tz.tx.service.AccountServiceImpl">
        <property name="aDao" ref="accountdao"></property>
    </bean> 
</beans>
  1. 测试代码
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:com/tz/tx/ApplicationContext.xml")
public class test {
    @Resource(name="accountdaoService")
    private AccountService as;

    @Test
    public void demo1(){
        as.chage(1, 2, 100d);   
    }   
}

在没有事务的情况下会出现问题,比如在转账的过程出现异常,则会不安全,所以我们必须要使用事务来处理。接下来我们介绍事务的操作

猜你喜欢

转载自blog.csdn.net/weixin_38008100/article/details/81388010