欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring使用注解方式處理事務(wù)

 更新時(shí)間:2022年08月26日 14:22:20   作者:少年??!  
這篇文章主要為大家詳細(xì)介紹了Spring使用注解方式處理事務(wù),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

Spring有專門的類來處理事務(wù),在這之前我們先要理解Spring處理事務(wù)中的幾個(gè)概念:

1.接口:

事務(wù)管理器是PlatformTransactionManager接口,在接口中定義了事務(wù)的主要函數(shù):commit(); 事務(wù)提交
rollback();事務(wù)回滾

2.事務(wù)管理器接口的實(shí)現(xiàn)類:

1)DataSourcTransactionManager:使用jdb或者mybatis訪問數(shù)據(jù)庫時(shí)使用的
<bean id=”myDataSource” class=“xx包.DataSourceTransactionManager”>
必須指定數(shù)據(jù)源
</bean>
2)HibernateTransactionManager:使用Hibernate框架時(shí),使用的實(shí)現(xiàn)類
3)事務(wù)超時(shí):TIMEOUT,秒為單位,默認(rèn)是-1,使用數(shù)據(jù)庫的默認(rèn)超時(shí)時(shí)間
超時(shí):指事務(wù)的最長執(zhí)行時(shí)間,也就是一個(gè)函數(shù)最長的執(zhí)行時(shí)間.當(dāng)時(shí)間到了,函數(shù)沒有
執(zhí)行完畢,Spring會(huì)回滾該函數(shù)的執(zhí)行(回滾事務(wù))

3.事務(wù)的傳播行為:事務(wù)在函數(shù)之間傳遞,函數(shù)怎么使用事務(wù)。通過傳播行為指定函數(shù)怎么使用事務(wù)

有7個(gè)傳播行為:
事務(wù)的傳播行為常量都是以PROPAGATION_開頭,形如:PROPAGATION_XXX
PROPAGATION_REQUIRED 指定的函數(shù)必須在事務(wù)內(nèi)執(zhí)行。若當(dāng)前存在事務(wù),就加入到當(dāng)前事務(wù)中, 若當(dāng)前沒事務(wù),就創(chuàng)建一個(gè)新事務(wù)。Spring默認(rèn)的事務(wù)傳播行為
PROPAGATION_REQUIES_NEW 總是新建一個(gè)新事務(wù),若當(dāng)前存在事務(wù),就將當(dāng)前事務(wù)掛起來,直 到新事務(wù)執(zhí)行完畢
PROPAGATION_SUPPORTS 指定的函數(shù)支持當(dāng)前事務(wù),但若當(dāng)前沒事務(wù),也可以使用非事務(wù)方式執(zhí) 行
PROPAGATION_MANDATORY
PROPAGATION_NESTED
PROPAGATION_NEVER
PROPAGATION_NOT_SUPPORTED

我們了解了Spring處理事務(wù)的一些概念以及一些常用的類,那么現(xiàn)在在Spring中使用事務(wù)

項(xiàng)目目錄:

要spring使用事務(wù)的注解就需要在application-config.xml(Spring核心配置文件)添加頭部信息:

<?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:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
? ? ? ?xsi:schemaLocation="http://www.springframework.org/schema/beans
?http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
?http://www.springframework.org/schema/tx
?http://www.springframework.org/schema/tx/spring-tx-2.0.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">

<!--聲明事務(wù)管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
? ? <property name="dataSource" ref="myDataSource"/>
</bean>
<!--聲明事務(wù)的注解驅(qū)動(dòng)
? transaction-manager:事務(wù)管理器對象的id
-->
<tx:annotation-driven transaction-manager="transactionManager"/>

BuyGoodsServiceImpl文件:

/**使用注解來設(shè)值事務(wù)的屬性(傳播行為,隔離等級,超時(shí),當(dāng)哪些異常發(fā)生的時(shí)候觸發(fā)回滾事務(wù))
?* 注意:該注解必須使用在公有函數(shù)上,而且拋出的異常必需繼承RuntimeException
?* */
@Transactional(propagation = Propagation.REQUIRED,
isolation = Isolation.DEFAULT,timeout = 20,
rollbackFor = {NullPointerException.class,NotEnoughException.class})
public void buyGoods(int goodsId, int nums)
? ? ? ? throws NullPointerException, NotEnoughException{
? ? /**生成銷售的訂單
? ? ?* */
? ? Sale sale=new Sale();
? ? sale.setGid(goodsId);
? ? sale.setNum(nums);
? ? saleDao.insertSale(sale);

? ? /**修改庫存
? ? ?* */
? ? Goods goods=goodsDao.selectGoodsById(goodsId);
? ? if(goods==null){
? ? ? ? throw new NullPointerException(goodsId+"沒有該商品");
? ? }
? ? if(goods.getAmount()<nums){
? ? ? ? throw new NotEnoughException(goodsId+"庫存不足");
? ? }

? ? /**操作庫存
? ? ?* */
? ? goods.setAmount(nums);
? ? goodsDao.updateGoods(goods);
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論