Young Gyu Blog
메뉴

JPA에서 transaction 처리 방법

Young Gyu 2015-11-13
@ Transactional(rollbackFor = {RuntimeException.class})
public SomeService {
  @ Autowired SomeRepository repository1;
  @ Autowired AnotherRepository repository2;
  @ Autowired ThirdRepository repository3;
  ...
  @ Transactional(rollbackFor = {RuntimeException.class})
  public void SomeMethod(SomeEntity obj, String someNewValue) {
    try {
      repository1.updateMethod();
      repository2.updateMethod();
      obj.setValue(someNewValue);
      repository3.save(obj);
    } catch (Exception ex) {
      throw new RuntimeException();
    }
  }
}
class MyService2 {
  @Transactional(noRollbackFor = IllegalArgumentException.class)
  public void validateAddress(Address address) {
    // Some validations using dao
    if (!isValid) {
      throws new IllegalArgumentException();
    }
  }
}

Tags: jpa, transaction

Category: 13