简体   繁体   English

Spring数据JPA保存全部或不保存和异常回滚

[英]Spring data JPA save all or none and exception rollback

Nested call of JpaRepository.save(), the requirement is to save all or none. JpaRepository.save() 的嵌套调用,要求是全部保存或不保存。

DAOs are: DAO 是:

@Entity
@Table(name = "A")
public class A implements Serializable{
}

@Entity
@Table(name = "B")
public class B implements Serializable{
}

@Entity
@Table(name = "C")
public class C implements Serializable{
}

Repositories are :存储库是:

public interface ARepository extends JpaRepository<A, String> {
}

public interface BRepository extends JpaRepository<B, String> {
}

public interface CRepository extends JpaRepository<C, String> {
}

Let's say DAOService layer have methods as depicted below:假设 DAOService 层具有如下所示的方法:

persistA(){
   persistB(b)
   aRepository.save(a);
}

persistB(B b){
   bRepository.save(b);
}

persistC(C b){
   cRepository.save(c);
}

persistAll(){
    persistA(a);
    persistC(c);
}

Here the requirement is all the database objects( A, B, and C ) to be saved as part of same transaction only which should be new.这里的要求是将所有数据库对象( A、B 和 C )保存为同一事务的一部分,这些对象应该是新的。 In case of exception( checked exception, as runtime exception already rollback everything ) during the save of any of the object, nothing should be save.如果在保存任何对象期间出现异常(检查异常,因为运行时异常已经回滚所有内容),则不应保存任何内容。 Meaning either everything saved to DB or nothing is saved.这意味着要么保存到数据库的所有内容,要么不保存任何内容。

Per the information shared in the below links:根据以下链接中共享的信息:

  1. https://www.ibm.com/developerworks/library/j-ts1/index.html https://www.ibm.com/developerworks/library/j-ts1/index.html
  2. https://www.baeldung.com/spring-transactional-propagation-isolation https://www.baeldung.com/spring-transactional-propagation-isolation

I have implemented this the following way:我已经通过以下方式实现了这一点:

@Transactional(propagation  = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
persistAll(){

}

@Transactional(propagation  = Propagation.REQUIRED, rollbackFor = Exception.class)
persistA(){
}

@Transactional(propagation  = Propagation.REQUIRED, rollbackFor = Exception.class)
persistB(B b){
}

@Transactional(propagation  = Propagation.REQUIRED, rollbackFor = Exception.class)
persistC(C c){
}

Is there a way to check if the same transaction is being used for saving A, B, as well as C.有没有办法检查是否正在使用相同的事务来保存 A、B 和 C。

Also, I would like to know if the following code snippet另外,我想知道以下代码片段是否

@Transactional(propagation  = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
persistAll(){

}

@Transactional(propagation  = Propagation.REQUIRED, rollbackFor = Exception.class)
persistA(){
}

@Transactional(propagation  = Propagation.REQUIRED, rollbackFor = Exception.class)
persistB(B b){
}

@Transactional(propagation  = Propagation.REQUIRED, rollbackFor = Exception.class)
persistC(C c){
}

is equivalent to:相当于:

@Transactional(propagation  = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
persistAll(){

}


persistA(){
}

persistB(B b){
}


persistC(C c){
}

You can't get a transaction id or something similar.您无法获得交易 ID 或类似的信息。 But you could check if Spring Transaction started a new transaction:但是您可以检查 Spring Transaction 是否启动了一个新事务:

TransactionAspectSupport.currentTransactionStatus().isNewTransaction();

So in persistAll() this should return true and in all other methods false.所以在persistAll()这应该返回 true 而在所有其他方法中返回 false。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM