Spring使用注解方式處理事務
Spring有專門的類來處理事務,在這之前我們先要理解Spring處理事務中的幾個概念:
1.接口:
事務管理器是PlatformTransactionManager接口,在接口中定義了事務的主要函數:commit(); 事務提交
rollback();事務回滾
2.事務管理器接口的實現類:
1)DataSourcTransactionManager:使用jdb或者mybatis訪問數據庫時使用的
<bean id=”myDataSource” class=“xx包.DataSourceTransactionManager”>
必須指定數據源
</bean>
2)HibernateTransactionManager:使用Hibernate框架時,使用的實現類
3)事務超時:TIMEOUT,秒為單位,默認是-1,使用數據庫的默認超時時間
超時:指事務的最長執(zhí)行時間,也就是一個函數最長的執(zhí)行時間.當時間到了,函數沒有
執(zhí)行完畢,Spring會回滾該函數的執(zhí)行(回滾事務)
3.事務的傳播行為:事務在函數之間傳遞,函數怎么使用事務。通過傳播行為指定函數怎么使用事務
有7個傳播行為:
事務的傳播行為常量都是以PROPAGATION_開頭,形如:PROPAGATION_XXX
PROPAGATION_REQUIRED 指定的函數必須在事務內執(zhí)行。若當前存在事務,就加入到當前事務中, 若當前沒事務,就創(chuàng)建一個新事務。Spring默認的事務傳播行為
PROPAGATION_REQUIES_NEW 總是新建一個新事務,若當前存在事務,就將當前事務掛起來,直 到新事務執(zhí)行完畢
PROPAGATION_SUPPORTS 指定的函數支持當前事務,但若當前沒事務,也可以使用非事務方式執(zhí) 行
PROPAGATION_MANDATORY
PROPAGATION_NESTED
PROPAGATION_NEVER
PROPAGATION_NOT_SUPPORTED
我們了解了Spring處理事務的一些概念以及一些常用的類,那么現在在Spring中使用事務
項目目錄:
要spring使用事務的注解就需要在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"> <!--聲明事務管理器--> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> ? ? <property name="dataSource" ref="myDataSource"/> </bean> <!--聲明事務的注解驅動 ? transaction-manager:事務管理器對象的id --> <tx:annotation-driven transaction-manager="transactionManager"/>
BuyGoodsServiceImpl文件:
/**使用注解來設值事務的屬性(傳播行為,隔離等級,超時,當哪些異常發(fā)生的時候觸發(fā)回滾事務) ?* 注意:該注解必須使用在公有函數上,而且拋出的異常必需繼承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); }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Java數據結構之數組(動力節(jié)點之Java學院整理)
這篇文章主要介紹了Java數據結構之數組(動力節(jié)點之Java學院整理)的相關資料,包括創(chuàng)建和內存分配,數組封裝后的使用等,需要的朋友參考下吧2017-04-04Java的Hibernate框架中Criteria查詢使用的實例講解
這篇文章主要介紹了Java的Hibernate框架中Criteria查詢使用的實例講解,Hibernate是Java的SSH三大web開發(fā)框架之一,需要的朋友可以參考下2016-01-01