java - spring事務(wù)不回滾
問題描述
1.spring事務(wù)不會回滾,網(wǎng)上的方法都試過了,沒有效果。2.配置如下:
1.spring.xml:<context:component-scan base-package='com.szcshl'><context:exclude-filter type='annotation' expression='org.springframework.stereotype.Controller'/> </context:component-scan>2.springmvc.xml
<context:component-scan base-package='com.szcshl' use-default-filters='false'><context:include-filter type='annotation' expression='org.springframework.stereotype.Controller'/> </context:component-scan>3.spring-hibernate.xml
<!-- 配置事務(wù)管理器 --> <bean name='transactionManager' class='org.springframework.orm.hibernate4.HibernateTransactionManager'><property name='sessionFactory' ref='sessionFactory'></property> </bean> <!-- 注解方式配置事物 --> <!-- <tx:annotation-driven transaction-manager='transactionManager' /> --> <!-- 攔截器方式配置事物 --> <tx:advice transaction-manager='transactionManager'><tx:attributes> <tx:method name='add*' /> <tx:method name='save*' /> <tx:method name='update*' /> <tx:method name='modify*' /> <tx:method name='edit*' /> <tx:method name='delete*' /> <tx:method name='remove*' /> <tx:method name='repair' /> <tx:method name='deleteAndRepair' /> <tx:method name='get*' propagation='SUPPORTS' read-only='true' /> <tx:method name='find*' propagation='SUPPORTS' read-only='true' /> <tx:method name='load*' propagation='SUPPORTS' read-only='true' /> <tx:method name='search*' propagation='SUPPORTS' read-only='true' /> <tx:method name='datagrid*' propagation='SUPPORTS' read-only='true' /> <tx:method name='*' propagation='SUPPORTS' read-only='true' /></tx:attributes> </tx:advice> <aop:config><aop:pointcut expression='execution(* com.szcshl.service..*Impl.*(..))' /><aop:advisor pointcut-ref='transactionPointcut' advice-ref='transactionAdvice' /> </aop:config>4.controller
@RequestMapping(value = '/Save' , method= RequestMethod.POST) public void departmentSave(Department department, HttpServletResponse response){ departmentService.save(department); throw new RuntimeException('拋出異常'); }5.service
@Service@Transactionalpublic class DepartmentServiceImpl extends BaseServiceImpl<Department> implements DepartmentService {@Autowired private DepartmentDao deptDao; ...... public void save(Department entity) {deptDao.save(entity); }}6.baseDao
public class BaseDaoImpl<T> implements BaseDao<T> { private SessionFactory sessionFactory; protected Class<T> entityClass; public SessionFactory getSessionFactory() {return sessionFactory; } @Resource public void setSessionFactory(SessionFactory sessionFactory) {this.sessionFactory = sessionFactory; } public Session getCurrentSession() {return this.sessionFactory.getCurrentSession(); } @SuppressWarnings('rawtypes') protected Class getEntityClass() {if (entityClass == null) { entityClass = (Class<T>) ((ParameterizedType) getClass() .getGenericSuperclass()).getActualTypeArguments()[0];}return entityClass; } @Transactional public Serializable save(T entity) {return this.getCurrentSession().save(entity); } ......}目錄結(jié)構(gòu):
問題解答
回答1:看了看,你對service接口進(jìn)行事務(wù)管理,而不是controller你拋出異常是在controller,當(dāng)然不會事務(wù)回滾啦
你試試在sevice實現(xiàn)類中,save一下,再拋個異常,看看save成功不成功
ps一下:mysql有兩個存儲引擎(常用),一個是InnoDB一個是MyISAM,前者支持行級鎖,事務(wù),外鍵,后者不支持
回答2:樓上說的沒錯,spring事務(wù)作用于service層,當(dāng)遇到service方法拋出異常時,事務(wù)將回滾。所以你的正確測試實踐應(yīng)該是在service層方法中拋出異常。
相關(guān)文章:
1. javascript - 原生canvas中如何獲取到觸摸事件的canvas內(nèi)坐標(biāo)?2. sql語句 - mysql中關(guān)聯(lián)表查詢問題3. javascript - iframe 為什么加載網(wǎng)頁的時候滾動條這樣顯示?4. javascript - vscode alt+shift+f 格式化js代碼,通不過eslint的代碼風(fēng)格檢查怎么辦。。。5. javascript - [js]為什么畫布里不出現(xiàn)圖片呢?在線等6. javascript - 有什么比較好的網(wǎng)頁版shell前端組件?7. javascript - 求解答:實例對象調(diào)用constructor,此時constructor內(nèi)的this的指向?8. javascript - 如何將一個div始終固定在某個位置;無論屏幕和分辨率怎么變化;div位置始終不變9. css - chrome下a標(biāo)簽嵌套img 顯示會多個小箭頭?10. html - vue項目中用到了elementUI問題
