<?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:context="http://www.springframework.org/schema/context"
     xmlns:aop="http://www.springframework.org/schema/aop"
     xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-2.5.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
  
     <bean id="sessionFactory" 
             class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 
         <property name="configLocation" value="classpath:hibernate.cfg.xml" /> 
         <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />
     </bean>     <!-- 定义事务管理器(声明式的事务) --> 
     <bean id="transactionManager"
         class="org.springframework.orm.hibernate3.HibernateTransactionManager">
         <property name="sessionFactory" ref="sessionFactory" />
     </bean>
    
     <!-- 配置DAO -->
     <bean id="userDaoTarget" class="com.bluesky.spring.dao.UserDaoImpl">
         <property name="sessionFactory" ref="sessionFactory" />
     </bean>
    
     <bean id="userDao" 
         class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> 
            <!-- 配置事务管理器 --> 
            <property name="transactionManager" ref="transactionManager" />    
         <property name="target" ref="userDaoTarget" /> 
          <property name="proxyInterfaces" value="com.bluesky.spring.dao.GeneratorDao" />
         <!-- 配置事务属性 --> 
         <property name="transactionAttributes"> 
             <props> 
                 <prop key="*">PROPAGATION_REQUIRED</prop>
             </props> 
         </property> 
     </bean> 
 </beans>

     第二种方式:所有Bean共享一个代理基类
 

<?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:context="http://www.springframework.org/schema/context"
     xmlns:aop="http://www.springframework.org/schema/aop"
     xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-2.5.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">    <bean id="sessionFactory" 
             class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 
         <property name="configLocation" value="classpath:hibernate.cfg.xml" /> 
         <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />
     </bean>     <!-- 定义事务管理器(声明式的事务) --> 
     <bean id="transactionManager"
         class="org.springframework.orm.hibernate3.HibernateTransactionManager">
         <property name="sessionFactory" ref="sessionFactory" />
     </bean>
    
     <bean id="transactionBase" 
             class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" 
             lazy-init="true" abstract="true"> 
         <!-- 配置事务管理器 --> 
         <property name="transactionManager" ref="transactionManager" /> 
         <!-- 配置事务属性 --> 
         <property name="transactionAttributes"> 
             <props> 
                 <prop key="*">PROPAGATION_REQUIRED</prop> 
             </props> 
         </property> 
     </bean>   
   
     <!-- 配置DAO -->
     <bean id="userDaoTarget" class="com.bluesky.spring.dao.UserDaoImpl">
         <property name="sessionFactory" ref="sessionFactory" />
     </bean>
    
     <bean id="userDao" parent="transactionBase" > 
         <property name="target" ref="userDaoTarget" />  
     </bean>
 </beans>

 第三种方式:使用拦截器
 

<?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:context="http://www.springframework.org/schema/context"
     xmlns:aop="http://www.springframework.org/schema/aop"
     xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-2.5.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">    <bean id="sessionFactory" 
             class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 
         <property name="configLocation" value="classpath:hibernate.cfg.xml" /> 
         <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />
     </bean>     <!-- 定义事务管理器(声明式的事务) --> 
     <bean id="transactionManager"
         class="org.springframework.orm.hibernate3.HibernateTransactionManager">
         <property name="sessionFactory" ref="sessionFactory" />
     </bean> 
   
     <bean id="transactionInterceptor" 
         class="org.springframework.transaction.interceptor.TransactionInterceptor"> 
         <property name="transactionManager" ref="transactionManager" /> 
         <!-- 配置事务属性 --> 
         <property name="transactionAttributes"> 
             <props> 
                 <prop key="*">PROPAGATION_REQUIRED</prop> 
             </props> 
         </property> 
     </bean>
      
     <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"> 
         <property name="beanNames"> 
             <list> 
                 <value>*Dao</value>
             </list> 
         </property> 
         <property name="interceptorNames"> 
             <list> 
                 <value>transactionInterceptor</value> 
             </list> 
         </property> 
     </bean> 
  
     <!-- 配置DAO -->
     <bean id="userDao" class="com.bluesky.spring.dao.UserDaoImpl">        <property name="sessionFactory" ref="sessionFactory" />    </bean></beans>

第四种方式:使用tx标签配置的拦截器
 

<?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:context="http://www.springframework.org/schema/context"
     xmlns:aop="http://www.springframework.org/schema/aop"
     xmlns:tx="http://www.springframework.org/schema/tx"
     xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-2.5.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">    <context:annotation-config />
     <context:component-scan base-package="com.bluesky" />    <bean id="sessionFactory" 
             class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 
         <property name="configLocation" value="classpath:hibernate.cfg.xml" /> 
         <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />
     </bean>     <!-- 定义事务管理器(声明式的事务) --> 
     <bean id="transactionManager"
         class="org.springframework.orm.hibernate3.HibernateTransactionManager">
         <property name="sessionFactory" ref="sessionFactory" />
     </bean>    <tx:advice id="txAdvice" transaction-manager="transactionManager">
         <tx:attributes>
             <tx:method name="*" propagation="REQUIRED" />
         </tx:attributes>
     </tx:advice>
    
     <aop:config>
         <aop:pointcut id="interceptorPointCuts"
             expression="execution(* com.bluesky.spring.dao.*.*(..))" />
         <aop:advisor advice-ref="txAdvice"
             pointcut-ref="interceptorPointCuts" />       
     </aop:config>     
 </beans>

第五种方式:全注解
 

<?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:context="http://www.springframework.org/schema/context"
     xmlns:aop="http://www.springframework.org/schema/aop"
     xmlns:tx="http://www.springframework.org/schema/tx"
     xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-2.5.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">    <context:annotation-config />
     <context:component-scan base-package="com.bluesky" />    <tx:annotation-driven transaction-manager="transactionManager"/>
    <bean id="sessionFactory" 
             class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 
         <property name="configLocation" value="classpath:hibernate.cfg.xml" /> 
         <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />
     </bean>     <!-- 定义事务管理器(声明式的事务) --> 
     <bean id="transactionManager"
         class="org.springframework.orm.hibernate3.HibernateTransactionManager">
         <property name="sessionFactory" ref="sessionFactory" />
     </bean>
    
 </beans>


脏读(事务没提交,提前读取):脏读就是指当一个事务正在访问数据,并且对数据进行了修改,而这种修改还没有提交到数据库中,这时,另外一个事务也访问这个数据,然后使用了这个数据。
不可重复度(两次读的不一致):指在一个事务内,多次读同一数据。在这个事务还没有结束时,另外一个事务也访问该同一数据。那么,在第一个事务中的两次读数据之间,由于第二个事务的修改,那么第一个事务两次读到的数据可能不一样,这样就发生了在一个事务内两次读到的数据是不一致的,因此称为是不可重复读。
幻读:是指当事务不是独立运行时发生的一种现象,例如第一个事务对一个表中的数据进行了修改,这种修改涉及到表中的全部数据行。同时,第二个事务也修改了这个表中的数据,这种修改是向表中插入一行新数据。那么,以后就会发生操作第一个事务的用户发现表中的、还没有修改数据行,就好像发生了幻觉一样
回滚丢失:当2个事务更新相同的数据源,如果第一个事务被提交,而另外一个事务却被撤销,那么会连同第一个事务所做的跟新也被撤销,也就是说第一个事务做的跟新丢失了
覆盖丢失:第二类更新丢失实在实际应用中经常遇到的并发问题,他和不可重复读本质上是同一类并发问题,通常他被看做不可重复读的特例;当2个或这个事务查询同样的记录后各自基于最初的查询结果更新该行时,会造成第二类丢失更新。因为每个事务都不知道不知道其他事务的存在,最后一个事务对记录做的修改将覆盖其他事务对该记录做的已提交的更新。。。