`
hain
  • 浏览: 449556 次
  • 来自: ...
社区版块
存档分类
最新评论

spring 2.0 事务

阅读更多
spring 2.0带来的aop变化和bean xml schema的变化,使得事务的处理变得更加的简单,同aop一样,事务也采用两种方式来处理,一种主要为xml 声明,另外的一种也就是注释的引入。
先来看第一种情况:
<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"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

  <!-- SessionFactory, DataSource, etc. omitted -->

  <bean id="myTxManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="mySessionFactory"/>
  </bean>
 
  <aop:config>
   <!-- 这定义了主要的切面,也就是那些接口可以使用事务,这里只是说执行ProductService的所有方法-->
    <aop:pointcut id="productServiceMethods" expression="execution(* product.ProductService.*(..))"/>
    <aop:advisor advice-ref="txAdvice" pointcut-ref="productServiceMethods"/>
  </aop:config>
  <!--主要的事务 advice 声明事务的相关属性-->
  <tx:advice id="txAdvice" transaction-manager="myTxManager">
    <tx:attributes>
      <tx:method name="increasePrice*" propagation="REQUIRED"/>
      <tx:method name="someOtherBusinessMethod" propagation="REQUIRES_NEW"/>
      <tx:method name="*" propagation="SUPPORTS" read-only="true"/>
    </tx:attributes>
  </tx:advice>

  <bean id="myProductService" class="product.SimpleProductService">
    <property name="productDao" ref="myProductDao"/>
  </bean>

</beans>
第二种方式,使用 @Transactional 注释


@Transactional

public interface FooService {

 

    Foo getFoo(String fooName);

 

    Foo getFoo(String fooName, String barName);

 

    void insertFoo(Foo foo);

 

    void updateFoo(Foo foo);

}


xml 配置

<!-- from the file 'context.xml' -->

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

       xsi:schemaLocation="

       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd

       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd

       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

 

  <!-- this is the service object that we want to make transactional -->

  <bean id="fooService" class="x.y.service.DefaultFooService"/>

 

  <!-- enable the configuration of transactional behavior based on annotations -->

  <tx:annotation-driven/>

 

  <!-- a PlatformTransactionManager is still required -->

  <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

    <!-- sourced from somewhere else -->

    <property name="dataSource" ref="dataSource"/>

  </bean>

 

  <!-- other <bean/> definitions here -->

 

</beans>

public class DefaultFooService implements FooService {

 

    public Foo getFoo(String fooName) {

        // do something

    }

 

    // these settings have precedence

    @Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)

    public void updateFoo(Foo foo) {

        // do something

    }

}

分享到:
评论

相关推荐

    Spring2.0 事务处理

    NULL 博文链接:https://todd-liangt.iteye.com/blog/337274

    spring2.0技术手册 (扫描版)

    第5章JDBC、事务支持 第6章Hibernate与Spring 第7章SpringWebMVC框架 第8章View层方案、Web框架整合 第9章API封装 第10章项目:Spring在线书签 Spring Framework 是一个开源的Java/Java EE全功能栈(full-stack)...

    spring2.0声明式事务

    3. spring2.0配置事务 a) 将spring 1.2升级到spring2.0 i. 去掉spring1.2相关的包 ii. 添加spring2.0的jar包:spring.jar,aspecjrt.jar,aspectjweaver.jar 和cglib-nodep-2.1.3,jar iii. 修改spring配置文件,增加...

    Spring2.0宝典 源码

    全书分22章,内容涵盖了Spring的核心机制、依赖注入、资源访问、AOP框架、事务框架、整合Hibernate、DAO支持、JDBC支持、MVC框架、整合第三方表现层技术、整合第三方MVC框架、远程访问支持、EJB访问和实现、Spring对...

    SPRING2.0中文帮助文档

    在此基础上,Spring还提供了包括声明式事务管理,RMI或Web Services远程访问业务逻辑,以及可以多种方法进行的持久化数据库地解决方案。另外,Spring还有一个全功能的 MVC框架,并能透明的把 AOP 集成到你的软件中去...

    Spring 2.0 + Struts 1.2 + Hibernate 3.2 + DWR 2.0 的JavaEE应用示例

    总结,该例示注释非常详细,演示Spring托管Hibernate和Struts的Action, 以及Spring的事务声明(包括1.x与2.x的用法)与非事务声明的使用方式。 使用DWR可以方便的使用Spring托管的持久层功能。 目的:希望广大Java...

    Spring 2.0 开发参考手册

    2. Spring 2.0 的新特性 2.1. 简介 2.2. 控制反转(IoC)容器 2.2.1. 更简单的XML配置 2.2.2. 新的bean作用域 2.2.3. 可扩展的XML编写 2.3. 面向切面编程(AOP) 2.3.1. 更加简单的AOP XML配置 2.3.2. 对@...

    spring2.0+struts1.2+hibernate3

    spring2.0整合struts1.2和hibernate3开发小例子,并配有AOP事务管理,所需要的支持包还需要根据指导手册导入,供大家参考。

    struts1.2+spring2.0+hibernate3.2 整合源码

    1、struts1.2+spring 2.0+hibernate3.2 2、struts的动作交由spring来管理,hibernate的配置集中在spring中配置。 3、增加了声明式事务处理,加强了hibernateTemplate的简单事务处理。 4、完整的Myeclipse的工程文件...

    基于springCloud2.0搭建lcn解决分布式事务

    基于springcloud2.0搭建lcn,里面有数据库,文档原理,还有原理分析,分布式事务应用场景,分布式事务解决方案

    spring2.0开发参考手册

    在此基础上,Spring还提供了包括声明式事务管理,RMI或Web Services远程访问业务逻辑,以及可以多种方法进行的持久化数据库地解决方案。另外,Spring还有一个全功能的 MVC框架,并能透明的把 AOP 集成到你的软件中去...

    spring2.0中文开发参考手册

    在此基础上,Spring还提供了包括声明式事务管理,RMI或Web Services远程访问业务逻辑,以及可以多种方法进行的持久化数据库地解决方案。另外,Spring还有一个全功能的 MVC框架,并能透明的把 AOP 集成到你的软件中去...

    spring 2.0中文API

    在此基础上,Spring还提供了包括声明式事务管理,RMI或Web Services远程访问业务逻辑,以及可以多种方法进行的持久化数据库地解决方案。另外,Spring还有一个全功能的 MVC框架,并能透明的把 AOP 集成到你的软件中去...

    spring2.0开发手册

    在此基础上,Spring还提供了包括声明式事务管理,RMI或Web Services远程访问业务逻辑,以及可以多种方法进行的持久化数据库地解决方案。另外,Spring还有一个全功能的 MVC框架,并能透明的把 AOP 集成到你的软件中去...

    Spring2.0学习笔记

    1、ICO环境的搭建 2、属性注入 3、自定义属性编辑器 4、AOP实现 5、事务处理 6、Spring事务配置的五种方式 详解 7、Struts与Spring集成方案

    Spring2.0开发参考手册

    在此基础上,Spring还提供了包括声明式事务管理,RMI或Web Services远程访问业务逻辑,以及可以多种方法进行的持久化数据库地解决方案。另外,Spring还有一个全功能的 MVC框架,并能透明的把 AOP 集成到你的软件中去...

    Spring2.0+Hibernate3.1的事务管理

    NULL 博文链接:https://japankn.iteye.com/blog/320445

Global site tag (gtag.js) - Google Analytics