国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術文章
文章詳情頁

java - Spring +Mybatis 事務 不能回滾

瀏覽:127日期:2024-01-24 10:18:42

問題描述

配置事務的文件 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><!-- 連接池最大數量 --><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> <!-- (事務管理)transaction manager, use JtaTransactionManager for global tx --> <bean class='org.springframework.jdbc.datasource.DataSourceTransactionManager'><property name='dataSource' ref='dataSource' /> </bean><!-- 事務注解驅動,標注@Transactional的類和方法將具有事務性 --> <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;//設置了一個異常 } catch (Exception e) {System.out.println(e.getMessage());TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); } dao.add_attend_api(map2);}}

執行該方法 出現異常 沒有回滾,第一條數據插入到了數據庫中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;//設置了一個異常} catch (Exception e) { System.out.println(e.getMessage()); TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); Throw e;}

標簽: java
相關文章:
主站蜘蛛池模板: 精品欧美成人bd高清在线观看 | 日本免费毛片在线高清看 | 亚洲国产午夜精品理论片的软件 | 国内精品一区二区三区最新 | 九九亚洲视频 | 国产一区二区不卡 | 欧美日韩精品在线视频 | 2020亚洲男人天堂 | 一区二区欧美视频 | 无码精品一区二区三区免费视频 | 欧美一级毛片欧美一级无片 | 特级淫片国产免费高清视频 | 精品一区国产 | 18女人毛片大全 | 一区在线观看 | 亚洲成年男人的天堂网 | 亚州一级毛片 | 中文字幕日韩在线 | 亚洲 欧美 精品专区 极品 | 国产一区二区在线视频 | 美国人成毛片在线播放 | 国产精品高清在线观看地址 | 美女喷水网站 | 久久精品国产精品亚洲人人 | 欧美一级久久久久久久大 | 国产精品亚洲四区在线观看 | 日韩在线小视频 | 日韩 欧美 中文 亚洲 高清 在线 | 欧美一级片在线看 | 91精品久久久久含羞草 | 91视频免费播放 | 久久综合久久自在自线精品自 | 国产精品一区在线观看 | 香蕉网站狼人久久五月亭亭 | 国产亚洲精品国产第一 | 亚洲影院手机版777点击进入影院 | 美女视频网站色 | 国产精品亚洲欧美日韩区 | 男人天堂新地址 | a毛片a毛片a视频 | 大伊香蕉精品视频在线 |