Spring MVC 使用随笔

来源:互联网 发布:windows 新增api 编辑:程序博客网 时间:2024/06/08 00:05

此篇文章是博主在学习过程中的一些小记录,只是自己参考和记录,能力有限,请多多指教。

Spring 中事务的控制以及回滚:

           1. 首先要确定使用的数据库存储引擎是支持回滚的,例如 InnoDB。有些存储引擎是不支持回滚的例如 MyISAM。

               (ps: 查看存储引擎的方法: Navicat中选择要操作的表,设计表---> 选项)


           2. Mybatis的配置文件 本人习惯性的配置在 spring-applicationContext.xml中 :

<!-- Transaction Manager --><bean id="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource" /></bean><!-- scanner transaction --><tx:annotation-driven  transaction-manager="transactionManager" />
   

         3. 在需要回滚的方法中使用注解:

       @Transactional(value = "transactionManager", isolation = Isolation.READ_COMMITTED, propagation = Propagation                      .REQUIRED, rollbackFor = {Exception.class})       public void tranferTest() throws Exception{ 
                       ....
           throw new Exception("事务抛出异常");
           //在需要回滚时手动抛出异常
       }
     
     4.事务管理中的回滚是指将所进行的数据库操作进行回滚而不是将方法里的所有操作都“回滚”(撤销)

0 0