`
曹逸飞
  • 浏览: 3250 次
  • 性别: Icon_minigender_1
  • 来自: 广州
最近访客 更多访客>>
社区版块
存档分类
最新评论

spring 事务总结

阅读更多

最近详细了解一下spring事务:


Spring配置文件中关于事务配置总是由三个组成部分,分别是DataSource、TransactionManager和代理机制这三部分,无论哪种配置方式,一般变化的只是代理机制这部分。

 

Spring配置文件中关于事务配置总是由三个组成部分,分别是DataSource、TransactionManager和代理机制这三部分,无论哪种配置方式,一般变化的只是代理机制这部分。

 

具体如下图:

 

根据代理机制的不同,总结了五种Spring事务的配置方式,配置文件如下:

 

第一种方式:每个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 >
   
   
<!-- 配置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 >

此时在DAO上需加上@Transactional注解,如下:

package com.bluesky.spring.dao;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Component;

import com.bluesky.spring.domain.User;

@Transactional
@Component(
" userDao " )
public   class UserDaoImpl extends HibernateDaoSupport implements UserDao {

   
public List < User > listUsers() {
       
return   this .getSession().createQuery( " from User " ).list();
    }
   
   
}

 

 

 

分享到:
评论

相关推荐

    spring事务总结.docx

    事务失效、事务回滚、大事务问题、编程式事务

    Spring事务总结

    spring配置事务 项目总结出来的 公司多年开发经验 使用 方便

    Spring中事务的传播属性详解

    Spring中事务的传播属性详解,Spring中事务的传播属性详解

    spring事务管理几种方式代码实例

    spring事务管理几种方式代码实例:涉及编程式事务,声明式事务之拦截器代理方式、AOP切面通知方式、AspectJ注解方式,通过不同方式实例代码展现,总结spring事务管理的一般规律,从宏观上加深理解spring事务管理特性...

    Spring事务五种不同的代理配置

    Spring事务配置的五种方式 ,根据代理机制的不同,总结了五种Spring事务的配置方式.

    Spring事务不生效.pdf

    Spring事务不生效,spring项目中常见事务不生效总结

    Spring总结.xmind

    包含Spring简介,AOP,SpringMVC,集成JDBC,事务,ORM框架,Spring集成知识点总结,以思维导图的形式展示,xmind文件。

    Spring事务配置的五种方式

    Spring事务配置的五种方式,学习总结

    Spring事务配置的五种方法(二)[总结].pdf

    Spring事务配置的五种方法(二)[总结].pdf

    Spring的事务10种常见失效场景总结.zip

    Spring的事务10种常见失效场景总结.zip

    spring杂谈 作者zhang KaiTao

    1.1 Spring事务处理时自我调用的解决方案及一些实现方式的风险 1.2 我对AOP的理解 1.3 Spring开闭原则的表现-BeanPostProcessor的扩展点-1 1.4 我对IoC/DI的理解 1.5 SpringMVC + spring3.1.1 + hibernate4.1.0 集成...

    全网最热spring问题总结.pdf

    总结了常见的spring面试题,方便学习者查漏补缺。 使用 Spring 框架的好处是什么? 轻量: Spring 是轻量的, 基本的版本大约 2MB。 控制反转: Spring 通过控制反转实现了松散耦合, 对象们给出它们的依 赖, ...

    spring声明事务,编程事务实现

    相信对于很多初学者或者使用者,都了解spring事务,但是具体细节,实现方式,都没有系统的概念或者无从下手,本人周末闲来无事,坐下总结,时间紧促,写的不周全的地方还请提出宝贵意见。

    Spring学习总结(不含整合其他框架)

    文档对Spring的知识做了一个全面的总结,具体讲解Spring的控制反转、依赖注入、面向切面编程、Spring中的Bean配置、Spring对JDBC的支持以及Spring的声明式事务

    全面分析Spring的编程式事务管理与声明式事务管理.doc

    本文总结了 Spring 事务管理的基本概念和机制,包括事务隔离级别、事务传播行为、编程式事务管理和声明式事务管理等。通过对 Spring 事务管理的理解,您将能够灵活运用事务管理机制来确保企业应用的可靠性和数据的...

    Spring Data JPA系列4——Spring声明式事务处理与多数据源支持.doc

    Spring 声明式事务处理与多数据源支持 在大部分涉及到数据库操作的项目里面,事务控制、事务处理都是一个无法回避的问题。Spring 框架提供了声明式事务处理机制,使得业务代码中进行事务控制操作起来非常简单。只需...

    spring的事务管理

    csdn博客竟然也出问题,好不容总结一下,竟然发不了博客。依稀csdn! 1、spring与hibernate事务管理 2、aop 3、事务管理,声明式。 3、事务传递特性说明

    Spring + Hibernate + Struts 事务配置小例子(带提示框等小技巧)

    前几天搞 Spring + Hibernate + Struts 事务配置 ,网上找了好多资料,不过好无语,大多都是 Ctrl + V,浪费俺的宝贵时间 现在我总结配出一套,给大家参考参考,可能有不足,请大家多多交流。 附:内有弹出...

    spring培训-笔记

    目录: Spring教程 1 名词解释 2 Spring框架概述 3 ...Spring的声明式事务 22 Spring对其它企业应用支持 22 注:后面的内容我将不再完善,但网上的朋友可以帮助完善,只需注明住处即可。

Global site tag (gtag.js) - Google Analytics