Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
216 views
in Technique[技术] by (71.8m points)

java - Understanding Spring Transaction boundaries

I am trying to clear my doubts w.r.t. Spring Transaction boundaries with following example.

@Transactional(propagation=Propagation.REQUIRES_NEW)
public void test() {    
    test1();
    test2();        
}

@Transactional(propagation=Propagation.NOT_SUPPORTED, readOnly=false)
public void test1() {
    this.jdbcTemplate.execute("INSERT INTO TEST VALUES('T', 'C2', 0, 1)");      
}

@Transactional(propagation=Propagation.SUPPORTS, isolation=Isolation.READ_UNCOMMITTED, readOnly=true)
public void test2() {
    System.out.println(this.jdbcTemplate.queryForInt("select count(*) from TEST"));         
}

I want to isolate test2() method from test1() that is each time test() is called test2() should not read the data committed by test1(). Please advice is it possible to handle this scenario using propagation or isolation attributes.

Thanks in advance.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Transaction attributes are applied to external calls , not internal calls made by bean method such as your case. If you want transaction boundaries applied to your call, you should inject your bean instance such as below. But I think that it is not good practice, I would not recommend.. The right way to accomplish is to define another spring bean and associate it with your previous bean and put your test method to this new bean.

@Service("yourBean")
@Transactional
public class YourBeanClass implement IYourBean {

   @Resource(name="yourBean")
    IYourBean yourBean;

        @Transactional(propagation=Propagation.REQUIRED)
    public void test() {    
        yourBean.test1();
        yourBean.test2();       
    }

    @Transactional(propagation=Propagation.REQUIRED)
    public void test1() {
        this.jdbcTemplate.execute("INSERT INTO TEST VALUES('T', 'C2', 0, 1)");      
    }

    @Transactional(propagation=Propagation.NOT_SUPPORTED)
    public void test2() {
        System.out.println(this.jdbcTemplate.queryForInt("select count(*) from TEST"));         
    }

}

Alternative and better way to solve this particular problem ;

 @Service("otherBean")
    @Transactional
    public class OtherBeanClass implement IOtherBean {

     @Autowired
     IYourBean yourBean;

      @Transactional(propagation=Propagation.REQUIRED)
        public void test() {    
            yourBean.test1();
            yourBean.test2();       
        }

   }



    @Service("yourBean")
    @Transactional
    public class YourBeanClass implement IYourBean {



        @Transactional(propagation=Propagation.REQUIRED)
        public void test1() {
            this.jdbcTemplate.execute("INSERT INTO TEST VALUES('T', 'C2', 0, 1)");      
        }

        @Transactional(propagation=Propagation.NOT_SUPPORTED)
        public void test2() {
            System.out.println(this.jdbcTemplate.queryForInt("select count(*) from TEST"));         
        }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...