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"));
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…