Spring中的aop事物

事物

  • 什么是事物?
    事务逻辑上的一组操作,组成这组操作的各个逻辑单元,要么一起成功,要么一起失败。

  • 事物特性
    原子性 :强调事务的不可分割.
    一致性 :事务的执行的前后数据的完整性保持一致.
    隔离性 :一个事务执行的过程中,不应该受到其他事务的干扰
    持久性 :事务一旦结束,数据就持久到数据

  • 事物引发的安全性问题
    脏读 :一个事务读到了另一个事务的未提交的数据
    不可重复读 :一个事务读到了另一个事务已经提交的 update 的数据导致多次查询结果不一致.
    幻读 :一个事务读到了另一个事务已经提交的 insert 的数据导致多次查询结果不一致.

  • 事物的隔离级别
    读未提交:脏读,不可重复读,虚读都有可能发生
    读已提交 :避免脏读。但是不可重复读和虚读有可能发生
    可重复读 :避免脏读和不可重复读.但是虚读有可能发生.
    串行化的 :避免以上所有读问题.

  • 事物操作
    打开事物,提交事物,回滚事物

spring进行事物管理

因为在不同的平台,操作事物的代码不相同,spring提供了一个接口PlatformTransactionManager,根据平台的不同,有不同的实现类,DataSourceTransactionManager,HibernateTransactionManager

事物的传播行为
99%的情况都用默认的PROPAGATION_REQUIRED
这里写图片描述

案例体现

xml配置开发

  • 导包
    4+2(基本包+日志包)
    基本包
    整合junit测试包
    这里写图片描述
    数据库驱动包和Spring的JDBC包
    这里写图片描述
    aop联盟包,织入包,aspects,tx事物包
    这里写图片描述
  • 导入约束
    这里写图片描述

  • 准备数据库
    这里写图片描述

  • 代码实现,编写一个转账的实例

Dao层接口

package cn.itcast.dao;
public interface AccountDao {
    //加钱
    void increaseMoney(Integer id,Double money);
    //减钱
    void decreaseMoney(Integer id,Double money);
}

Dao层实现

package cn.itcast.dao;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao  {

    @Override
    public void increaseMoney(Integer id, Double money) {
        getJdbcTemplate().update("update t_account set money = money+? where id = ? ", money,id);
    }

    @Override
    public void decreaseMoney(Integer id, Double money) {
        getJdbcTemplate().update("update t_account set money = money-? where id = ? ", money,id);
    }
}

Service接口

package cn.itcast.service;

public interface AccountService {
    //转账方法
    void transfer(Integer from,Integer to,Double money);
}

Service实现

package cn.itcast.service;

import org.springframework.transaction.support.TransactionTemplate;

import cn.itcast.dao.AccountDao;

public class AccountServiceImpl implements AccountService {

    private AccountDao ad ;

    @Override
    public void transfer(final Integer from,final Integer to,final Double money) {
                //减钱
                ad.decreaseMoney(from, money);
                //这里出现异常,转账将不会成功
                int i = 1/0;
                //加钱
                ad.increaseMoney(to, money);
    }
    public void setAd(AccountDao ad) {
        this.ad = ad;
    }
}
  • Properties配置文件
jdbc.jdbcUrl=jdbc:mysql:///hibernate_32
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.user=root
jdbc.password=1234
  • XML配置文件
<?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" xmlns:tx="http://www.springframework.org/schema/tx" 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 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd ">

<!-- 指定spring读取db.properties配置 -->
<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="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" >
    <property name="dataSource" ref="dataSource" ></property>
</bean>

<!-- 配置事务通知 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager" >
    <tx:attributes>
        <!-- 以方法为单位,指定方法应用什么事务属性
            isolation:隔离级别
            propagation:传播行为
            read-only:是否只读
            以下的method中name配齐了企业两套的增删改查操作的方法,只要是以这些开头的都会被spring事物管理。
         -->
        <tx:method name="save*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
        <tx:method name="persist*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
        <tx:method name="update*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
        <tx:method name="modify*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
        <tx:method name="delete*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
        <tx:method name="remove*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
        <tx:method name="get*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="true" />
        <tx:method name="find*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="true" />
        <tx:method name="transfer" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
    </tx:attributes>
</tx:advice>


<!-- 配置织入 -->
<aop:config  >
    <!-- 配置切点表达式 -->
    <aop:pointcut expression="execution(* cn.itcast.service.*ServiceImpl.*(..))" id="txPc"/>
    <!-- 配置切面 : 通知+切点
            advice-ref:通知的名称
            pointcut-ref:切点的名称
     -->
    <aop:advisor advice-ref="txAdvice" pointcut-ref="txPc" />
</aop:config>

<!--Dao-->
<bean name="accountDao" class="cn.itcast.dao.AccountDaoImpl" >
    <property name="dataSource" ref="dataSource" ></property>
</bean>
<!--Service-->
<bean name="accountService" class="cn.itcast.service.AccountServiceImpl" >
    <property name="ad" ref="accountDao" ></property>
</bean>  

</beans>
  • 测试
package cn.itcast.tx;

import javax.annotation.Resource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import cn.itcast.service.AccountService;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class Demo {
    @Resource(name="accountService")
    private AccountService as;

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

注解配置开发

删除事物通知和织入的xml配置,添加以下代码即可开启事物注解开发

<!-- 开启使用注解管理aop事务 -->
<tx:annotation-driven/>

然后在Service实现类中使用注解开发

@Transactional(isolation=Isolation.REPEATABLE_READ,propagation=Propagation.REQUIRED,readOnly=true)

这里写图片描述
通常情况下将注解配置写在类的上面,这样整个类中所有的方法,都会使用这一个注解配置,如果有个别的方法要使用不同属性的注解配置,则在该方法上单独写上注解配置即可。

一般我们Service编程使用注解配置,直接使用@Transactional的默认配置即可

猜你喜欢

转载自blog.csdn.net/qq_42780864/article/details/81384140