`

springframework【13】

阅读更多

spring的事务管理

 

Spring提供了编程性事务管理与声明式的事务管理。Spring事务管理的抽象关键在于org.springframework.transaction.PlatformTransactionManager接口的实现。

PlatfromTransactionManager接口有许多具体的事务实现类,例如:DataSourceTransactionManager、HibernateTransactionManager、JdoTransactionManager,JtaTransactionManager等等。

  此处需要注意的是,要使用Mysql数据库进行事务处理,必须建立支持事务的表类型,例如:InnoDB的表类型。

1、  Spring提供的编程性事务

可以清楚的控制事务的边界,自行实现事务的开始时间,撤销时间,结束时间等,可以实现细粒度的事务控制。

编码例子如下:

 

private PlatformTransactionManager txManager;

    public void setTxManager(PlatformTransactionManager txManager) {

       this.txManager = txManager;

}

    public void insertUser(User user)  {

       DefaultTransactionDefinition def = new DefaultTransactionDefinition();

       def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);

       TransactionStatus status = txManager.getTransaction(def);

       try{

           JdbcTemplate tmp = new JdbcTemplate(dataSource);

           tmp.execute("insert  into tbl_user (name,pwd) values ('"+user.getName()+"','"+user.getPwd()+"')");

            //写错的sql,等着抛出运行时异常

           tmp.update("sssssssssssss");

           UserUpdate uu = new UserUpdate(dataSource);

            uu.update(new Object[]{user.getName(),user.getPwd()});

       }catch(DataAccessException e){

           txManager.rollback(status);

           throw e;

       }

           txManager.commit(status);

    }

 

配置文件的写法如下:

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

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

</bean>

<bean id="userDao" class="com.itcast.dao.impl.UserDaoImpl">

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

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

</bean>

 

2、  声明式的事务管理

Spring事务的相关API可以不用介入程序之中,只是在配置文件上修改配置下就可以移去事务管理服务。Spring的声明式事务是通过springAOP来实现的,所以执行程序的时候,请记得将spring-aop.jar给放到classpath中。

1)、最基础的事务配置如下:

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

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

</bean>

<bean id="userDao" class="com.itcast.dao.impl.UserDaoImpl">

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

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

</bean>

<!-- 事务代理 -->

<bean id="userDaoTxProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">

    <property name="target" ref="userDao"></property>

    <property name="transactionManager" ref="txManager"></property>

    <property name="transactionAttributes">

    <props>

    <prop key="insert*">PROPAGATION_REQUIRED</prop>

</props>

 

我们在使用的时候只是需要取得userDaoTxProxy 而不是userDao就可以完成对这个Dao的事务控制了。

2)、将事务抽象提取,让我们的代理类取得一个拦截器方式的事务,从而将每个代理类的事务都抽象出来集中管理,这样只要我们命名规范,那么我们的事务处理就更加容易。

例如:

分享到:
评论

相关推荐

    Getting.started.with.Spring.Framework.2nd.Edition1491011912.epub

    Getting started with Spring ...Chapter 13 – More Spring Web MVC – internationalization, file upload and asynchronous request processing Chapter 14 – Securing applications using Spring Security

    spring-framework-reference-4.1.2

    3. New Features and Enhancements in Spring Framework 4.0 ............................................ 17 3.1. Improved Getting Started Experience .........................................................

    Spring Framework 数据(均为英文本, 均较新, 2017~2018)

    文档中包括13本收集的 Spring Frameworks 相关的技术数据,均为英文版,包括Spring 4 和 Spring 5,内容涉及 Spring Core-Frameworks、 Spring-Boot、 Spring Micro-Services 等。书籍列表如下 Beginning Spring ...

    Learning.Spring.Application.Development.1783987367

    Discover the key Spring framework-related technology standards such as Spring core, Spring-AOP, Spring data access frameworks, and Spring testing to develop robust Java applications easily and rapidly...

    spring_MVC源码

    09. &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt; 10. &lt;/listener&gt; 11. 12. &lt;servlet&gt; 13. &lt;servlet-name&gt;spring&lt;/servlet-name&gt; 14. &lt;servlet-class&gt;org.spring...

    Spring攻略PDF版

     第13章 Spring Security框架   第14章 Spring Portlet MVC框架   第15章 Spring Web Flow   第16章 Spring远程服务和Web服务   第17章 Spring对EJB和JMS的支持   第18章 Spring对JMX、电子...

    spring-framework-reference4.1.4

    3. New Features and Enhancements in Spring Framework 4.0 ............................................ 17 3.1. Improved Getting Started Experience .........................................................

    Struts2+Spring3+MyBatis3完整实例

    - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1e7d503: defining beans [accountBizImpl,accountDaoImpl,baseMapperDaoImpl,org.springframework....

    spring aop 实现源代码--xml and annotation(带lib包)

    顾名思义,Before Advice会在目标对象的方法执行之前被调用,您可以通过实现org.springframework.aop.MethodBeforeAdvice接口来实现Before Advice的逻辑,接口定义如下: java 代码 1. package org.spring...

    Spring.MVC.A.Tutorial.2nd.Edition.1771970316

    This is a tutorial on Spring MVC, a module in the Spring Framework for rapidly developing web applications. The MVC in Spring MVC stands for Model-View-Controller, a design pattern widely used in ...

    Spring攻略中文版PDF

     第13章 Spring Security框架   第14章 Spring Portlet MVC框架   第15章 Spring Web Flow   第16章 Spring远程服务和Web服务   第17章 Spring对EJB和JMS的支持   第18章 Spring对JMX、电子...

    Spring攻略英文版(附带源码)

     第13章 Spring Security框架   第14章 Spring Portlet MVC框架   第15章 Spring Web Flow   第16章 Spring远程服务和Web服务   第17章 Spring对EJB和JMS的支持   第18章 Spring对JMX、电子邮件和...

    spring mvc标签使用指南

    Spring Framework开发参考: 1 13.9. 使用Spring的表单标签库 1 13.9.1. 配置 1 13.9.2. form标签 1 13.9.3. input标签 3 13.9.4. checkbox标签 3 13.9.5. checkboxes标签 6 13.9.6. radiobutton标签 6 13.9.7. ...

    spring-framework-5.3.2-dist.zip

    spring-framework-5.3.2-dist SHA-256:e88f15e4a3a18f70ac01ef67a7bbec4578a2ab0fc1f0d86c9c8f7ac8ece3375a (Uploaded: Identical) SHA-1:2246d8f8ac7b445b13d5d21b1b52a7cf68ae1c69 (Uploaded: Identical) MD5:a3ee...

    struts2驱动包

    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [applicationContext.xml]: Initialization of bean failed; nested ...

    Pro.Spring.Boot.1942270003

    Pro Spring Boot is your authoritative hands-on practical guide for increasing your Spring ...Chapter 13: Spring Boot in the Cloud Chapter 14: Extending Spring Boot Apps Appendix A: Spring Boot 1.4.x

    springboot-learn-jdbc.zip

    │ Maven__org_springframework_boot_spring_boot_test_autoconfigure_2_2_6_RELEASE.xml │ Maven__org_springframework_spring_aop_5_2_5_RELEASE.xml │ Maven__org_springframework_spring_beans_5_2_5_...

    Spring MVC 入门实例

    11 &lt;bean id="simpleUrlHandlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"&gt; 12 13 14 &lt;prop key="/hello.do"&gt;helloController 15 16 17 18 19 20 &lt;!-- 21 22 --&gt; ...

    webx3框架指南PDF教程附学习Demo

    第 3 章 Webx Framework ...................................................................................... 24 3.1. Webx的初始化 ....................................................................

Global site tag (gtag.js) - Google Analytics