在学习Spring事务这节的时候,遇到这样一个问题:调用同类方法事务不生效!具体问题描述如下:
测试的主要代码——
public interface FooService {
void insertThenRollback() throws RollBackException;
void invokeInsertThenRollback() throws RollBackException;
}
@Component
public class FooServiceImpl implements FooService {
@Autowired
private JdbcTemplate jdbcTemplate;
@Transactional(rollbackFor = RollBackException.class)
public void insertThenRollback() throws RollBackException {
jdbcTemplate.execute("INSERT INTO FOO (BAR) VALUES ('BBB')");
throw new RollBackException();
}
// 注意! 这个方法没有申明事务!
public void invokeInsertThenRollback() throws RollBackException {
this.insertThenRollback();
}
}
@SpringBootApplication
@EnableTransactionManagement(mode = AdviceMode.PROXY)
@Slf4j
public class XiaoLearnSpringApplication implements CommandLineRunner {
@Autowired
private FooService fooService;
@Autowired
private JdbcTemplate jdbcTemplate;
public