谁帮我讲讲spring框架里的“事务传播行为”和“事务隔离级别”是什么意思?
什么时候需要配置,怎么配置啊?

解决方案 »

  1.   

    事务传播行为:是指添加事物时的策略 
    1  PROPAGATION_REQUIRED
      如果当前没有事务,就新建一个事务,如果已经存在一个事务中,加入到这个事务中。这是最常见的选择。
    2  PROPAGATION_SUPPORTS
      支持当前事务,如果当前没有事务,就以非事务方式执行。
    3  PROPAGATION_MANDATORY
      使用当前的事务,如果当前没有事务,就抛出异常。
    4  PROPAGATION_REQUIRES_NEW
      新建事务,如果当前存在事务,把当前事务挂起。
    5  PROPAGATION_NOT_SUPPORTED
      以非事务方式执行操作,如果当前存在事务,就把当前事务挂起。
    6  PROPAGATION_NEVER
      以非事务方式执行,如果当前存在事务,则抛出异常。
    7  PROPAGATION_NESTED
      如果当前存在事务,则在嵌套事务内执行。如果当前没有事务,则执行与PROPAGATION_REQUIRED类似的操作。
    事务隔离级别 是指并发情况下由于数据变动而造成的脏读、幻读、不可重复读等等“事务传播行为”和“事务隔离级别”没什么关系
      

  2.   

    简单说:
    [color=#FF0000]事务传播行为[color=#FF6600]
    以添加用户为例:
    操作添加用户并记录日志
    dao层接口  方法
    Userdao:   adduser(..);
    Logdao:    adduserlog();服务层       方法
    Userservice adduser(..);当userservice 添加一个用户时得调两个dao层的方法(  adduser(..)和 adduserlog();  )
    第一dao层方法如果没有开启用事务而只有第二个方法才有事,显然是不合我们的事务要求的,
    我们可以设置事务的传播方式(如果当前没有事务,就新建一个事务...后面会有说明)[color=#FF0000]事务隔离级别[color=#FF6600]
    大概就地讲使用数据的锁的机制以下是以前的总结文档里找出来的

    Spring中的事务管理  Spring的事务管理通过AOP代理来实现
    根据事务属性,对每个代理对象的每个方法进行拦截,
    在方法执行前启动事务,
    方法执行完毕后根据是否有异常和异常种类进行提交或回滚  spring 默认通过捕获运行时异常实现事务提交或回滚
     事务属性:
    TransactionDefinition接口中定义事务属性
    事务属性通常由事务的传播行为,
    事务的隔离级别,
    事务的超时值
    事务只读标志等组成Spring事务传播方式
    主要控制当前的事务如何传播到另外的事务中PROPAGATION_NESTED 
    如果当前存在事务,则在嵌套事务内执行。
    如果当前没有事务,则进行与PROPAGATION_REQUIRED类似的操作。
    嵌套事务一个非常重要的概念就是内层事务依赖于外层事务。
    外层事务失败时,会回滚内层事务所做的动作。而内层事务操作失败并不会引起外层事务的回滚PROPAGATION_NEVER 
    以非事务方式执行,如果当前存在事务,则抛出异常。PROPAGATION_NOT_SUPPORTED
    以非事务方式执行操作,如果当前存在事务,就把当前事务挂起PROPAGATION_REQUIRED
    支持当前事务,如果当前没有事务,就新建一个事务。这是最常见的选择(默认)PROPAGATION_REQUIREDS_NEW
    新建事务,如果当前存在事务,把当前事务挂起。PROPAGATION_SUPPORTS
    支持当前事务,如果当前没有事务,就以非事务方式执行。PROPAGATION_MANDATORY
    支持当前事务,如果当前没有事务,就抛出异常。Spring事务隔离级别:
    主要定义事务与事务之间在数据库读写方面的控制范围
    主要解决脏读、不可重复读、虚读三个问题ISOLATION_DEFAULT 默认级别ISOLATION_READ_UNCOMMITED
    事务最低的隔离级别,充许别外一个事务可以看到这个事务未提交的数据,
    会产生脏读,不可重复读和幻像读ISOLATION_COMMITED
    保证一个事务修改的数据提交后才能被另外一个事务读取,可以避免脏读出现,
    但是可能会出现不可重复读和幻像读ISOLATION_REPEATABLE_READ
    保证一个事务不能读取另一个事务未提交的数据外可以防止脏读,不可重复读
    但是可能出现幻像读ISOLATION_SERIALIZABLE 
    花费最高代价但是最可靠的事务隔离级别。
    事务被处理为顺序执行。除了防止脏读,不可重复读外,还避免了幻像读Spring中的事务管理方式:
    编程式和声明式(常用)注意事项:  
    1. 事务边界设置在service层
    2. 需要使用SessionFactory.getCurrentSession
    (hibernate3.6)不需要配置Session上下文hibernate.current_session_context_class
    3. Spring默认通过捕获运行时异常实现事务回滚,
    非运行期异常不会触发rollback
    可以通过配置rollback-for 指定异常类
    事务中不要catch异常,否则spring放弃事务管理s
      

  3.   

    <?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:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
             http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
             http://www.springframework.org/schema/aop
             http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
             http://www.springframework.org/schema/tx 
             http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
             http://www.springframework.org/schema/context
             http://www.springframework.org/schema/context/spring-context-3.0.xsd">
      
    <!-- 加载数据源文件 <context:property-placeholder location="classpath:datasource.properties"/> connection.provider_class -->   

    <!-- 整合hibernate xml版本
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    </bean>-->
    <bean id="sessionFactory" 
          class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
            <property name="configLocation">
    <value>classpath:hibernate.cfg.xml</value>
    </property>
    <!--可以使用scan  
    <property name="packagesToScan">
    <value>com.xhc</value> 
    </property>
      --></bean>   <!-- 定义HibernateTransactionManager类    针对Hibernate特定实现的事务管理器 -->
      <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
      </bean>  <!-- 定义事务通知  设置事务属性  -->
      <tx:advice id="txAdvice"   transaction-manager="txManager">
           <tx:attributes>
              <tx:method name="save*"     propagation="REQUIRED"  isolation="DEFAULT" />
              <tx:method name="update*"   propagation="REQUIRED"  isolation="DEFAULT" />
              <tx:method name="delete*"   propagation="REQUIRED"  isolation="READ_COMMITTED" />
              <tx:method name="find*"     propagation="SUPPORTS"  read-only="true"/>
           </tx:attributes>
      </tx:advice>  <!-- 装配事务 -->
      <aop:config>
          <aop:pointcut id="transactionPointcut" 
                        expression="execution(public * com.xhc.*.*(..))"/>
          <aop:advisor  advice-ref="txAdvice" pointcut-ref="transactionPointcut"/>
       </aop:config>
       
      <!-- 声明spring为hibernate提供的模版类HibernateTemplate 
           HibernateTemplate是对Hibernate 操作Session执行CURD的封装
      -->
      <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate" >
         <property name="sessionFactory" ref="sessionFactory" />
      </bean>
      <!-- hibernate curd基础类 -->
      <bean id="hibernateDAOImpl" class="com.xhc.basedao.HibernateBaseDAOImpl" >
         <property name="sessionFactory" ref="sessionFactory" />
      </bean>
      
       <!-- 启用CGLIB 自动代理  testaaaaaaaa-->
       <aop:aspectj-autoproxy proxy-target-class="true"/>
       
      
      <!--
      搜索,自动注入
      <context:component-scan base-package="org.spring.ssh"/>-->
    </beans>