spring事务管理

2021-02-14 13:18

阅读:388

标签:interface   建模   info   UNC   详情   config   getbean   aci   概念   

1、事务

(1)概念

一组业务操作,要求要么全不成功,要么全部不成功

(2)特性(ACID)

原子性、一致性、隔离性、持久性

(3)隔离问题

脏读

不可重复读

幻读

(4)隔离级别

read uncommitted:读未提交,存在三个问题

read committed:读已提交,解决了脏读,存在两个问题

repeatable read:可重复读,解决脏读、不可重复度、存在一个问题

serializable:串行化,都解决了,单事务

(5)savePoint(保存点)

记录操作的当前位置,之后可以回滚到指定指定的位置(可以回滚一部分)

 

2、spring的事务管理

(1)导包

tx:

技术图片

 

 

 

事务管理器:

技术图片

 

 

 第一个jdbc的时候使用,第二个整合hibernate的时候使用

(2)三个顶级接口

技术图片

 

 

 (3)传播行为

技术图片

 

 

 第一个为默认值

 

3、手动管理事务

(1)创建表

技术图片

 

 

 (2)导入jar包

核心:4+1

技术图片

aop:aop联盟、spring aop、aspectj规范、spring aspect

技术图片

数据库:jdbc、tx

连接池:C3P0

技术图片

 

 

 

 (3)创建Dao层和Service层的接口和接口的实现类:

Dao层:

public interface AccountDao {
    void out(String outer,Integer money);
    void in(String inner,Integer money);
}

Service层:

public interface AccountService {
     void transfer(String outer,String inner,Integer money);
}
public class AccountServiceImpl implements AccountService {
    private AccountDao accountDao;
    private TransactionTemplate transactionTemplate;
    @Override
    public void transfer(final String outer,final String inner,final Integer money) {
        transactionTemplate.execute(new TransactionCallbackWithoutResult() {
            @Override
            protected void doInTransactionWithoutResult(TransactionStatus transactionStatus) {
                accountDao.out(outer, money);
                int i=1/0;
                accountDao.in(inner, money);
            }
        });

    }

    public void setAccountDao(AccountDaoImpl accountDao) {
        this.accountDao=accountDao;
    }

    public void setTransactionTemplate(TransactionTemplate transactionTemplate) {
        this.transactionTemplate=transactionTemplate;
    }
}

在实现类里面制造异常,用于校验

(4)配置文件:


       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd">
    
    class="com.mchange.v2.c3p0.ComboPooledDataSource">
        class="org.springframework.transaction.support.TransactionTemplate">
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        class="pers.zhb.dao.AccountDaoImpl">
        class="pers.zhb.service.AccountServiceImpl">
        

(5)测试:

public class Test {
    public static void main(String[] args) {
        ApplicationContext applicationContext=new
                ClassPathXmlApplicationContext("applicationContext.xml");
        AccountService accountService =(AccountService) applicationContext.getBean("accountService");
        accountService.transfer("zhai","zhang",10);
    }
}

在出现异常后不管是转账方还是收款方,金额均未发生变化

 

4、aop配置基于xml

(1)创建Service层和Dao层以及接口和接口的实现类:

Service层:

public interface AccountService {
     void transfer(String outer,String inner,Integer money);
}
public class AccountServiceImpl implements AccountService {
    private AccountDao accountDao;
    private TransactionTemplate transactionTemplate;
    @Override
    public void transfer( String outer, String inner, Integer money) {
                accountDao.out(outer, money);
                int num=1/0;
                accountDao.in(inner, money);
    }

    public void setAccountDao(AccountDaoImpl accountDao) {
        this.accountDao=accountDao;
    }

    public void setTransactionTemplate(TransactionTemplate transactionTemplate) {
        this.transactionTemplate=transactionTemplate;
    }
}

Dao层:

public interface AccountDao {
    void out(String outer,Integer money);
    void in(String inner,Integer money);
}
public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao{
    @Override
    public void out(String outer, Integer money) {
        this.getJdbcTemplate().update("update transfer set money=money-? where username=?",money,outer);
    }

    @Override
    public void in(String inner, Integer money) {
        this.getJdbcTemplate().update("update transfer set money=money+? where username=?",money,inner);
    }
}

(2)配置文件:

xml version="1.0" encoding="UTF-8"?>
beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns="http://www.springframework.org/schema/beans"
       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/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">
    
    bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        property name="driverClass" value="com.mysql.jdbc.Driver">property>
        property name="jdbcUrl" value="jdbc:mysql:///bank_transfer">property>
        property name="user" value="root">property>
        property name="password" value="root">property>
    bean>
    bean id="accountDao" class="pers.zhb.dao.AccountDaoImpl">
        property name="dataSource" ref="dataSource">property>
    bean>
    bean id="accountService" class="pers.zhb.service.AccountServiceImpl">
        property name="accountDao" ref="accountDao">property>
    bean>
    
    bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        property name="dataSource" ref="dataSource">property>
    bean>
    
    tx:advice id="txAdvice" transaction-manager="txManager">
        tx:attributes>
            tx:method name="transfer()" propagation="REQUIRED" isolation="DEFAULT"/>
        tx:attributes>
    tx:advice>
    
    aop:config>
        aop:advisor advice-ref="txAdvice" pointcut="execution(* pers.zhb.service..*.*(..))">aop:advisor>
    aop:config>


beans>

(3)测试类:

public class Test {
    public static void main(String[] args) {
        ApplicationContext applicationContext=new
                ClassPathXmlApplicationContext("applicationContext.xml");
        AccountService accountService =(AccountService) applicationContext.getBean("accountService");
        accountService.transfer("zhai","zhang",10);
    }
}

没有异常发生的时候能够保证转账金额的减少量和收款人的金额的增加量是一样的,当有异常发生时,转账方和收款方的金额都不会发生变化。

 

5、注解方式

(1)Dao层和Service层的接口和接口的实现类:

Dao层:

public interface AccountDao {
    void out(String outer,Integer money);
    void in(String inner,Integer money);
}
public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao{
    @Override
    public void out(String outer, Integer money) {
        this.getJdbcTemplate().update("update transfer set money=money-? where username=?",money,outer);
    }

    @Override
    public void in(String inner, Integer money) {
        this.getJdbcTemplate().update("update transfer set money=money+? where username=?",money,inner);
    }
}

Service层:

public interface AccountService {
     void transfer(String outer,String inner,Integer money);
}
@Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT)
public class AccountServiceImpl implements AccountService {
    private AccountDao accountDao;
    private TransactionTemplate transactionTemplate;
    @Override

    public void transfer( String outer, String inner, Integer money) {
                accountDao.out(outer, money);

                accountDao.in(inner, money);
    }

    public void setAccountDao(AccountDaoImpl accountDao) {
        this.accountDao=accountDao;
    }

    public void setTransactionTemplate(TransactionTemplate transactionTemplate) {
        this.transactionTemplate=transactionTemplate;
    }
}

(2)配置文件:

xml version="1.0" encoding="UTF-8"?>
beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns="http://www.springframework.org/schema/beans"
       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/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">
    
    bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        property name="driverClass" value="com.mysql.jdbc.Driver">property>
        property name="jdbcUrl" value="jdbc:mysql:///bank_transfer">property>
        property name="user" value="root">property>
        property name="password" value="root">property>
    bean>
    bean id="accountDao" class="pers.zhb.dao.AccountDaoImpl">
        property name="dataSource" ref="dataSource">property>
    bean>
    bean id="accountService" class="pers.zhb.service.AccountServiceImpl">
        property name="accountDao" ref="accountDao">property>
    bean>
    
    bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        property name="dataSource" ref="dataSource">property>
    bean>
    
    tx:annotation-driven transaction-manager="txManager">tx:annotation-driven>
beans>

(3)测试类:

public class Test {
    public static void main(String[] args) {
        ApplicationContext applicationContext=new
                ClassPathXmlApplicationContext("applicationContext.xml");
        AccountService accountService =(AccountService) applicationContext.getBean("accountService");
        accountService.transfer("zhai","zhang",10);
    }
}

 

spring事务管理

标签:interface   建模   info   UNC   详情   config   getbean   aci   概念   

原文地址:https://www.cnblogs.com/zhai1997/p/12722124.html


评论


亲,登录后才可以留言!