- 使用的是 aop 聲明式事務。 但是 循環插入寫在 Controller 層中所以一直不能回滾,Controller 層的掃描在 springmvc 的配置文件中使用
1 // 掃描包下的除了 @Controller 注冊到 spring spring的配置 事務的配置都是在spring的配置文件中的。 2 <context:component-scan base-package="包名">
3 <context:exclude-filter type="annotation" 4 expression="org.springframework.stereotype.Controller" /> // exclude-filter 過濾掉不掃描的注解 與之對應的是 include-filter 5 </context:component-scan> 6 7 8 // 使用 @RequestMapping 注解是必須開啟 annotation-driven 該注解要和掃描controller的注解一起放在spring主配置文件。 9 <mvc:annotation-driven />
- Controller 層的掃描是在 springmvc 的配置文件中實現的。 springmvc 也有一個屬於自己的子容器,相當於被 spring 容器包裹。 所以這種情況下 在 Controller 中使用 @transactional 會失效,配置切點也會失效。
- 好的解決方法是將事務在 Service 層進行管理。 有時候 try-catch 捕獲了事務異常,如果沒有 拋出一個 RuntimeException 也不會進行回滾。
