SSM如何實(shí)現(xiàn)在Controller中添加事務(wù)管理
SSM在Controller中添加事務(wù)管理
本人使用:
- 集成開發(fā)環(huán)境:idea
- 項(xiàng)目管理工具:maven
- 數(shù)據(jù)庫(kù):oracle
- 框架:Spring+SpringMVC+myBatis
一般而言,事務(wù)都是加在Service層的,但也可以加在Controller層。。
看了不少人的博客,總結(jié)出兩個(gè)方法:
- 在controller層寫編程式事務(wù)
- 將事務(wù)配置定義在Spring MVC的應(yīng)用上下文(spring-mvc.xml)中
現(xiàn)在具體來(lái)說(shuō)說(shuō)怎么實(shí)現(xiàn)的:
1.在controller層寫編程式事務(wù)【繁瑣,不推薦】
spring-mybatis.xml中事物管理器的配置依舊
<!-- 配置數(shù)據(jù)源事務(wù) --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> ? ? <property name="dataSource" ?ref="dataSource"/> </bean>? <!--? ? ? 注解方式配置事務(wù) @Transactional ? ? 但因?yàn)槭窃赾ontroller中寫編程式事務(wù),這里可以不配置<tx:annotation-driven transaction-manager="transactionManager" /> --> <tx:annotation-driven transaction-manager="transactionManager" />
在controller中的方法里編寫事務(wù)
//在每個(gè)controller中注入transactionManager @Resource private PlatformTransactionManager transactionManager; ? @PostMapping(value = "setCode") @ResponseBody public void setCode(Invoice invoice, InvoiceAddress invoiceAddress,String token,String orderIDs, ? ? ? ? ? ? ? ? ? ? Integer pid,HttpServletResponse response){? ? ? DefaultTransactionDefinition defaultTransactionDefinition = new DefaultTransactionDefinition(); ? ? defaultTransactionDefinition.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED); ? ? TransactionStatus status = transactionManager.getTransaction(defaultTransactionDefinition); ? ? ? try { ? ? ? ? invoiceService.insert(token,pid,invoice); ? ? ? ? int iID= invoice.getId(); ? ? ? ? String substring = orderIDs.substring(0, orderIDs.length()-1); ? ? ? ? String[] split = substring.split(","); ? ? ? ? for (String string2 : split) { ? ? ? ? ? ? bOrderService.updateIStatus("1",string2); ? ? ? ? } ? ? ? ? invoiceOrderService.insert(iID,substring); ? ? ? ? if(Integer.parseInt(invoice.getiType())==1){ ? ? ? ? ? ? invoiceAddressService.insert(iID,invoiceAddress); ? ? ? ? } ? ? ? ? ? System.out.println("======制造一個(gè)運(yùn)行時(shí)異常aa======"); ? ? ? ? System.out.println("運(yùn)行時(shí)異常:"+100/0); ? ? ? ? ? //沒有異常便手動(dòng)提交事務(wù) ? ? ? ? transactionManager.commit(status); ? ? ? ? printJson(response,result(200,"ok")); ? ? }catch (Exception e){ ? ? ? ? //有異常便回滾事務(wù) ? ? ? ? transactionManager.rollback(status); ? ? ? ? e.printStackTrace(); ? ? ? ? printJson(response,result(500,"false")); ? ? }? }
2.將事務(wù)配置定義在Spring MVC的應(yīng)用上下文(spring-mvc.xml)中【簡(jiǎn)單明了、一勞永逸】
spring-mybatis.xml中事物管理器配置不變
在spring-mvc.xml中也定義事務(wù)配置:
<!-- ? ? 命名空間中 加入: ? ? xmlns:tx="http://www.springframework.org/schema/tx"? ?? ? ? http://www.springframework.org/schema/tx ? ? http://www.springframework.org/schema/tx/spring-tx.xsd --> <tx:annotation-driven/>
將@Transactional(rollbackFor = { Exception.class })注解打在Controller上
@Controller @RequestMapping(value = "/invoiceC") @Transactional(rollbackFor = { Exception.class }) public class InvoiceController extends BaseController {?? ? ? @Autowired ? ? private InvoiceService invoiceService; ? ? ? @Autowired ? ? private InvoiceOrderService invoiceOrderService; ? ? ? @Autowired ? ? private InvoiceAddressService invoiceAddressService; ? ? ? @Autowired ? ? private BalanceRechangeOrderService bOrderService;? ?? ? ? ? @PostMapping(value = "setCode") ? ? @ResponseBody ? ? public void setCode(Invoice invoice, InvoiceAddress invoiceAddress,String token,String orderIDs, ? ? ? ? ? ? ? ? ? ? ? ? Integer pid,HttpServletResponse response){ ? ? ? ? invoiceService.insert(token,pid,invoice);? ? ? ?? ? ? ? ? int iID= invoice.getId(); ? ? ? ? String substring = orderIDs.substring(0, orderIDs.length()-1);//截取最后一個(gè) ? ? ? ? String[] split = substring.split(",");//以逗號(hào)分割? ? ? ? ? for (String string2 : split) { ? ? ? ? ? ? bOrderService.updateIStatus("1",string2); ? ? ? ? }? ? ? ? ? invoiceOrderService.insert(iID,substring);? ? ? ? ? if(Integer.parseInt(invoice.getiType())==1){ ? ? ? ? ? ? //紙質(zhì)發(fā)票,收貨地址 ? ? ? ? ? ? invoiceAddressService.insert(iID,invoiceAddress); ? ? ? ? }? ? ? ? ? System.out.println("======制造一個(gè)運(yùn)行時(shí)異常aa======"); ? ? ? ? System.out.println("運(yùn)行時(shí)異常:"+100/0); ? ? ? ? printJson(response,result(200,"ok"));? ? ? } }
現(xiàn)在,我們來(lái)談?wù)劄槭裁粗埃浚?=》
- 在spring-mybatis.xml的<aop:config>添加對(duì)Controller的聲明式事務(wù)攔截
- 在Controller的class加上@Transactional
兩者均未生效呢???
原理:因?yàn)閟pring容器和spring-mvc是父子容器。在服務(wù)器啟動(dòng)時(shí),會(huì)先加載web.xml配置文件 ==> 再加載spring配置文件 ==> 再回到web.xml【加載監(jiān)聽器;加載過(guò)濾器;加載前端控制器】==>再加載springMVC配置文件
在Spring配置文件中,我們掃描注冊(cè)的是service實(shí)現(xiàn)類,就算掃描注冊(cè)了controller 也會(huì)在后面加載SpringMVC配置文件[掃描注冊(cè)controller]覆蓋掉,所以想要在controller中實(shí)現(xiàn)事務(wù)管理,僅在spring配置文件配置<tx:annotation-driven>或<aop:config>是沒有效果的,必須將事務(wù)配置定義在Spring MVC的應(yīng)用上下文(spring-mvc.xml)中。
因?yàn)樵趕pring-framework-reference.pdf文檔中說(shuō)明了:
<tx:annoation-driven/>只會(huì)查找和它在相同的應(yīng)用上下文件中定義的bean上面的@Transactional注解
SSM下Controller層的事務(wù)配置問(wèn)題
在寫項(xiàng)目過(guò)程中遇到了多表聯(lián)合修改數(shù)據(jù)時(shí)的事務(wù)問(wèn)題,按照之前的學(xué)習(xí),事務(wù)都是配置在service層中的,但是我的項(xiàng)目模塊里一個(gè)service對(duì)應(yīng)一個(gè)數(shù)據(jù)表,所以想在controller層加一個(gè)針對(duì)多個(gè)表的數(shù)據(jù)修改以及添加的事務(wù)配置。悲慘的是,在controller層配置事務(wù)出錯(cuò)沒有回滾!
按照我已所接觸的邏輯,控制層是不建議寫業(yè)務(wù)邏輯的,所以在里面調(diào)用的是多個(gè)service層的接口(使用Autowired)來(lái)調(diào)用多個(gè)表的業(yè)務(wù)操作。但是多個(gè)表形成一個(gè)事務(wù),所以我沒找在service層里單獨(dú)添加事務(wù)的合適的方法。如果有前輩想到合適的方法,望賜教!叩謝!
解決
原來(lái)的配置
首先是在service層上添加事務(wù)的配置,我這里的事務(wù)處理采用的是注解的方式,所以配置文件相較于配置事務(wù)的方式大大簡(jiǎn)化了。
首先命名空間中加入:
xmlns:tx="http://www.springframework.org/schema/tx" http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
然后是xml文件的配置:
? <!-- service除了業(yè)務(wù)(操作dao)還要有事務(wù) --> ? <tx:annotation-driven ? transaction-manager="txManager" /> ? <!-- 配置Spring的聲明式事務(wù)管理器 --> ? <bean id="txManager" ? class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> ? <property name="dataSource" ref="dataSource"></property> ? </bean>
其中,數(shù)據(jù)源我是配置在了dao層的配置文件中,由于都在spring的管理之下,所以在service直接使用是能夠找到的。
以下是我的maven依賴的jar包版本:
<!-- https://mvnrepository.com/artifact/org.springframework/spring-tx --> ? <dependency> ? ?? ?<groupId>org.springframework</groupId> ? ?? ?<artifactId>spring-tx</artifactId> ? ?? ?<version>5.1.5.RELEASE</version> ? </dependency> ? <!-- Spring jdbc事務(wù)管理 --> ? <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc --> ? <dependency> ? ?? ?<groupId>org.springframework</groupId> ? ?? ?<artifactId>spring-jdbc</artifactId> ? ?? ?<version>5.1.5.RELEASE</version> ? </dependency>
以上是我起初的配置。但是僅僅這樣是無(wú)法在controller層添加事務(wù)的。
修正后的配置
在service層的配置文件不變的情況下,我們想要在controller層添加事務(wù),只需要在spring-mvc.xml中引入事務(wù)的注解驅(qū)動(dòng)標(biāo)簽即可。
<!--在xml文件頭部引入命名空間,參考serviice層--> <tx:annotation-driven/>
為什么會(huì)這樣?
首先我們來(lái)看配置文件的加載:
? <!-- 配置前端控制器 --> ? <servlet> ? <servlet-name>DispatcherServlet</servlet-name> ? <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> ? <init-param> ? ?? ?<param-name>contextConfigLocation</param-name> ? ?? ?<param-value>classpath:spring-mvc.xml</param-value> ? </init-param> ? </servlet> ? <servlet-mapping> ? <servlet-name>DispatcherServlet</servlet-name> ? <url-pattern>*.action</url-pattern> ? </servlet-mapping> ? <!-- 配置spring容器加載的監(jiān)聽器 --> ? <listener> ? <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> ? </listener> ? <context-param> ? <param-name>contextConfigLocation</param-name> ? <param-value>classpath:spring-*.xml</param-value> ? </context-param>
以上是我的web.xml的部分配置。在項(xiàng)目啟動(dòng)過(guò)程中,加載spring-mvc.xml是使用DispatcherServlet加載的,而加載spring-service.xml與spring-dao.xml使用的是ContextLoaderListener。
然后我們需要知道的是,ContextLoaderListener是早于DispatcherServlet啟動(dòng)的,而在ContextLoaderListener加載service層配置時(shí)controller并沒有加載到容器中,但是此時(shí)事務(wù)的動(dòng)態(tài)代理已經(jīng)切入到了service層,所以后續(xù)的controller層并沒有被增強(qiáng)。
因此,我們需要在controller層再次加入 <tx:annotation-driven/>。
僅為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
使用Springboot整合GridFS實(shí)現(xiàn)文件操作
這篇文章主要介紹了使用Springboot整合GridFS實(shí)現(xiàn)文件操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10使用Java判定一個(gè)數(shù)值是否在指定的開閉區(qū)間范圍內(nèi)
這篇文章主要給大家介紹了關(guān)于使用Java判定一個(gè)數(shù)值是否在指定的開閉區(qū)間范圍內(nèi)的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Java具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-09-09druid?handleException執(zhí)行流程源碼解析
這篇文章主要為大家介紹了druid?handleException執(zhí)行流程源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09Spring Security實(shí)現(xiàn)自動(dòng)登陸功能示例
自動(dòng)登錄在很多網(wǎng)站和APP上都能用的到,解決了用戶每次輸入賬號(hào)密碼的麻煩。本文就使用Spring Security實(shí)現(xiàn)自動(dòng)登陸功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11Java操作MinIO實(shí)現(xiàn)文件的上傳和刪除
本文主要介紹如何將本地Java項(xiàng)目resources目錄下的一個(gè)PNG圖片上傳到MinIO,然后將上傳的圖片刪除,文中有詳細(xì)的流程步驟和示例代碼,需要的朋友可以參考下2023-06-06SpringBoot?+?Redis如何解決重復(fù)提交問(wèn)題(冪等)
在開發(fā)中,一個(gè)對(duì)外暴露的接口可能會(huì)面臨瞬間的大量重復(fù)請(qǐng)求,本文就介紹了SpringBoot + Redis如何解決重復(fù)提交問(wèn)題,具有一定的參考價(jià)值,感興趣的可以了解一下2021-12-12Java?axios與spring前后端分離傳參規(guī)范總結(jié)
這篇文章主要介紹了Java?axios與spring前后端分離傳參規(guī)范總結(jié),文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的朋友可以參考一下2022-08-08Java filter中的chain.doFilter使用詳解
這篇文章主要介紹了Java filter中的chain.doFilter使用詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11