1. 程式人生 > >ider 使用MyBatis Srping -AOP 配置 事務的回滾和異常。。。。。

ider 使用MyBatis Srping -AOP 配置 事務的回滾和異常。。。。。

事務

事務:

司機版:沒有感覺

王棟:資料庫中的概念。提交事務 。。

帥帥:在某一個階段出現問題,整個事務回滾。撤銷所有影響。

王哲言:沒有好好學

事務:同生共死,一個整體。

星星:四種隔離級別  提交和回滾

馮如巖:事務有ACID原則

  原子性

  一致性

  隔離性

  永續性

 

 

家有兒女:導演:英達

 

藝術源於生活,遠遠高於生活

事務源於基礎 ,遠遠高於之前的理解

 

 

 

如何用真實的程式程式碼去體現事務的優點

  區別

 

1.購買股票

  賬戶

  股票

 

 

 

事務:

司機版:沒有感覺

王棟:資料庫中的概念。提交事務 。。

帥帥:在某一個階段出現問題,整個事務回滾。撤銷所有影響。

王哲言:沒有好好學

事務:同生共死,一個整體。

星星:四種隔離級別  提交和回滾

馮如巖:事務有ACID原則

  原子性

  一致性

  隔離性

  永續性

 

 

家有兒女:導演:英達

 

藝術源於生活,遠遠高於生活

事務源於基礎 ,遠遠高於之前的理解

 

 

 

如何用真實的程式程式碼去體現事務的優點

  區別

 

1.購買股票

  賬戶

  股票

 

 

 

Service 排程底層dao

        2

dao

 

Exception

 

Throwable

 

 

Spring 預設對 執行時異常  事務處理  自動回滾

 

 

 

事務代理工廠Bean

TransactionProxyFactoryBean

 

 

 

 

Properties

 

事務:

 

資料庫四種隔離級別

1.讀未提交  Read_Uncommitted

2.讀已經提交 Read_committed

3.可重複讀  Repeatable_read

4.序列化    Serializable

 

 

 

 

七種傳播行為

PROPAGATION_REQUIRED

 

PROPAGATION_REQUIRED

 

 

作業:

1.事務講解除錯通過。形成部落格

2.髒讀,不可重複讀,幻象讀  丟失更新(樂觀鎖,悲觀鎖)

3.Spring+Mybatis整合預習

 

DAO

package cn.bdqn.Bookshoop.dao;

/**
 * Created by Administrator on 2017/8/4.
 */
public interface AcoountDao  {
    public boolean Acoountone(int aid ,double  money,boolean isBuy );
}

 

 

package cn.bdqn.Bookshoop.dao;

import org.springframework.jdbc.core.support.JdbcDaoSupport;

/**
 * Created by Administrator on 2017/8/4.
 */
public class AcoountDaoImpl extends JdbcDaoSupport implements AcoountDao {
    public boolean Acoountone(int aid, double money, boolean isBuy) {
        boolean fat=false;
        String sql=null;
        if(isBuy){
            sql="update account set balance=balance-? where aid=?";
        }else{
            sql="update account set balance=balance+? where aid=?";
        }


        int update = this.getJdbcTemplate().update(sql, money, aid);
        if (update>0){
            fat=true;
        }
        return fat;
    }
}

 

 

package cn.bdqn.Bookshoop.dao;

/**
 * Created by Administrator on 2017/8/4.
 */
public interface Stock {
    public boolean Stocktone(int sid ,int count, boolean isBuy );
}

package cn.bdqn.Bookshoop.dao;

import org.springframework.jdbc.core.support.JdbcDaoSupport;

/**
 * Created by Administrator on 2017/8/4.
 */
public class StockImpl extends JdbcDaoSupport implements Stock {

    public boolean Stocktone(int sid, int count, boolean isBuy) {
        boolean fat=false;
        String sql=null;
        if(isBuy){
            sql="update stock set balance=balance+? where aid=?";
        }else{
            sql="update stock set balance=balance-? where aid=?";
        }


        int update = this.getJdbcTemplate().update(sql, count, sid);
        if (update>0){
            fat=true;
        }
        return fat;

    }
}

 

 

DAO層的方法

ENTITY

package cn.bdqn.Bookshoop.entity;

/**
 * Created by Administrator on 2017/8/4.
 */
public class Account {
    public Integer getAid() {
        return aid;
    }

    public void setAid(Integer aid) {
        this.aid = aid;
    }

    public String getAname() {
        return aname;
    }

    public void setAname(String aname) {
        this.aname = aname;
    }

    public double getBalance() {
        return balance;
    }

    public void setBalance(double balance) {
        this.balance = balance;
    }

    private Integer aid;
        private String aname;
        private double balance;
}

package cn.bdqn.Bookshoop.entity;

/**
 * Created by Administrator on 2017/8/4.
 */
public class Stock {
    private Integer sid;  //股票賬戶編號
    
private Integer count; //持有股數
    
private String sname;//股票名稱

    
public Integer getSid() {
        return sid;
    }

    public void setSid(Integer sid) {
        this.sid = sid;
    }

    public Integer getCount() {
        return count;
    }

    public void setCount(Integer count) {
        this.count = count;
    }

    public String getSname() {
        return sname;
    }

    public void setSname(String sname) {
        this.sname = sname;
    }
}

package cn.bdqn.Bookshoop.entity;

/**
 * Created by Administrator on 2017/8/4.
 */
public class StokException extends Exception {
    public StokException() {
    }

    public StokException(String message) {
        super(message);
    }
}

 

 

SERVICE裡組合程式碼 把兩個DAO層方法放在一個方法

 

package cn.bdqn.Bookshoop.Service;

import cn.bdqn.Bookshoop.entity.StokException;

/**
 * Created by Administrator on 2017/8/4.
 */
public interface AcoountService {
    public void Acoountone(int aid, double money , int countq,int sid ) throws Exception;
}

package cn.bdqn.Bookshoop.Service;

import cn.bdqn.Bookshoop.entity.StokException;
import cn.bdqn.Bookshoop.dao.AcoountDao;
import cn.bdqn.Bookshoop.dao.Stock;


/**
 * Created by Administrator on 2017/8/4.
 */
public class AcoountServiceImpl implements AcoountService {
    private  AcoountDao adao;
    private Stock sdao;

    public void Acoountone(int aid, double money , int count,int sid  ) throws Exception {
        boolean isBuy=true;



        //z植入AcountDao
        
adao.Acoountone(aid,money,isBuy);
        //異常攔截歸滾  下面程式碼不執行


        
if (1==1) {


            throw new StokException();
        }
        sdao.Stocktone(sid,count,isBuy);

    }


    public Stock getSdao() {
        return sdao;
    }

    public void setSdao(Stock sdao) {
        this.sdao = sdao;
    }

    public AcoountDao getAdao() {

        return adao;
    }

    public void setAdao(AcoountDao adao) {
        this.adao = adao;
    }
}

 

 

主要的 resources下面的XML

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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
"
>
<!--00.識別jbdc。propertties檔案-->
    
<context:property-placeholder location="jdbc.properties"></context:property-placeholder>

   <!-- 01.建立資料庫 ${}Spring n內設的一個數據  DriverManger–>

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${jdbc.driverClassName}"></property>
        <property name="url" value="${jdbc.url}"></property>
        <property name="username" value="${jdbc.user}"></property>
        <property name="password" value="${jdbc.password}"></property>

    </bean>-->

   <!--<!–02.阿里巴巴的–>

    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${jdbc.driverClassName}"></property>
        <property name="url" value="${jdbc.url}"></property>
        <property name="username" value="${jdbc.user}"></property>
        <property name="password" value="${jdbc.password}"></property>

    </bean>-->
  <!--  <!–03.c3p0 com.mchange.v2.c3p0.ComboPooledDataSource–>

    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="dataSourceName" value="${jdbc.driverClassName}"></property>
        <property name="jdbcUrl" value="${jdbc.url}"></property>
        <property name="user" value="${jdbc.user}"></property>
        <property name="password" value="${jdbc.password}"></property>

    </bean>-->
      <!--04.dbcp-->

   
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
       <property name="driverClassName" value="${jdbc.driverClassName}"></property>
       <property name="url" value="${jdbc.url}"></property>
       <property name="username" value="${jdbc.user}"></property>
       <property name="password" value="${jdbc.password}"></property>

   </bean>

    <!--02.jdbcTemplate 配置-->
    
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!--03.dao配置-->
    
<bean id="adao" class="cn.bdqn.Bookshoop.dao.AcoountDaoImpl">
        <property name="jdbcTemplate" ref="jdbcTemplate"></property>
    </bean>
    <!--03.dao配置-->
    
<bean id="sdao" class="cn.bdqn.Bookshoop.dao.StockImpl">
        <property name="jdbcTemplate" ref="jdbcTemplate"></property>
    </bean>
    <!--04.service   bookService-->
    
<bean id="acoountService" class="cn.bdqn.Bookshoop.Service.AcoountServiceImpl">
        <property name="adao" ref="adao"></property>
        <property name="sdao" ref="sdao"></property>
    </bean>

    <!--事務管理器-->
    
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
<!--配置事務。攔截業務方法-->
    
<bean id="acoountServiceProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
        <!--目標-->
        
<property name="target" ref="acoountService"></property>
        <property name="transactionManager" ref="transactionManager"></property>
        <!--增強-->
        
<property name="transactionAttributes">
            <props>
                <prop

//這個別寫錯 !ISOLATION_DEFAULT,PROPAGATION_REQUIRED,-StokException 它是異常方法名字

key="Acoountone">ISOLATION_DEFAULT,PROPAGATION_REQUIRED,-StokException</prop>
            </props>
        </property>
    </bean>
</beans>

 

讓後在測試類

package cn.bdqn.tset;

import cn.bdqn.Bookshoop.Service.AcoountService;
import cn.bdqn.Bookshoop.Service.AcoountServiceImpl;
import cn.bdqn.spring0730dai.intterceotorNames.some;
import cn.bdqn.spring19jdbctemplate.dao.impl.BookDAOImpl;
import cn.bdqn.spring19jdbctemplate.service.IBookService;
import cn.bdqn.springaop0731.UserName;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Created by Administrator on 2017/7/23.
 */
public class SpringTset0804 {
    @Test
   public  void set(){
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContextJdbcBookshoop.xml");
        AcoountService service = (AcoountService) ctx.getBean("acoountServiceProxy");
        try {
            service.Acoountone(2,200,300,1);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}