感性泪水和理性精神:SPRING事务管理

来源:百度文库 编辑:中财网 时间:2024/05/10 08:32:14

  前段时间对Spring的事务配置做了比较深入的研究,在此之间对Spring的事务配置虽说也配置过,但是一直没有一个清楚的认识。通过这次的学习发觉Spring的事务配置只要把思路理清,还是比较好掌握的。

    总结如下:

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

DataSourceTransactionManager这两部分只是会根据数据访问方式有所变化,比如使用Hibernate进行数据访问时,DataSource实际为SessionFactoryTransactionManager的实现为HibernateTransactionManager

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

    第一种方式:每个Bean都有一个代理


    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">

                class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 
         
       
   
 

     
            class="org.springframework.orm.hibernate3.HibernateTransactionManager">
       
   

   
   
   
       
   

   
            class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> 
            
               
         
        
         
         
             
                PROPAGATION_REQUIRED
           
 
       
 
   
 

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


    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">

                class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 
         
       
   
 

     
            class="org.springframework.orm.hibernate3.HibernateTransactionManager">
       
   

   
                class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" 
            lazy-init="true" abstract="true"> 
         
         
         
         
             
                PROPAGATION_REQUIRED 
           
 
       
 
   
   
  
   
   
       
   

   
     
          
   

第三种方式:使用拦截器


    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">

                class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 
         
       
   
 

     
            class="org.springframework.orm.hibernate3.HibernateTransactionManager">
       
   
 
  
            class="org.springframework.transaction.interceptor.TransactionInterceptor"> 
         
         
         
             
                PROPAGATION_REQUIRED 
           
 
       
 
   

     
     
         
             
                *Dao
           
 
       
 
         
             
                transactionInterceptor 
           
 
       
 
   
 
 
   
   
       
   

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


    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">

   
   

                class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 
         
       
   
 

     
            class="org.springframework.orm.hibernate3.HibernateTransactionManager">
       
   


   
       
           
       

   

   
   
                    expression="execution(* com.bluesky.spring.dao.*.*(..))" />
                    pointcut-ref="interceptorPointCuts" />       
   
     

第五种方式:全注解


    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">

   
   

   

                class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 
         
       
   
 

     
            class="org.springframework.orm.hibernate3.HibernateTransactionManager">
       
   

   

此时在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 listUsers() {
        return this.getSession().createQuery("from User").list();
    }
   
   
}

 

 

Spring事务原理

统观spring事务,围绕着两个核心PlatformTransactionManager和TransactionStatus

spring提供了几个关于事务处理的类: 
TransactionDefinition //事务属性定义
TranscationStatus //代表了当前的事务,可以提交,回滚。
PlatformTransactionManager这个是spring提供的用于管理事务的基础接口,其下有一个实现的抽象类AbstractPlatformTransactionManager,我们使用的事务管理类例如DataSourceTransactionManager等都是这个类的子类。

一般事务定义步骤:

TransactionDefinition td = new TransactionDefinition();

TransactionStatus ts = transactionManager.getTransaction(td);

try

{ //do sth

  transactionManager.commit(ts);

}

catch(Exception e){transactionManager.rollback(ts);}

 


spring提供的事务管理可以分为两类:编程式的和声明式的。编程式的,比较灵活,但是代码量大,存在重复的代码比较多;声明式的比编程式的更灵活。

编程式主要使用transactionTemplate。省略了部分的提交,回滚,一系列的事务对象定义,需注入事务管理对象.

void add()

{

    transactionTemplate.execute( new TransactionCallback(){

        pulic Object doInTransaction(TransactionStatus ts)

       { //do sth}

    }

}


声明式:

使用TransactionProxyFactoryBean:


  
  
  
   
    PROPAGATION_REQUIRED
    PROPAGATION_REQUIRED
    PROPAGATION_REQUIRED,readOnly
   

  

 


围绕Poxy的动态代理 能够自动的提交和回滚事务
org.springframework.transaction.interceptor.TransactionProxyFactoryBean

·                               PROPAGATION_REQUIRED--支持当前事务,如果当前没有事务,就新建一个事务。这是最常见的选择。

·                               PROPAGATION_SUPPORTS--支持当前事务,如果当前没有事务,就以非事务方式执行。

·                               PROPAGATION_MANDATORY--支持当前事务,如果当前没有事务,就抛出异常。

·                               PROPAGATION_REQUIRES_NEW--新建事务,如果当前存在事务,把当前事务挂起。

·                               PROPAGATION_NOT_SUPPORTED--以非事务方式执行操作,如果当前存在事务,就把当前事务挂起。

·                               PROPAGATION_NEVER--以非事务方式执行,如果当前存在事务,则抛出异常。

·                               PROPAGATION_NESTED--如果当前存在事务,则在嵌套事务内执行。如果当前没有事务,则进行与PROPAGATION_REQUIRED类似的操作。

Http之Get/Post请求区别

1HTTP请求格式:

[]

HTTP请求中,第一行必须是一个请求行(request line),用来说明请求类型、要访问的资源以及使用的HTTP版本。紧接着是一个首部(header)小节,用来说明服务器要使用的附加信息。在首部之后是一个空行,再此之后可以添加任意的其他数据[称之为主体(body]

2GETPOST区别

HTTP定义了与服务器交互的不同方法,最基本的方法是 GET POST.

HTTP-GETHTTP-POST是使用HTTP的标准协议动词,用于编码和传送变量名/变量值对参数,并且使用相关的请求语义。每个HTTP-GETHTTP-POST都由一系列HTTP请求头组成,这些请求头定义了客户端从服务器请求了什么,而响应则是由一系列HTTP应答头和应答数据组成,如果请求成功则返回应答。
  HTTP-GET以使用MIME类型application/x-www-form-urlencodedurlencoded文本的格式传递参数。Urlencoding是一种字符编码,保证被传送的参数由遵循规范的文本组成,例如一个空格的编码是"%20"。附加参数还能被认为是一个查询字符串。
  与HTTP-GET类似,HTTP-POST参数也是被URL编码的。然而,变量名/变量值不作为URL的一部分被传送,而是放在实际的HTTP请求消息内部被传送。

1get是从服务器上获取数据,post是向服务器传送数据。

1)   在客户端,Get方式在通过URL提交数据,数据在URL中可以看到;POST方式,数据放置在HTML HEADER内提交。

2 对于get方式,服务器端用Request.QueryString获取变量的值,对于post方式,服务器端用Request.Form获取提交的数据。

2)   GET方式提交的数据最多只能有1024字节,而POST则没有此限制。

3)   安全性问题。正如在(1)中提到,使用 Get 的时候,参数会显示在地址栏上,而 Post 不会。所以,如果这些数据是中文数据而且是非敏感数据,那么使用 get;如果用户输入的数据不是中文字符而且包含敏感数据,那么还是使用 post为好。

注:所谓安全的意味着该操作用于获取信息而非修改信息。幂等的意味着对同一 URL 的多个请求应该返回同样的结果。完整的定义并不像看起来那样严格。换句话说,GET 请求一般不应产生副作用。从根本上讲,其目标是当用户打开一个链接时,她可以确信从自身的角度来看没有改变资源。比如,新闻站点的头版不断更新。虽然第二次请求会返回不同的一批新闻,该操作仍然被认为是安全的和幂等的,因为它总是返回当前的新闻。反之亦然。POST 请求就不那么轻松了。POST 表示可能改变服务器上的资源的请求。仍然以新闻站点为例,读者对文章的注解应该通过 POST 请求实现,因为在注解提交之后站点已经不同了(比方说文章下面出现一条注解)。

下面举一个简单的例子来说明它们的区别:

分别通过getpost方式提交表单-->


http://wxf0701.cnblogs.com/
/>
">




http://wxf0701.cnblogs.com/
>
">


<% If Request.QueryString("Text") <> "" Then %>
通过get方式传递的字符串是: "<%= Request.QueryString("Text") %>"

<% End If %>

<% If Request.Form("Text") <> "" Then %>
通过Post方式传递的字符串是: "<%= Request.Form("Text") %>"

<% End If %>

享元模式

 

有和多个小的对象它们有很多属性相同,把它们变成一个对象,把不同的属性变成

方法的参数称之为外部状态,相同的属性称之为这个对象的内部状态