Spring事物回滚详解

news/2024/7/20 2:42:48 标签: spring, exception, aop, string, attributes, 详解, class
class="baidu_pl">
class="article_content clearfix">
class="htmledit_views"> 原文地址为: Spring事物回滚class="tags" href="/tags/XiangJie.html" title=详解>详解

class="htmledit_views"> (一) 用编程的方法来实现,我觉得这种方法比较灵活,控制起来比较方便,但是需要写一些额外的代码
<!--定义Bean-->
    <bean id="Test" class="com.test.Test">
        <property name="template" ref="jdbcTemplate" />
        <property name="transaction" ref="transactionTemplate" />
    </bean>
 
<!--事务模板 -->  
  <bean id="transactionTemplate" class="org.class="tags" href="/tags/SPRING.html" title=spring>springframework.transaction.support.TransactionTemplate">  
           <property name="transactionManager">  
                    <ref local="transactionManager"/>  
           </property>  
  </bean>  
  <!-- jdbc事务管理器 -->  
  <bean id="transactionManager" class="org.class="tags" href="/tags/SPRING.html" title=spring>springframework.jdbc.datasource.DataSourceTransactionManager">  
           <property name="dataSource">          
            <ref local="dataSource"/>  
   </property>


public class Test {
    private JdbcTemplate template;
    private TransactionTemplate transaction;
    private static final String TEST_SQL="insert into user(name,date) values(?,?)";
    public void setTemplate(JdbcTemplate template) {
        this.template = template;
    }

    public void setTransaction(TransactionTemplate transaction) {
        this.transaction = transaction;
    }
    public  void testSQL() throws Exception{
        for(int i=0;i<10;i++){
            if(i==8)throw new Exception("====error when updaa=======");//到第八条数据时我抛异常
            template.update(TEST_SQL,new Object[]{"xugang"+i,"123"});
        }
       
    }
    public static void main(String[] args) throws Exception{
        ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
        final Test t=(Test)ctx.getBean("Test");
        boolean f=t.transaction.execute(new TransactionCallback<Boolean>() {
            @Override
            public Boolean doInTransaction(TransactionStatus status) {
                    try {    
                        t.testSQL();//测试SQL
                        return Boolean.TRUE;//插入成功
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        status.setRollbackOnly();//回滚事物
                        e.printStackTrace();
                        return Boolean.FALSE;
                    }
            }
        });
}

(二)用声明的方法来实现事物的管理,在配置文件中加入下面
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:class="tags" href="/tags/ATTRIBUTES.html" title=attributes>attributes>
            <tx:method name="get*" read-only="true" /><!--以get开头的方法中的Connection是readonly的 -->
            <tx:method name="update*" rollback-for="Throwable"/><!-- 在update开头的方法中遇到异常(Throwable)就回滚-->
            <tx:method name="*" />
        </tx:class="tags" href="/tags/ATTRIBUTES.html" title=attributes>attributes>
    </tx:advice>
    <class="tags" href="/tags/AOP.html" title=aop>aop:config>
        <class="tags" href="/tags/AOP.html" title=aop>aop:pointcut expression="execution(* com.test.Test2.*(..))"<!-- 匹配com.test.Test2这个类下面的所有方法-->
            id="service" />
        <class="tags" href="/tags/AOP.html" title=aop>aop:advisor advice-ref="txAdvice" pointcut-ref="service" />
    </class="tags" href="/tags/AOP.html" title=aop>aop:config>

/**
 * 测试事物回滚
 * @author 许刚
 */
public class Test2 {
    private JdbcTemplate template;
    private TransactionTemplate transaction;
    private static final String TEST_SQL="insert into user(name,date) values(?,?)";
    public void setTemplate(JdbcTemplate template) {
        this.template = template;
    }

    public void setTransaction(TransactionTemplate transaction) {
        this.transaction = transaction;
    }
    public  void testSQL() throws Exception{
        for(int i=0;i<10;i++){
            if(i==8)throw new Exception("====error when updaa=======");
            template.update(TEST_SQL,new Object[]{"xugang"+i,"123"});
        }
        
    }
    
    public void getData(){
        template.update(TEST_SQL,new Object[]{"xugang","123"});
    }
    public void updateData() throws Exception{
        testSQL();
    }    
    public static void main(String[] args) throws Exception{
        ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
        final Test2 t=(Test2)ctx.getBean("Test2");
        //t.getData();//get开头的方法是readonly的就会抛下面的异常
        //  java.sql.SQLException: Connection is read-only. Queries leading to data modification are not allowed
        t.updateData();//执行玩了以后,数据库里面一条数据都没有的,在第8条的时候回滚了
    }

}

转载注明出处

http://blog.csdn.net/xugangjava



转载请注明本文地址: Spring事物回滚class="tags" href="/tags/XiangJie.html" title=详解>详解

http://www.niftyadmin.cn/n/1167306.html

相关文章

嵌入式系统安全简介

概述&#xff1a;本文主要介绍嵌入式安全相关的概念&#xff0c;基于ARM TrustZone技术进行说明&#xff0c;以手机应用为主要场景进行讲解 首先问一个简单的问题&#xff1a;我们使用的手机密码/指纹/人脸数据是存放在哪里&#xff1f; 对于现在各种智能产品了解的人都知道一个…

ArcGIS中标注(label)的使用技巧

原文地址为&#xff1a; ArcGIS中标注(label)的使用技巧标注是显示在地图上的文字信息&#xff0c;它是出图中不可或缺的重要元素。标注的样式丰富&#xff0c;并且放置位置灵活&#xff0c;因此带来了对标注控制的难度。例如地质图里的上下标&#xff0c;或是一些分式的标注&a…

python值的引用传递和go语言的值传递

一&#xff1a;值传递 实参a 原本指向地址 1638212&#xff0c;代表1638212这个地址的值是3。在swap函数中&#xff0c;实参a将值拷贝给形参a&#xff0c;形参a此时也在内存中拥有地址&#xff0c;地址 xxxx&#xff0c;值为3&#xff0c;在所有的函数体内的操作&#xff0c;都…

Android动画及图片的缩放和旋转

原文地址为&#xff1a; Android动画及图片的缩放和旋转Android动画有2种&#xff0c;一种是Tween Animation,另一种是Frame Animation&#xff0c;先说说Tween动画吧。 Tween动画是对视图对象中的内容进行一系列简单的转换&#xff0c;比如位置的移动&#xff0c;大小的缩放&a…

Spring-Boot - 初步搭建

原文地址为&#xff1a; Spring-Boot - 初步搭建official document&#xff1a;http://projects.spring.io/spring-boot/ 项目代码&#xff1a; https://github.com/Ryan-Miao/springboot-test 一、简介 SpringMVC是非常伟大的框架&#xff0c;开源&#xff0c;发展迅速。优…

WPF中的文字

控件 Textbox &#xff08;可读可写&#xff09;&&TextBlock(只读) TextBlock属性&#xff1a; 文本框颜色&#xff1a;background (brush)文本框的位置&#xff1a;Canvas.Top Canvas.Left (double )文字颜色&#xff1a; foreground(brush)字体&#xff1a;fontfamil…

一分钟学会 log4net(c#) 配置及使用

原文地址为&#xff1a; 一分钟学会 log4net&#xff08;c#&#xff09; 配置及使用初次由java转做c#项目&#xff0c;研究了一下log4net的使用。 1. 首先从apache网站下载log4net&#xff0c; http://logging.apache.org/log4net/download_log4net.cgi 。我下的是最新版本 lo…

(3)UNIX/Linux系统结构

UNIX/Linux 系统可以粗糙地抽象为 3 个层次&#xff0c;如图所示。底层是 UNIX/Linux 操作系统&#xff0c;即系统内核&#xff08;Kernel&#xff09;&#xff1b;中间层是Shell层&#xff0c;即命令解释层&#xff1b;高层则是应用层。 1) 内核层 内核层是 UNIX/Linux 系统的…