Spring 實現事務的三種方式


事務:保證數據的運行不會說A給B錢,A錢給了B卻沒收到。

實現事務的三種方式(重要代碼):

1.aspectJ AOP實現事務:

<bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dateSource"></property>
</bean>
<tx:advice id="stockAdvice" transaction-manager="dataSourceTransactionManager">
<tx:attributes>
<tx:method name="by*" isolation="DEFAULT" propagation="REQUIRED" rollback-for="MyExepction"/>
<tx:method name="select*" isolation="DEFAULT" propagation="REQUIRED" read-only="true"></tx:method>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="exAdvice" expression="execution(* *..service.*.*(..))"></aop:pointcut>
<aop:advisor advice-ref="stockAdvice" pointcut-ref="exAdvice"></aop:advisor>
</aop:config>

2.事務代理工廠Bean實現事務:

<bean id="tproxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager" ref="dataSourceTransactionManager"></property>&lt;!&ndash;寫的是事務&ndash;&gt;
<property name="target" ref="byStockService"></property>&lt;!&ndash;要進行事務的類&ndash;&gt;
<property name="transactionAttributes">
<props>&lt;!&ndash;key寫的是service層要增強的方法&ndash;&gt;
&lt;!&ndash; 事務的隔離級別,后面逗號后面是異常類,用於回滾數據&ndash;&gt;
<prop key="ByStock">ISOLATION_DEFAULT,PROPAGATION_REQUIRED,-MyExepction</prop>
</props>
</property>
</bean>

3.注解方式實現事務:

<tx:annotation-driven transaction-manager="dataSourceTransactionManager"/>
在需要進行事務的方法上增加一個注解“
@Transactional(rollbackFor = MyExepction.class )

做一個買股票的小例子來看一下事務:

1.使用事務工廠Bean

xml:

 

這個異常剛好卡在支付金額,和股票增加直接,

 

 RuntimeException是運行時異常
Exception是檢查異常
兩者的區別:
運行時異常是默認回滾,
檢查異常是默認提交的。

 

 數據表:

 

 結果異常出現后,數據進行了回滾,A表中並沒有少余額,B表中也沒有多股票。

 

 

基本的架構:

 

  dao:                                                                           

IStockDao:
package cn.Spring.Day20xy.dao;

public interface IStockDao {
    public int updateStock(int sid,int soucnt,boolean isbuay)throws Exception;
}
IAccountDao:
package cn.Spring.Day20xy.dao;

public interface IAccountDao {
    public int updateAccount(int aid,Double amoeny,boolean isbay) throws Exception;
}
StockDaoImpl:
package cn.Spring.Day20xy.dao;

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

public class StockDaoImpl extends JdbcDaoSupport implements IStockDao {

    @Override
    public int updateStock(int sid, int soucnt, boolean isbuay) throws Exception {
        String sql="";
        if (isbuay){
            sql="update stock set scount=scount+? where sid=?";
        }else {
            sql="update stock set scount=scount-? where sid=?";
        }
        return this.getJdbcTemplate().update(sql,soucnt,sid);
    }
}
AccountDaoImpl:
package cn.Spring.Day20xy.dao;

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

public class AccountDaoImpl extends JdbcDaoSupport implements IAccountDao{
    @Override
    public int updateAccount(int aid,Double amoeny,boolean isbay) throws Exception {
        String sql="";
        if (isbay){
            sql="update account set abalance=abalance-? where aid=?";
        }else {
            sql="update account set abalance=abalance+? where aid=?";
        }
        return this.getJdbcTemplate().update(sql,amoeny,aid);
    }
}

  entity:                                                                           

Account:
package cn.Spring.Day20xy.entity;

public class Account {
    private Integer aid;
    private String aname;
    private double abalance;

    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 getAbalance() {
        return abalance;
    }

    public void setAbalance(double abalance) {
        this.abalance = abalance;
    }
}

  

Stock:
package cn.Spring.Day20xy.entity;

public class Stock {
    //股票代號
    private Integer sid;

    //股票名稱
    private String sname;

    //股數
    private Integer scount;

    public Integer getSid() {
        return sid;
    }

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

    public String getSname() {
        return sname;
    }

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

    public Integer getScount() {
        return scount;
    }

    public void setScount(Integer scount) {
        this.scount = scount;
    }
}
MyExepction:
package cn.Spring.Day20xy.entity;

public class MyExepction extends RuntimeException {
    public MyExepction() {
    }

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


}

  service:                                                                           

IByStockService :

package cn.Spring.Day20xy.service;

public interface IByStockService {
    public int ByStock(int aid,double moeny,int sid ,int count)throws Exception;
}
ByStockServiceImpl:
package cn.Spring.Day20xy.service;

import cn.Spring.Day20xy.dao.IAccountDao;
import cn.Spring.Day20xy.dao.IStockDao;
import cn.Spring.Day20xy.entity.MyExepction;
import org.springframework.transaction.annotation.Transactional;

public class ByStockServiceImpl implements IByStockService{
    //植入Dao
    private IAccountDao accountDao;
    private IStockDao stockDao;

    @Override
    @Transactional(rollbackFor = MyExepction.class )
    public int ByStock(int aid,double moeny,int sid ,int count) throws Exception {
        boolean isbay=true;
        int i = accountDao.updateAccount(aid, moeny, isbay);
       if (1==1) {
           throw new MyExepction("出異常了");
       }
        int i1 = stockDao.updateStock(sid, count, isbay);
        return i+i1;
    }

    public IAccountDao getAccountDao() {
        return accountDao;
    }

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

    public IStockDao getStockDao() {
        return stockDao;
    }

    public void setStockDao(IStockDao stockDao) {
        this.stockDao = stockDao;
    }
}

 

  xml:                                                                           

<?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:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context" 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/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
    <!--1.配置數據源-->
    <bean id="dateSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql:///newss2230"></property>
        <property name="username" value="root"></property>
        <property name="password" value="123"></property>
    </bean>
    <context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
    <!--2.JDBCTampLate-->
    <bean id="jdbcTempLate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dateSource"></property>
    </bean>
    <!--3.Dao-->
    <bean id="accountDao" class="cn.Spring.Day20xy.dao.AccountDaoImpl">
        <property name="jdbcTemplate" ref="jdbcTempLate"></property>
    </bean>
    <!--3.Dao-->
    <bean id="stockDao" class="cn.Spring.Day20xy.dao.StockDaoImpl">
        <property name="jdbcTemplate" ref="jdbcTempLate"></property>
    </bean>
    <!--4.Service-->
    <bean id="byStockService" class="cn.Spring.Day20xy.service.ByStockServiceImpl">
        <property name="accountDao" ref="accountDao"></property>
        <property name="stockDao" ref="stockDao"></property>

        <!--事務-->
    </bean>
    <bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dateSource"></property>
    </bean>

    <!--事務注解驅動-->
    <tx:annotation-driven transaction-manager="dataSourceTransactionManager"/>
    <!--事務代理工廠bean-->
    <!--<bean id="tproxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
        <property name="transactionManager" ref="dataSourceTransactionManager"></property>
        <property name="target" ref="byStockService"></property>
      <property name="transactionAttributes">
          <props>&lt;!&ndash;key寫的是service層的方法&ndash;&gt;
              <prop key="ByStock">ISOLATION_DEFAULT,PROPAGATION_REQUIRED,-MyExepction</prop>
          </props>
      </property>
    </bean>-->
</beans>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM