zoukankan      html  css  js  c++  java
  • Spring事务传递

    2018-09-25

        @Transactional(propagation=Propagation.NEVER)
        public void update(){
             Session s = sessionFactory.getCurrentSession();
             Student stu = (Student)s.get(Student.class, 7);
             System.out.println("ok" + stu.getName());
        }
        
        @Transactional(propagation=Propagation.REQUIRED)
        public void hasTran(){
            update();
        }

    hasTran和update方法如果在一个bean方法里面,调用hasTran()的时候,并不报错。说明update并没有识别出它是never级别的事务传递,因为在同一个bean里面调用不识别

    如果把hasTran()放到另一个bean里面,调用hasTran()就会报错。

    Existing transaction found for transaction marked with propagation 'never'

    如果两个id重复了,有保护机制。

    @Service
    public class TestService2 {

      @Transactional(propagation=Propagation.REQUIRED)
      public void hasTran(){
        Session s = sessionFactory.getCurrentSession();
        Student stu = new Student();
        stu.setName("spring2018+++");
        s.update(stu);
        testService.update();
      }

    }

    @Service
    public class TestService {

      @Transactional(propagation=Propagation.REQUIRED)
      public void update(){
        Session s = sessionFactory.getCurrentSession();
        Student stu = new Student();
        stu.setId(7);
        stu.setName("spring2016");
        s.update(stu);
      }

    }

    public static void main(String[] args) {

      TestService2 testService2 = (TestService2) context.getBean("testService2");
      testService2.hasTran();

    }

    Exception in thread "main" org.hibernate.NonUniqueObjectException: A different object with the same identifier value was already associated with the session : [cn.angelshelter.spring_study.pojo.Student#7]

    还有在一个事务内,如果多次调用Student stu = (Student)s.get(Student.class, 7);方法,其实只是查询了一次,不会说执行一次查询一次。

    如果方法A是REQUIRED的,方法B也是REQUIRED的,方法A调用方法B,方法B会抛出运行时异常,但是在A方法中,会try{ B },最后的结果是

    Exception in thread "main" org.springframework.transaction.UnexpectedRollbackException: Transaction rolled back because it has been marked as rollback-only

    事务没有提交。

    如果在上面的基础上,把B改为REQUIRES_NEW,然后A和B不是同一个事务,A的事务是可以提交的。

    SUPPORTS最佛性,有事务就支持,没事务也通过。好像加了跟没加一个样。

    NOT_SUPPORTED。无事务运行,如果你遇到一个方法

     Exception in thread "main" org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current thread 
    那你加一个NOT_SUPPORTED它就不报错了。告诉程序,这个是没有事务也让它执行。
     
    MANDATORY 要求有事务,要不然抛出异常。
     
    刚发现REQUIRES_NEW有一定的危险性。有Studnet对象(id为7,张三),如果我在A方法中,把它的名字改为aa,然后调用B方法,B方法是REQUIRES_NEW的,这时候,如果把张三查询(按id查)出来,它的名字依然是张三,
    而不是方法中改变的aa。
     
    最后来一个总结吧:场景是A调用B,B抛出异常,A里面会捕获B的异常。
    REQUIRED  +  REQUIRED   他们是同一个事务,所以对象bean是互通的(就是A修改了Student, B如果查询,是查到A修改后的Student),然后B抛异常影响了A,A也提交不了。
     
    REQUIRED  +  NESTED      他们是同一个事务,所以对象bean是互通的,B抛异常不影响A(前提是要try), A最后可以提交事务
     
    REQUIRED  + REQUIRES_NEW  他们是不同事务,所以对象bean不是互通的,B抛异常不影响A(前提是要try), A最后可以提交事务
     
     
     
  • 相关阅读:
    JMeter压力测试并发测试(入门篇)
    System.currentTimeMillis()的性能问题以及解决方法
    Java日期时间API系列4-----Jdk7及以前的日期时间类的线程安全问题【转】
    使用Apache Commons Chain
    Spring中的InitializingBean接口的使用[转]
    Hash环/一致性Hash原理【转】
    ASP.NET Core 进程内模型和进程外模式
    如何运行多个同名Windows服务实例
    WPF系列 —— 控件添加依赖属性
    WPF系列——简单绑定学习
  • 原文地址:https://www.cnblogs.com/angelshelter/p/9699344.html
Copyright © 2011-2022 走看看