回滚不适用于多次插入

时间:2019-12-18 13:38:38

标签: spring-boot jpa spring-transactions

在Application类内部的方法上的

@Transactional注释不会回滚插入。但是,服务类(EmpService.java)方法(“ insertEmp(Emp emp)”)上的@Transactional注释按预期工作。

有人可以让我知道为什么@Transactional工作不同吗? Spring Boot版本-具有h2数据库的2.1.3.RELEASE。 请让我知道是否需要其他信息。

@SpringBootApplication
@ComponentScan("org.saheb")
@EnableJpaRepositories("org.saheb.repo")
@EntityScan("org.saheb.vo")
@EnableTransactionManagement
public class SpringJpaTransactionApplication implements CommandLineRunner {
@Autowired
private EmpService empService;

public static void main(String[] args) {
    SpringApplication.run(SpringJpaTransactionApplication.class, args);
}

@Override
public void run(String... args) throws Exception {
    insertSingleBatch();
}
@Transactional(rollbackFor=RuntimeException.class, propagation=Propagation.REQUIRES_NEW)
public void insertSingleBatch() {
    try {
        Set<Emp> empSet = new LinkedHashSet<>();
        Dept dept = new Dept();
        dept.setDeptNo(10);
        empSet.add(new Emp("abc", "abc", dept));
        empSet.add(new Emp("xyz", "xyz", dept));
        empSet.add(new Emp("def", "def", dept));
        empSet.add(new Emp("pqrstu", "pqr", dept));// This will fail as max character allowed in 5 and should rollback all the insertion. But, first three records are getting saved in h2 database.
        empService.insertEmp(empSet);
    } catch (RuntimeException e) {
        System.out.println("Exception in batch1.." + e.getMessage());
    }
}
}

@Service
public class EmpService {
@Autowired
private EmpRepository empRepository;

//@Transactional(rollbackFor=RuntimeException.class, propagation=Propagation.REQUIRES_NEW)//This is working as expected as all the insertions are rolling back after failure of 4th insertion
public void  insertEmp(Set<Emp> empSet) {
    System.out.println("Inside insert");
    for (Emp temp : empSet) {
        Emp temp2 =empRepository.save(temp);
        System.out.println("inserted-->"+temp2.getFirstName());
    }
}
}

1 个答案:

答案 0 :(得分:0)

您是从同一bean上@Transactional进行“自我调用”的方法,该方法无效。此行为在herehere的文档中得到了很好的解释(搜索关键字“自我调用”)

您可以简单地将@Transactional方法移动到另一个bean,然后将该bean注入其客户bean并调用此@Transactional方法。

或使用TransactionTemplate在事务中执行它:


   @Autowired
   private TransactionTemplate txTemplate;

   @Override
   public void run(String... args) throws Exception {
            txTemplate.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
            txTemplate.execute(status->{
                insertSingleBatch();
                return null;
            });

   }

请注意,TransactionTemplate将忽略@Transactional上的设置,您必须以编程方式对其进行配置。