SSM如何實現(xiàn)在Controller中添加事務(wù)管理
SSM在Controller中添加事務(wù)管理
本人使用:
- 集成開發(fā)環(huán)境:idea
- 項目管理工具:maven
- 數(shù)據(jù)庫:oracle
- 框架:Spring+SpringMVC+myBatis
一般而言,事務(wù)都是加在Service層的,但也可以加在Controller層。。
看了不少人的博客,總結(jié)出兩個方法:
- 在controller層寫編程式事務(wù)
- 將事務(wù)配置定義在Spring MVC的應(yīng)用上下文(spring-mvc.xml)中
現(xiàn)在具體來說說怎么實現(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 ? ? 但因為是在controller中寫編程式事務(wù),這里可以不配置<tx:annotation-driven transaction-manager="transactionManager" /> --> <tx:annotation-driven transaction-manager="transactionManager" />
在controller中的方法里編寫事務(wù)
//在每個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("======制造一個運行時異常aa======"); ? ? ? ? System.out.println("運行時異常:"+100/0); ? ? ? ? ? //沒有異常便手動提交事務(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)中【簡單明了、一勞永逸】
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);//截取最后一個 ? ? ? ? String[] split = substring.split(",");//以逗號分割? ? ? ? ? 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("======制造一個運行時異常aa======"); ? ? ? ? System.out.println("運行時異常:"+100/0); ? ? ? ? printJson(response,result(200,"ok"));? ? ? } }
現(xiàn)在,我們來談?wù)劄槭裁粗???=》
- 在spring-mybatis.xml的<aop:config>添加對Controller的聲明式事務(wù)攔截
- 在Controller的class加上@Transactional
兩者均未生效呢???
原理:因為spring容器和spring-mvc是父子容器。在服務(wù)器啟動時,會先加載web.xml配置文件 ==> 再加載spring配置文件 ==> 再回到web.xml【加載監(jiān)聽器;加載過濾器;加載前端控制器】==>再加載springMVC配置文件
在Spring配置文件中,我們掃描注冊的是service實現(xiàn)類,就算掃描注冊了controller 也會在后面加載SpringMVC配置文件[掃描注冊controller]覆蓋掉,所以想要在controller中實現(xiàn)事務(wù)管理,僅在spring配置文件配置<tx:annotation-driven>或<aop:config>是沒有效果的,必須將事務(wù)配置定義在Spring MVC的應(yīng)用上下文(spring-mvc.xml)中。
因為在spring-framework-reference.pdf文檔中說明了:
<tx:annoation-driven/>只會查找和它在相同的應(yīng)用上下文件中定義的bean上面的@Transactional注解
SSM下Controller層的事務(wù)配置問題
在寫項目過程中遇到了多表聯(lián)合修改數(shù)據(jù)時的事務(wù)問題,按照之前的學(xué)習(xí),事務(wù)都是配置在service層中的,但是我的項目模塊里一個service對應(yīng)一個數(shù)據(jù)表,所以想在controller層加一個針對多個表的數(shù)據(jù)修改以及添加的事務(wù)配置。悲慘的是,在controller層配置事務(wù)出錯沒有回滾!
按照我已所接觸的邏輯,控制層是不建議寫業(yè)務(wù)邏輯的,所以在里面調(diào)用的是多個service層的接口(使用Autowired)來調(diào)用多個表的業(yè)務(wù)操作。但是多個表形成一個事務(wù),所以我沒找在service層里單獨添加事務(wù)的合適的方法。如果有前輩想到合適的方法,望賜教!叩謝!
解決
原來的配置
首先是在service層上添加事務(wù)的配置,我這里的事務(wù)處理采用的是注解的方式,所以配置文件相較于配置事務(wù)的方式大大簡化了。
首先命名空間中加入:
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>
以上是我起初的配置。但是僅僅這樣是無法在controller層添加事務(wù)的。
修正后的配置
在service層的配置文件不變的情況下,我們想要在controller層添加事務(wù),只需要在spring-mvc.xml中引入事務(wù)的注解驅(qū)動標簽即可。
<!--在xml文件頭部引入命名空間,參考serviice層--> <tx:annotation-driven/>
為什么會這樣?
首先我們來看配置文件的加載:
? <!-- 配置前端控制器 --> ? <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的部分配置。在項目啟動過程中,加載spring-mvc.xml是使用DispatcherServlet加載的,而加載spring-service.xml與spring-dao.xml使用的是ContextLoaderListener。
然后我們需要知道的是,ContextLoaderListener是早于DispatcherServlet啟動的,而在ContextLoaderListener加載service層配置時controller并沒有加載到容器中,但是此時事務(wù)的動態(tài)代理已經(jīng)切入到了service層,所以后續(xù)的controller層并沒有被增強。
因此,我們需要在controller層再次加入 <tx:annotation-driven/>。
僅為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
使用Springboot整合GridFS實現(xiàn)文件操作
這篇文章主要介紹了使用Springboot整合GridFS實現(xiàn)文件操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-10-10使用Java判定一個數(shù)值是否在指定的開閉區(qū)間范圍內(nèi)
這篇文章主要給大家介紹了關(guān)于使用Java判定一個數(shù)值是否在指定的開閉區(qū)間范圍內(nèi)的相關(guān)資料,文中通過實例代碼介紹的非常詳細,對大家學(xué)習(xí)或者使用Java具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2022-09-09druid?handleException執(zhí)行流程源碼解析
這篇文章主要為大家介紹了druid?handleException執(zhí)行流程源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-09-09Spring Security實現(xiàn)自動登陸功能示例
自動登錄在很多網(wǎng)站和APP上都能用的到,解決了用戶每次輸入賬號密碼的麻煩。本文就使用Spring Security實現(xiàn)自動登陸功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-11-11SpringBoot?+?Redis如何解決重復(fù)提交問題(冪等)
在開發(fā)中,一個對外暴露的接口可能會面臨瞬間的大量重復(fù)請求,本文就介紹了SpringBoot + Redis如何解決重復(fù)提交問題,具有一定的參考價值,感興趣的可以了解一下2021-12-12Java?axios與spring前后端分離傳參規(guī)范總結(jié)
這篇文章主要介紹了Java?axios與spring前后端分離傳參規(guī)范總結(jié),文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的朋友可以參考一下2022-08-08Java filter中的chain.doFilter使用詳解
這篇文章主要介紹了Java filter中的chain.doFilter使用詳解,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11