I am trying to clear my doubts w.r.t. Spring Transaction boundaries with following example.
@Transactional(propagation=Propagation.REQUIRES_NEW)
public void
test() should probably be SUPPORTS, so if there is an existing TX, it will be propagated to test1(). test1() should be REQUIRED so your insert actually commits. test2() should be REQUIRES_NEW.
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"));
}
}