java - Spring +Mybatis 事務(wù) 不能回滾
問題描述
配置事務(wù)的文件 spring-mybatis.xml
<?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:p='http://www.springframework.org/schema/p' xmlns:context='http://www.springframework.org/schema/context' xmlns:tx='http://www.springframework.org/schema/tx' xmlns:mvc='http://www.springframework.org/schema/mvc' xsi:schemaLocation='http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-4.3.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd'> <context:component-scan base-package='com.ys.attendance.mapper'/> <bean class='org.springframework.beans.factory.config.PropertyPlaceholderConfigurer'><property name='location' value='classpath:conf/jdbc.properties' /> </bean> <bean destroy-method='close'><property name='driverClassName' value='${driver}' /><property name='url' value='${url}' /><property name='username' value='${username}' /><property name='password' value='${password}' /><!-- 初始化連接大小 --><property name='initialSize' value='${initialSize}'></property><!-- 連接池最大數(shù)量 --><property name='maxActive' value='${maxActive}'></property><!-- 連接池最大空閑 --><property name='maxIdle' value='${maxIdle}'></property><!-- 連接池最小空閑 --><property name='minIdle' value='${minIdle}'></property><!-- 獲取連接最大等待時間 --><property name='maxWait' value='${maxWait}'></property> </bean> <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 --> <bean class='org.mybatis.spring.SqlSessionFactoryBean'><property name='dataSource' ref='dataSource' /><property name='configLocation' value='classpath:conf/mybatis/mybatis-config.xml'></property><!-- 自動掃描mapping.xml文件 --><property name='mapperLocations' value='classpath:conf/SqlMapper/sqlmap.xml'></property> </bean> <!-- DAO接口所在包名,Spring會自動查找其下的類 --> <bean class='org.mybatis.spring.mapper.MapperScannerConfigurer'><property name='basePackage' value='com.ys.attendance.mapper' /><property name='sqlSessionFactoryBeanName' value='sqlSessionFactory'></property> </bean> <!-- (事務(wù)管理)transaction manager, use JtaTransactionManager for global tx --> <bean class='org.springframework.jdbc.datasource.DataSourceTransactionManager'><property name='dataSource' ref='dataSource' /> </bean><!-- 事務(wù)注解驅(qū)動,標(biāo)注@Transactional的類和方法將具有事務(wù)性 --> <tx:annotation-driven transaction-manager='transactionManager' proxy-target- /></beans>
在service 中寫了一個方法 是使用注解 在 這個service類上加了 @Transactional
@Transactional('transactionManager')@Servicepublic class AttendServiceImpl implements AttendService{@Overridepublic void traincation() { Map<String,String> map1 = new HashMap<String, String>(); map1.put('userid', '1111'); map1.put('username','肖總'); map1.put('date', '2017-03-15'); map1.put('state', '/');Map<String,String> map2 = new HashMap<String, String>(); map2.put('userid', '1131'); map2.put('username','小名'); map2.put('date', '2017-03-15'); map2.put('state', '/'); dao.add_attend_api(map1);try {int i = 1/0;//設(shè)置了一個異常 } catch (Exception e) {System.out.println(e.getMessage());TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); } dao.add_attend_api(map2);}}
執(zhí)行該方法 出現(xiàn)異常 沒有回滾,第一條數(shù)據(jù)插入到了數(shù)據(jù)庫中org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.transaction.NoTransactionException: No transaction aspect-managed TransactionStatus in scope
是哪里配置有問題么?
問題解答
回答1:<context:component-scan base-package='com.ys.attendance.mapper'/>
包掃描的范圍太小了吧。你的service在那個包下。
回答2:方法申明時候增加 throws Exception試試
public void traincation throws Exception(){
...
}
回答3:要把異常拋出去Spring才能捕獲到給你回滾呀把代碼改成這樣:
try { int i = 1/0;//設(shè)置了一個異常} catch (Exception e) { System.out.println(e.getMessage()); TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); Throw e;}
相關(guān)文章:
1. 數(shù)組按鍵值封裝!2. angular.js - webpack build后的angularjs路由跳轉(zhuǎn)問題3. java - web項目中,用戶登陸信息存儲在session中好 還是cookie中好,取決于什么?4. 老師,怎么不講一次性添加多個數(shù)據(jù)5. mysql - 查詢字段做了索引為什么不起效,還有查詢一個月的時候數(shù)據(jù)都是全部出來的,如果分拆3次的話就沒問題,為什么呢。6. mysql - 大部分?jǐn)?shù)據(jù)沒有行溢出的text字段是否需要拆表7. pdo - mysql 簡單注入疑問8. Mysql取下一條記錄9. python - linux 下用wsgifunc 運行web.py該如何修改代碼10. 表格對其 只涉及到對其,沒有涉及到大小,長寬還有背景色類的嗎
