zoukankan      html  css  js  c++  java
  • spring和mybatis整合之添加事务

     <!-- 事务管理器 -->
     <bean id="txManager" 
     class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
     <property name="dataSource" ref="dataSource"></property>
     
     </bean>
     <!-- 事务增强 -->
     <tx:advice id="txAdvice" transaction-manager="txManager">
      <tx:attributes>
        <tx:method name="add*" propagation="REQUIRED"/>
    			<tx:method name="insert*" propagation="REQUIRED"/>
    			<tx:method name="update*" propagation="REQUIRED"/>
    			<tx:method name="del*" propagation="REQUIRED"/>
    			<tx:method name="find*" propagation="NOT_SUPPORTED"/>
    			<tx:method name="search*" propagation="NOT_SUPPORTED"/>
    			<tx:method name="*" propagation="SUPPORTS" read-only="true"/>
      </tx:attributes>
     </tx:advice>
    

      

    propagation:事务传播机制

    REQUIRED(默认值)

    REQUIRES_NEW 、MANDATORY、NESTED

    SUPPORTS NOT_SUPPORTED、NEVER

    isolation:事务隔离等级 DEFAULT(默认值)

    READ_COMMITTED READ_UNCOMMITTED

    REPEATABLE_READ SERIALIZABLE

    timeout:事务超时时间,允许事务运行的最长时间,以秒为单位。默认值为-1,表示不超时

    read-only:事务是否为只读,默认值为false

    rollback-for:设定能够触发回滚的异常类型

    Spring默认只在抛出runtime exception时才标识事务回滚 可以通过全限定类名指定需要回滚事务的异常,多个类名用逗号隔开

    no-rollback-for:设定不触发回滚的异常类型

    Spring默认checked Exception不会触发事务回滚 可以通过全限定类名指定不需回滚事务的异常,多个类名用英文逗号隔开

    <!-- 华丽的分割线--!>

    在Spring配置文件中配置事务管理类,并添加对注解配置的事务的支持

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

      

    使用@Transactional为方法添加事务支持

    @Transactional
    @Service("userService")
    public class UserServiceImpl implements UserService {
     	……
    	@Transactional(propagation = Propagation.SUPPORTS)
        	public List<User> findUsersWithConditions(User user) {
            		。。。。。。。。。。。。。。。。。。。。
        }}
    

      

  • 相关阅读:
    JavaScript入门基础(三)
    JavaScript入门基础(二)
    Web页面该如何布局
    如何通过SQL创建删除表的索引,UNIQUE KEY
    vim使用大全
    安装vmwaretools后 真机和虚拟机仍不能复制黏贴
    php通用函数html时间文件大小生成随机数
    Centos下安装配置phpmyadmin
    [Leetcode 43] 11 Container With Most Water
    [Leetcode 39] 129 Sum Root to Leaf Numbers
  • 原文地址:https://www.cnblogs.com/XingPeng/p/10617221.html
Copyright © 2011-2022 走看看