{"id":326342,"date":"2021-07-12T21:00:30","date_gmt":"2021-07-12T21:00:30","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=326342"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=326342","title":{"rendered":"Spring Data: \u043d\u044e\u0430\u043d\u0441\u044b @Transactional"},"content":{"rendered":"\n<div class=\"post__text post__text_v2\" id=\"post-content-body\">\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/1c6\/7ba\/b9f\/1c67bab9f571f810e1f3ebdb9f7749ed.png\" width=\"650\" height=\"390\"><figcaption><\/figcaption><\/figure>\n<h2>Rollback \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e<\/h2>\n<p>\u041f\u0440\u0435\u0434\u043f\u043e\u043b\u043e\u0436\u0438\u043c, \u0447\u0442\u043e \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u0441\u0435\u0440\u0432\u0438\u0441, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0441\u043e\u0437\u0434\u0430\u0435\u0442 \u0442\u0440\u0435\u0445 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439 \u0432 \u0440\u0430\u043c\u043a\u0430\u0445 \u043e\u0434\u043d\u043e\u0439 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438. \u0415\u0441\u043b\u0438 \u0447\u0442\u043e-\u0442\u043e \u0438\u0434\u0435\u0442 \u043d\u0435 \u0442\u0430\u043a, \u0432\u044b\u0431\u0440\u0430\u0441\u044b\u0432\u0430\u0435\u0442\u0441\u044f <code>java.lang.Exception<\/code>.<\/p>\n<pre><code class=\"java\">@Service public class PersonService {   @Autowired   private PersonRepository personRepository;    @Transactional   public void addPeople(String name) throws Exception {     personRepository.saveAndFlush(new Person(\"Jack\", \"Brown\"));     personRepository.saveAndFlush(new Person(\"Julia\", \"Green\"));     if (name == null) {       throw new Exception(\"name cannot be null\");     }     personRepository.saveAndFlush(new Person(name, \"Purple\"));   } }<\/code><\/pre>\n<p>\u0410 \u0432\u043e\u0442 \u043f\u0440\u043e\u0441\u0442\u043e\u0439 unit-\u0442\u0435\u0441\u0442.<\/p>\n<pre><code class=\"java\">@SpringBootTest @AutoConfigureTestDatabase class PersonServiceTest {   @Autowired   private PersonService personService;   @Autowired   private PersonRepository personRepository;    @BeforeEach   void beforeEach() {     personRepository.deleteAll();   }    @Test   void shouldRollbackTransactionIfNameIsNull() {     assertThrows(Exception.class, () -&gt; personService.addPeople(null));     assertEquals(0, personRepository.count());   } }<\/code><\/pre>\n<p>\u041a\u0430\u043a \u0434\u0443\u043c\u0430\u0435\u0442\u0435, \u0442\u0435\u0441\u0442 \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u0441\u044f \u0443\u0441\u043f\u0435\u0448\u043d\u043e, \u0438\u043b\u0438 \u043d\u0435\u0442? \u041b\u043e\u0433\u0438\u043a\u0430 \u0433\u043e\u0432\u043e\u0440\u0438\u0442 \u043d\u0430\u043c, \u0447\u0442\u043e Spring \u0434\u043e\u043b\u0436\u0435\u043d \u043e\u0442\u043a\u0430\u0442\u0438\u0442\u044c \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u044e \u0438\u0437-\u0437\u0430 \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f. \u0421\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e <code>personRepository.count()<\/code> \u0434\u043e\u043b\u0436\u0435\u043d \u0432\u0435\u0440\u043d\u0443\u0442\u044c <code>0<\/code>, \u0442\u0430\u043a \u0432\u0435\u0434\u044c? \u041d\u0435 \u0441\u043e\u0432\u0441\u0435\u043c.<\/p>\n<pre><code>expected: &lt;0&gt; but was: &lt;2&gt; Expected :0 Actual   :2<\/code><\/pre>\n<p>\u0417\u0434\u0435\u0441\u044c \u0442\u0440\u0435\u0431\u0443\u044e\u0442\u0441\u044f \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043e\u0431\u044a\u044f\u0441\u043d\u0435\u043d\u0438\u044f. \u041f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e Spring \u043e\u0442\u043a\u0430\u0442\u044b\u0432\u0430\u0435\u0442 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438 \u0442\u043e\u043b\u044c\u043a\u043e \u0432 \u0441\u043b\u0443\u0447\u0430\u0435 <strong>\u043d\u0435\u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c\u043e\u0433\u043e <\/strong>\u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f. <strong>\u041f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c\u044b\u0435<\/strong> \u0436\u0435 \u0441\u0447\u0438\u0442\u0430\u044e\u0442\u0441\u044f \u00ab\u0432\u043e\u0441\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u043c\u044b\u043c\u0438\u00bb \u0438\u0437-\u0437\u0430 \u0447\u0435\u0433\u043e Spring \u0432\u043c\u0435\u0441\u0442\u043e <code>rollback<\/code> \u0434\u0435\u043b\u0430\u0435\u0442 <code>commit<\/code>. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 <code>personRepository.count()<\/code> \u0432\u043e\u0437\u0440\u0430\u0449\u0430\u0435\u0442 <code>2<\/code>.<\/p>\n<p>\u0421\u0430\u043c\u044b\u0439 \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u0438\u0441\u043f\u0440\u0430\u0432\u0438\u0442\u044c \u044d\u0442\u043e \u2014 \u0437\u0430\u043c\u0435\u043d\u0438\u0442\u044c <code>Exception<\/code> \u043d\u0430 \u043d\u0435\u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c\u043e\u0435 \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, <code>NullPointerException<\/code>. \u041b\u0438\u0431\u043e \u043c\u043e\u0436\u043d\u043e \u043f\u0435\u0440\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u0430\u0442\u0440\u0438\u0431\u0443\u0442 <code>rollbackFor<\/code> \u0443 \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u0438.<\/p>\n<p>\u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043e\u0431\u0430 \u044d\u0442\u0438\u0445 \u043c\u0435\u0442\u043e\u0434\u0430 \u043a\u043e\u0440\u0440\u0435\u043a\u0442\u043d\u043e \u043e\u0442\u043a\u0430\u0442\u044b\u0432\u0430\u044e\u0442 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u044e.<\/p>\n<pre><code class=\"java\">@Service public class PersonService {   @Autowired   private PersonRepository personRepository;    @Transactional(rollbackFor = Exception.class)   public void addPeopleWithCheckedException(String name) throws Exception {     addPeople(name, Exception::new);   }    @Transactional   public void addPeopleWithNullPointerException(String name) {     addPeople(name, NullPointerException::new);   }    private &lt;T extends Exception&gt; void addPeople(String name, Supplier&lt;? extends T&gt; exceptionSupplier) throws T {     personRepository.saveAndFlush(new Person(\"Jack\", \"Brown\"));     personRepository.saveAndFlush(new Person(\"Julia\", \"Green\"));     if (name == null) {       throw exceptionSupplier.get();     }     personRepository.saveAndFlush(new Person(name, \"Purple\"));   } }<\/code><\/pre>\n<pre><code class=\"java\">@SpringBootTest @AutoConfigureTestDatabase class PersonServiceTest {   @Autowired   private PersonService personService;   @Autowired   private PersonRepository personRepository;    @BeforeEach   void beforeEach() {     personRepository.deleteAll();   }    @Test   void testThrowsExceptionAndRollback() {     assertThrows(Exception.class, () -&gt; personService.addPeopleWithCheckedException(null));     assertEquals(0, personRepository.count());   }    @Test   void testThrowsNullPointerExceptionAndRollback() {     assertThrows(NullPointerException.class, () -&gt; personService.addPeopleWithNullPointerException(null));     assertEquals(0, personRepository.count());   }  }<\/code><\/pre>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/68a\/ac4\/9bf\/68aac49bfccfb518142d213e89c22d14.png\" width=\"493\" height=\"103\"><figcaption><\/figcaption><\/figure>\n<h2>Rollback \u043f\u0440\u0438 \u00ab\u0433\u043b\u0443\u0448\u0435\u043d\u0438\u0438\u00bb \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f<\/h2>\n<p>\u041d\u0435 \u0432\u0441\u0435 \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f \u0434\u043e\u043b\u0436\u043d\u044b \u0431\u044b\u0442\u044c \u043f\u0440\u043e\u0431\u0440\u043e\u0448\u0435\u043d\u044b \u0432\u0432\u0435\u0440\u0445 \u043f\u043e \u0441\u0442\u0435\u043a\u0443 \u0432\u044b\u0437\u043e\u0432\u043e\u0432. \u0418\u043d\u043e\u0433\u0434\u0430 \u0432\u043f\u043e\u043b\u043d\u0435 \u0434\u043e\u043f\u0443\u0441\u0442\u0438\u043c\u043e \u043e\u0442\u043b\u043e\u0432\u0438\u0442\u044c \u0435\u0433\u043e \u0432\u043d\u0443\u0442\u0440\u0438 \u043c\u0435\u0442\u043e\u0434\u0430 \u0438 \u0437\u0430\u043b\u043e\u0433\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u043e\u0431 \u044d\u0442\u043e\u043c.<\/p>\n<p>\u041f\u0440\u0435\u0434\u043f\u043e\u043b\u043e\u0436\u0438\u043c, \u0447\u0442\u043e \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u0435\u0449\u0435 \u043e\u0434\u0438\u043d <strong>\u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u043e\u043d\u043d\u044b\u0439<\/strong> \u0441\u0435\u0440\u0432\u0438\u0441, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u0442, \u043c\u043e\u0436\u0435\u0442 \u043b\u0438 \u0431\u044b\u0442\u044c \u0441\u043e\u0437\u0434\u0430\u043d \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u0441 \u043f\u0435\u0440\u0435\u0434\u0430\u043d\u043d\u044b\u043c \u0438\u043c\u0435\u043d\u0435\u043c. \u0415\u0441\u043b\u0438 \u043d\u0435\u0442, \u0432\u044b\u0431\u0440\u0430\u0441\u044b\u0432\u0430\u0435\u0442\u0441\u044f <code>IllegalArgumentException<\/code>.<\/p>\n<pre><code class=\"java\">@Service public class PersonValidateService {   @Autowired   private PersonRepository personRepository;    @Transactional   public void validateName(String name) {     if (name == null || name.isBlank() || personRepository.existsByFirstName(name)) {       throw new IllegalArgumentException(\"name is forbidden\");     }   } }<\/code><\/pre>\n<p>\u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u044e \u0432 \u043d\u0430\u0448 <code>PersonService<\/code>.<\/p>\n<pre><code class=\"java\">@Service @Slf4j public class PersonService {   @Autowired   private PersonRepository personRepository;   @Autowired   private PersonValidateService personValidateService;    @Transactional   public void addPeople(String name) {     personRepository.saveAndFlush(new Person(\"Jack\", \"Brown\"));     personRepository.saveAndFlush(new Person(\"Julia\", \"Green\"));     String resultName = name;     try {       personValidateService.validateName(name);     }     catch (IllegalArgumentException e) {       log.error(\"name is not allowed. Using default one\");       resultName = \"DefaultName\";     }     personRepository.saveAndFlush(new Person(resultName, \"Purple\"));   } }<\/code><\/pre>\n<p>\u0415\u0441\u043b\u0438 \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u044f \u043d\u0435 \u043f\u0440\u043e\u0445\u043e\u0434\u0438\u0442, \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0441 \u0438\u043c\u0435\u043d\u0435\u043c \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e.<\/p>\n<p>\u041e\u043a\u0435\u0439, \u0442\u0435\u043f\u0435\u0440\u044c \u043d\u0443\u0436\u043d\u043e \u043f\u0440\u043e\u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043d\u043e\u0432\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u044c.<\/p>\n<pre><code class=\"java\">@SpringBootTest @AutoConfigureTestDatabase class PersonServiceTest {   @Autowired   private PersonService personService;   @Autowired   private PersonRepository personRepository;    @BeforeEach   void beforeEach() {     personRepository.deleteAll();   }    @Test   void shouldCreatePersonWithDefaultName() {     assertDoesNotThrow(() -&gt; personService.addPeople(null));     Optional&lt;Person&gt; defaultPerson = personRepository.findByFirstName(\"DefaultName\");     assertTrue(defaultPerson.isPresent());   } }<\/code><\/pre>\n<p>\u041e\u0434\u043d\u0430\u043a\u043e \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u043d\u0435\u043e\u0436\u0438\u0434\u0430\u043d\u043d\u044b\u043c.<\/p>\n<pre><code>Unexpected exception thrown: org.springframework.transaction.UnexpectedRollbackException: Transaction silently rolled back because it has been marked as rollback-only<\/code><\/pre>\n<p>\u0421\u0442\u0440\u0430\u043d\u043d\u043e. \u041c\u044b \u043e\u0442\u043b\u043e\u0432\u0438\u043b\u0438 \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435. \u041f\u043e\u0447\u0435\u043c\u0443 \u0436\u0435 Spring \u043e\u0442\u043a\u0430\u0442\u0438\u043b \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u044e? \u041f\u0440\u0435\u0436\u0434\u0435 \u0432\u0441\u0435\u0433\u043e \u043d\u0443\u0436\u043d\u043e \u0440\u0430\u0437\u043e\u0431\u0440\u0430\u0442\u044c\u0441\u044f \u0441 \u0442\u0435\u043c, \u043a\u0430\u043a Spring \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0441 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u044f\u043c\u0438.<\/p>\n<p>\u041f\u043e\u0434 \u043a\u0430\u043f\u043e\u0442\u043e\u043c Spring \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0435\u0442 \u043f\u0430\u0442\u0442\u0435\u0440\u043d <a href=\"https:\/\/www.baeldung.com\/spring-aop\" rel=\"noopener noreferrer nofollow\">\u0430\u0441\u043f\u0435\u043a\u0442\u043d\u043e-\u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f<\/a>. \u041e\u043f\u0443\u0441\u043a\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u044b\u0435 \u0434\u0435\u0442\u0430\u043b\u0438, \u0438\u0434\u0435\u044f \u0437\u0430\u043a\u043b\u044e\u0447\u0430\u0435\u0442\u0441\u044f \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e bean \u043e\u0431\u043e\u0440\u0430\u0447\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u0432 \u043f\u0440\u043e\u043a\u0441\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u044e\u0442\u0441\u044f \u0432 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0435 \u0441\u0442\u0430\u0440\u0442\u0430 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f. \u0412\u043d\u0443\u0442\u0440\u0438 \u044d\u0442\u043e\u0433\u043e \u043f\u0440\u043e\u043a\u0441\u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u0442\u0440\u0435\u0431\u0443\u0435\u043c\u0430\u044f \u043b\u043e\u0433\u0438\u043a\u0430. \u0412 \u043d\u0430\u0448\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435, \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u044f\u043c\u0438. \u041a\u043e\u0433\u0434\u0430 \u043a\u0430\u043a\u043e\u0439-\u043d\u0438\u0431\u0443\u0434\u044c bean \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u043e\u043d\u043d\u044b\u0439 \u0441\u0435\u0440\u0432\u0438\u0441 \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 DI \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438, Spring \u043d\u0430 \u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435 \u0432\u043d\u0435\u0434\u0440\u044f\u0435\u0442 \u043f\u0440\u043e\u043a\u0441\u0438.<\/p>\n<p>\u041d\u0438\u0436\u0435 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d workflow \u0432\u044b\u0437\u043e\u0432\u0430 \u0432\u044b\u0448\u0435\u043d\u0430\u043f\u0438\u0441\u0430\u043d\u043d\u043e\u0433\u043e \u043c\u0435\u0442\u043e\u0434\u0430 <code>addPeople<\/code>.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/d63\/e94\/56f\/d63e9456f25696dee4d52ac61934b718.png\" width=\"2000\" height=\"1688\"><figcaption><\/figcaption><\/figure>\n<p>\u041f\u0430\u0440\u0430\u043c\u0435\u0442\u0440 <code>propagation<\/code> \u0443 <code><a class=\"mention\" href=\"\/users\/transactional\">@Transactional<\/a><\/code> \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e \u0438\u043c\u0435\u0435\u0442 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 <code>REQUIRED<\/code>. \u042d\u0442\u043e \u0437\u043d\u0430\u0447\u0438\u0442, \u0447\u0442\u043e \u043d\u043e\u0432\u0430\u044f \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u044f \u0441\u043e\u0437\u0434\u0430\u0435\u0442\u0441\u044f, \u0435\u0441\u043b\u0438 \u043e\u043d\u0430 \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0443\u0435\u0442. \u0418\u043d\u0430\u0447\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u0435\u0442\u0441\u044f \u0432 \u0442\u0435\u043a\u0443\u0449\u0435\u0439. \u0422\u0430\u043a \u0447\u0442\u043e \u0432 \u043d\u0430\u0448\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0432\u0435\u0441\u044c \u0437\u0430\u043f\u0440\u043e\u0441 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u0432 \u0440\u0430\u043c\u043a\u0430\u0445 \u0435\u0434\u0438\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0439 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438.<\/p>\n<p>\u041e\u0434\u043d\u0430\u043a\u043e \u0437\u0434\u0435\u0441\u044c \u0435\u0441\u0442\u044c \u043d\u044e\u0430\u043d\u0441. \u0415\u0441\u043b\u0438 <code>RuntimeException<\/code> \u0431\u044b\u043b \u0432\u044b\u0431\u0440\u043e\u0448\u0435\u043d \u0438\u0437-\u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446 <em>transactional proxy<\/em>, \u0442\u043e Spring \u043e\u0442\u043c\u0435\u0447\u0430\u0435\u0442 \u0442\u0435\u043a\u0443\u0449\u0443\u044e \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u044e \u043a\u0430\u043a <strong>rollback-only<\/strong>. \u0417\u0434\u0435\u0441\u044c \u0443 \u043d\u0430\u0441 \u0438\u043c\u0435\u043d\u043d\u043e \u0442\u0430\u043a\u043e\u0439 \u0441\u043b\u0443\u0447\u0430\u0439. <code>PersonValidateService.validateName<\/code> \u0432\u044b\u0431\u0440\u043e\u0441\u0438\u043b <code>IllegalArgumentException<\/code>. Transactional proxy \u0432\u044b\u0441\u0442\u0430\u0432\u0438\u043b \u0444\u043b\u0430\u0433 <code>rollback<\/code>. \u0414\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0438\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0443\u0436\u0435 \u043d\u0435 \u0438\u043c\u0435\u044e\u0442 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f, \u0442\u0430\u043a \u043a\u0430\u043a \u0432 \u043a\u043e\u043d\u0435\u0447\u043d\u043e\u043c \u0438\u0442\u043e\u0433\u0435 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u044f \u043d\u0435 \u0437\u0430\u043a\u043e\u043c\u043c\u0438\u0442\u0438\u0442\u0441\u044f.<\/p>\n<p>\u041a\u0430\u043a\u043e\u0432\u043e \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b? \u0412\u043e\u043e\u0431\u0449\u0435 \u0438\u0445 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0430\u0442\u0440\u0438\u0431\u0443\u0442 <code>noRollbackFor<\/code> \u0432 <code>PersonValidateService<\/code>.<\/p>\n<pre><code class=\"java\">@Service public class PersonValidateService {   @Autowired   private PersonRepository personRepository;    @Transactional(noRollbackFor = IllegalArgumentException.class)   public void validateName(String name) {     if (name == null || name.isBlank() || personRepository.existsByFirstName(name)) {       throw new IllegalArgumentException(\"name is forbidden\");     }   } }<\/code><\/pre>\n<p>\u0415\u0441\u0442\u044c \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u043f\u043e\u043c\u0435\u043d\u044f\u0442\u044c <code>propagation<\/code> \u043d\u0430 <code>REQUIRES_NEW<\/code>. \u0412 \u044d\u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 <code>PersonValidateService.validateName<\/code> \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d \u0432 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e\u0439 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438. \u0422\u0430\u043a \u0447\u0442\u043e \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u0430\u044f \u043d\u0435 \u0431\u0443\u0434\u0435\u0442 \u043e\u0442\u043c\u0435\u043d\u0435\u043d\u0430.<\/p>\n<pre><code class=\"java\">@Service public class PersonValidateService {   @Autowired   private PersonRepository personRepository;    @Transactional(propagation = Propagation.REQUIRES_NEW)   public void validateName(String name) {     if (name == null || name.isBlank() || personRepository.existsByFirstName(name)) {       throw new IllegalArgumentException(\"name is forbidden\");     }   } }<\/code><\/pre>\n<h2>\u0412\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u0435 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b \u0441 Kotlin<\/h2>\n<p>\u0423 Kotlin \u043c\u043d\u043e\u0433\u043e \u0441\u0445\u043e\u0436\u0435\u0441\u0442\u0435\u0439 \u0441 Java. \u041d\u043e \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f\u043c\u0438 \u043d\u0435 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043e\u0434\u043d\u043e\u0439 \u0438\u0437 \u043d\u0438\u0445.<\/p>\n<p>Kotlin \u0443\u0431\u0440\u0430\u043b \u043f\u043e\u043d\u044f\u0442\u0438\u044f <em>\u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c\u044b\u0445<\/em> \u0438 <em>\u043d\u0435\u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c\u044b\u0445<\/em> \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0439. \u0421\u0442\u0440\u043e\u0433\u043e \u0433\u043e\u0432\u043e\u0440\u044f, \u0432\u0441\u0435 \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f \u0432 Kotlin <em>\u043d\u0435\u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c\u044b\u0435<\/em>, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u043d\u0430\u043c \u043d\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0442\u044c \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u0438 <code>throws SomeException<\/code> \u0432 \u0441\u0438\u0433\u043d\u0430\u0442\u0443\u0440\u0430\u0445 \u043c\u0435\u0442\u043e\u0434\u043e\u0432, \u0430 \u0442\u0430\u043a\u0436\u0435 \u043e\u0431\u043e\u0440\u0430\u0447\u0438\u0432\u0430\u0442\u044c \u0438\u0445 \u0432\u044b\u0437\u043e\u0432\u044b \u0432 <code>try-catch<\/code>. \u041e\u0431\u0441\u0443\u0436\u0434\u0435\u043d\u0438\u0435 \u043f\u043b\u044e\u0441\u043e\u0432 \u0438 \u043c\u0438\u043d\u0443\u0441\u043e\u0432 \u0442\u0430\u043a\u043e\u0433\u043e \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u2014 \u0442\u0435\u043c\u0430 \u0434\u043b\u044f \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0438. \u041d\u043e \u0441\u0435\u0439\u0447\u0430\u0441 \u044f \u0445\u043e\u0447\u0443 \u043f\u0440\u043e\u0434\u0435\u043c\u043e\u043d\u0441\u0442\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0432\u0430\u043c \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u043e\u0433\u0443\u0442 \u0438\u0437-\u0437\u0430 \u044d\u0442\u043e\u0433\u043e \u0432\u043e\u0437\u043d\u0438\u043a\u043d\u0443\u0442\u044c \u043f\u0440\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 Spring Data.<\/p>\n<p>\u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u0435\u0440\u0435\u043f\u0438\u0448\u0435\u043c \u0441\u0430\u043c\u044b\u0439 \u043f\u0435\u0440\u0432\u044b\u0439 \u043f\u0440\u0438\u043c\u0435\u0440 \u0441 <code>java.lang.Exception<\/code> \u043d\u0430 Kotlin.<\/p>\n<pre><code class=\"kotlin\">@Service class PersonService(     @Autowired     private val personRepository: PersonRepository ) {     @Transactional     fun addPeople(name: String?) {         personRepository.saveAndFlush(Person(\"Jack\", \"Brown\"))         personRepository.saveAndFlush(Person(\"Julia\", \"Green\"))         if (name == null) {             throw Exception(\"name cannot be null\")         }         personRepository.saveAndFlush(Person(name, \"Purple\"))     } }<\/code><\/pre>\n<pre><code class=\"kotlin\">@SpringBootTest @AutoConfigureTestDatabase internal class PersonServiceTest {     @Autowired     lateinit var personRepository: PersonRepository      @Autowired     lateinit var personService: PersonService      @BeforeEach     fun beforeEach() {         personRepository.deleteAll()     }      @Test     fun `should rollback transaction if name is null`() {         assertThrows(Exception::class.java) { personService.addPeople(null) }         assertEquals(0, personRepository.count())     } }<\/code><\/pre>\n<p>\u0422\u0435\u0441\u0442 \u043f\u0430\u0434\u0430\u0435\u0442 \u043a\u0430\u043a \u0432 Java.<\/p>\n<pre><code>expected: &lt;0&gt; but was: &lt;2&gt; Expected :0 Actual   :2<\/code><\/pre>\n<p>\u0417\u0434\u0435\u0441\u044c \u043d\u0435\u0442 \u043d\u0438\u0447\u0435\u0433\u043e \u0443\u0434\u0438\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0433\u043e. Spring \u0443\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u0442 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u044f\u043c\u0438 \u0432 Kotlin \u0440\u043e\u0432\u043d\u043e \u0442\u0430\u043a \u0436\u0435, \u043a\u0430\u043a \u0438 \u0432 Java. \u041d\u043e \u0432 Java \u043c\u044b \u043d\u0435 \u043c\u043e\u0436\u0435\u043c \u0432\u044b\u0437\u0432\u0430\u0442\u044c \u043c\u0435\u0442\u043e\u0434, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u044b\u0431\u0440\u0430\u0441\u044b\u0432\u0430\u0435\u0442 <code>java.lang.Exception<\/code>, \u043d\u0435 \u043e\u0431\u043e\u0440\u0430\u0447\u0438\u0432\u0430\u044f \u0438\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u044e \u0432 <code>try-catch<\/code> \u0438\u043b\u0438 \u043d\u0435 \u043f\u0440\u043e\u0431\u0440\u0430\u0441\u044b\u0432\u0430\u044f \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 \u0434\u0430\u043b\u044c\u0448\u0435. Kotlin \u0436\u0435 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442. \u042d\u0442\u043e \u043c\u043e\u0436\u0435\u0442 \u043f\u0440\u0438\u0432\u0435\u0441\u0442\u0438 \u043a \u043d\u0435\u043f\u0440\u0435\u0434\u0432\u0438\u0434\u0435\u043d\u043d\u044b\u043c \u043e\u0448\u0438\u0431\u043a\u0430\u043c \u0438 \u0442\u0440\u0443\u0434\u043d\u043e \u0443\u043b\u043e\u0432\u0438\u043c\u044b\u043c \u0431\u0430\u0433\u0430\u043c. \u0422\u0430\u043a \u0447\u0442\u043e \u043a \u0442\u0430\u043a\u0438\u043c \u0432\u0435\u0449\u0430\u043c \u0441\u043b\u0435\u0434\u0443\u0435\u0442 \u043e\u0442\u043d\u043e\u0441\u0438\u0442\u044c\u0441\u044f \u0432\u0434\u0432\u043e\u0439\u043d\u0435 \u0432\u043d\u0438\u043c\u0430\u0442\u0435\u043b\u044c\u043d\u0435\u0435.<\/p>\n<blockquote>\n<p>\u0421\u0442\u0440\u043e\u0433\u043e \u0433\u043e\u0432\u043e\u0440\u044f, \u0432 Java \u0435\u0441\u0442\u044c <a href=\"https:\/\/www.gamlor.info\/wordpress\/2010\/02\/throwing-checked-excpetions-like-unchecked-exceptions-in-java\/\" rel=\"noopener noreferrer nofollow\">\u0445\u0430\u043a<\/a>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0432\u044b\u0431\u0440\u043e\u0441\u0438\u0442\u044c checked \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f, \u043d\u0435 \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u044f <code>throws<\/code> \u0432 \u0441\u0438\u0433\u043d\u0430\u0442\u0443\u0440\u0435.<\/p>\n<\/blockquote>\n<h2>\u0417\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/h2>\n<p>\u042d\u0442\u043e \u0432\u0441\u0435, \u0447\u0442\u043e \u044f \u0445\u043e\u0442\u0435\u043b \u0440\u0430\u0441\u0441\u043a\u0430\u0437\u0430\u0442\u044c \u043e <code><a class=\"mention\" href=\"\/users\/transactional\">@Transactional<\/a><\/code>\u0432 Spring. \u0415\u0441\u043b\u0438 \u0443 \u0432\u0430\u0441 \u0435\u0441\u0442\u044c \u043a\u0430\u043a\u0438\u0435-\u0442\u043e \u0432\u043e\u043f\u0440\u043e\u0441\u044b \u0438\u043b\u0438 \u043f\u043e\u0436\u0435\u043b\u0430\u043d\u0438\u044f, \u043f\u043e\u0436\u0430\u043b\u0443\u0439\u0441\u0442\u0430, \u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0439\u0442\u0435 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0438. \u0421\u043f\u0430\u0441\u0438\u0431\u043e \u0437\u0430 \u0447\u0442\u0435\u043d\u0438\u0435!<\/p>\n<p>Spring \u2014 \u0441\u0430\u043c\u044b\u0439 \u043f\u043e\u043f\u0443\u043b\u044f\u0440\u043d\u044b\u0439 \u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a \u0432 \u043c\u0438\u0440\u0435 Java. \u0420\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0430\u043c \u0438\u0437 \u00ab\u043a\u043e\u0440\u043e\u0431\u043a\u0438\u00bb \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u044b \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u044b \u0434\u043b\u044f API, \u0440\u043e\u043b\u0435\u0432\u043e\u0439 \u043c\u043e\u0434\u0435\u043b\u0438, \u043a\u044d\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0438 \u0434\u043e\u0441\u0442\u0443\u043f\u0430 \u043a \u0434\u0430\u043d\u043d\u044b\u043c. Spring Data \u0432 \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e\u0441\u0442\u0438 \u0434\u0435\u043b\u0430\u0435\u0442 \u0436\u0438\u0437\u043d\u044c \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0441\u0442\u0430 \u0433\u043e\u0440\u0430\u0437\u0434\u043e \u043b\u0435\u0433\u0447\u0435. \u041d\u0430\u043c \u0431\u043e\u043b\u044c\u0448\u0435 \u043d\u0435 \u043d\u0443\u0436\u043d\u043e \u0431\u0435\u0441\u043f\u043e\u043a\u043e\u0438\u0442\u044c\u0441\u044f \u043e \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u044f\u0445 \u043a \u0431\u0430\u0437\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 \u0438 \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0438 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u044f\u043c\u0438. \u0424\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a \u0441\u0434\u0435\u043b\u0430\u0435\u0442 \u0432\u0441\u0435 \u0437\u0430 \u043d\u0430\u0441. \u041e\u0434\u043d\u0430\u043a\u043e \u0442\u043e\u0442 \u0444\u0430\u043a\u0442, \u0447\u0442\u043e \u043c\u043d\u043e\u0433\u0438\u0435 \u0434\u0435\u0442\u0430\u043b\u0438 \u043e\u0441\u0442\u0430\u044e\u0442\u0441\u044f \u0434\u043b\u044f \u043d\u0430\u0441 \u0441\u043a\u0440\u044b\u0442\u044b\u043c\u0438, \u043c\u043e\u0436\u0435\u0442 \u043f\u0440\u0438\u0432\u0435\u0441\u0442\u0438 \u043a \u0442\u0440\u0443\u0434\u043d\u043e \u043e\u0442\u043b\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u043c\u044b\u043c \u0431\u0430\u0433\u0430\u043c \u0438 \u043e\u0448\u0438\u0431\u043a\u0430\u043c. \u0422\u0430\u043a \u0447\u0442\u043e \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u0433\u043b\u0443\u0431\u0436\u0435 \u043f\u043e\u0433\u0440\u0443\u0437\u0438\u043c\u0441\u044f \u0432 \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u044e <code>@Transa\u0441tional<\/code> \u0438 \u0443\u0437\u043d\u0430\u0435\u043c, \u0447\u0442\u043e \u0436\u0435 \u0442\u0430\u043c \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442.<\/p>\n<\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/567368\/\"> https:\/\/habr.com\/ru\/post\/567368\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"\n<div class=\"post__text post__text_v2\" id=\"post-content-body\">\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<h2>Rollback \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e<\/h2>\n<p>\u041f\u0440\u0435\u0434\u043f\u043e\u043b\u043e\u0436\u0438\u043c, \u0447\u0442\u043e \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u0441\u0435\u0440\u0432\u0438\u0441, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0441\u043e\u0437\u0434\u0430\u0435\u0442 \u0442\u0440\u0435\u0445 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439 \u0432 \u0440\u0430\u043c\u043a\u0430\u0445 \u043e\u0434\u043d\u043e\u0439 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438. \u0415\u0441\u043b\u0438 \u0447\u0442\u043e-\u0442\u043e \u0438\u0434\u0435\u0442 \u043d\u0435 \u0442\u0430\u043a, \u0432\u044b\u0431\u0440\u0430\u0441\u044b\u0432\u0430\u0435\u0442\u0441\u044f <code>java.lang.Exception<\/code>.<\/p>\n<pre><code class=\"java\">@Service public class PersonService {   @Autowired   private PersonRepository personRepository;    @Transactional   public void addPeople(String name) throws Exception {     personRepository.saveAndFlush(new Person(\"Jack\", \"Brown\"));     personRepository.saveAndFlush(new Person(\"Julia\", \"Green\"));     if (name == null) {       throw new Exception(\"name cannot be null\");     }     personRepository.saveAndFlush(new Person(name, \"Purple\"));   } }<\/code><\/pre>\n<p>\u0410 \u0432\u043e\u0442 \u043f\u0440\u043e\u0441\u0442\u043e\u0439 unit-\u0442\u0435\u0441\u0442.<\/p>\n<pre><code class=\"java\">@SpringBootTest @AutoConfigureTestDatabase class PersonServiceTest {   @Autowired   private PersonService personService;   @Autowired   private PersonRepository personRepository;    @BeforeEach   void beforeEach() {     personRepository.deleteAll();   }    @Test   void shouldRollbackTransactionIfNameIsNull() {     assertThrows(Exception.class, () -&gt; personService.addPeople(null));     assertEquals(0, personRepository.count());   } }<\/code><\/pre>\n<p>\u041a\u0430\u043a \u0434\u0443\u043c\u0430\u0435\u0442\u0435, \u0442\u0435\u0441\u0442 \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u0441\u044f \u0443\u0441\u043f\u0435\u0448\u043d\u043e, \u0438\u043b\u0438 \u043d\u0435\u0442? \u041b\u043e\u0433\u0438\u043a\u0430 \u0433\u043e\u0432\u043e\u0440\u0438\u0442 \u043d\u0430\u043c, \u0447\u0442\u043e Spring \u0434\u043e\u043b\u0436\u0435\u043d \u043e\u0442\u043a\u0430\u0442\u0438\u0442\u044c \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u044e \u0438\u0437-\u0437\u0430 \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f. \u0421\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e <code>personRepository.count()<\/code> \u0434\u043e\u043b\u0436\u0435\u043d \u0432\u0435\u0440\u043d\u0443\u0442\u044c <code>0<\/code>, \u0442\u0430\u043a \u0432\u0435\u0434\u044c? \u041d\u0435 \u0441\u043e\u0432\u0441\u0435\u043c.<\/p>\n<pre><code>expected: &lt;0&gt; but was: &lt;2&gt; Expected :0 Actual   :2<\/code><\/pre>\n<p>\u0417\u0434\u0435\u0441\u044c \u0442\u0440\u0435\u0431\u0443\u044e\u0442\u0441\u044f \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043e\u0431\u044a\u044f\u0441\u043d\u0435\u043d\u0438\u044f. \u041f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e Spring \u043e\u0442\u043a\u0430\u0442\u044b\u0432\u0430\u0435\u0442 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438 \u0442\u043e\u043b\u044c\u043a\u043e \u0432 \u0441\u043b\u0443\u0447\u0430\u0435 <strong>\u043d\u0435\u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c\u043e\u0433\u043e <\/strong>\u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f. <strong>\u041f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c\u044b\u0435<\/strong> \u0436\u0435 \u0441\u0447\u0438\u0442\u0430\u044e\u0442\u0441\u044f \u00ab\u0432\u043e\u0441\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u043c\u044b\u043c\u0438\u00bb \u0438\u0437-\u0437\u0430 \u0447\u0435\u0433\u043e Spring \u0432\u043c\u0435\u0441\u0442\u043e <code>rollback<\/code> \u0434\u0435\u043b\u0430\u0435\u0442 <code>commit<\/code>. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 <code>personRepository.count()<\/code> \u0432\u043e\u0437\u0440\u0430\u0449\u0430\u0435\u0442 <code>2<\/code>.<\/p>\n<p>\u0421\u0430\u043c\u044b\u0439 \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u0438\u0441\u043f\u0440\u0430\u0432\u0438\u0442\u044c \u044d\u0442\u043e \u2014 \u0437\u0430\u043c\u0435\u043d\u0438\u0442\u044c <code>Exception<\/code> \u043d\u0430 \u043d\u0435\u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c\u043e\u0435 \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, <code>NullPointerException<\/code>. \u041b\u0438\u0431\u043e \u043c\u043e\u0436\u043d\u043e \u043f\u0435\u0440\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u0430\u0442\u0440\u0438\u0431\u0443\u0442 <code>rollbackFor<\/code> \u0443 \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u0438.<\/p>\n<p>\u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043e\u0431\u0430 \u044d\u0442\u0438\u0445 \u043c\u0435\u0442\u043e\u0434\u0430 \u043a\u043e\u0440\u0440\u0435\u043a\u0442\u043d\u043e \u043e\u0442\u043a\u0430\u0442\u044b\u0432\u0430\u044e\u0442 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u044e.<\/p>\n<pre><code class=\"java\">@Service public class PersonService {   @Autowired   private PersonRepository personRepository;    @Transactional(rollbackFor = Exception.class)   public void addPeopleWithCheckedException(String name) throws Exception {     addPeople(name, Exception::new);   }    @Transactional   public void addPeopleWithNullPointerException(String name) {     addPeople(name, NullPointerException::new);   }    private &lt;T extends Exception&gt; void addPeople(String name, Supplier&lt;? extends T&gt; exceptionSupplier) throws T {     personRepository.saveAndFlush(new Person(\"Jack\", \"Brown\"));     personRepository.saveAndFlush(new Person(\"Julia\", \"Green\"));     if (name == null) {       throw exceptionSupplier.get();     }     personRepository.saveAndFlush(new Person(name, \"Purple\"));   } }<\/code><\/pre>\n<pre><code class=\"java\">@SpringBootTest @AutoConfigureTestDatabase class PersonServiceTest {   @Autowired   private PersonService personService;   @Autowired   private PersonRepository personRepository;    @BeforeEach   void beforeEach() {     personRepository.deleteAll();   }    @Test   void testThrowsExceptionAndRollback() {     assertThrows(Exception.class, () -&gt; personService.addPeopleWithCheckedException(null));     assertEquals(0, personRepository.count());   }    @Test   void testThrowsNullPointerExceptionAndRollback() {     assertThrows(NullPointerException.class, () -&gt; personService.addPeopleWithNullPointerException(null));     assertEquals(0, personRepository.count());   }  }<\/code><\/pre>\n<figure class=\"\"><figcaption><\/figcaption><\/figure>\n<h2>Rollback \u043f\u0440\u0438 \u00ab\u0433\u043b\u0443\u0448\u0435\u043d\u0438\u0438\u00bb \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f<\/h2>\n<p>\u041d\u0435 \u0432\u0441\u0435 \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f \u0434\u043e\u043b\u0436\u043d\u044b \u0431\u044b\u0442\u044c \u043f\u0440\u043e\u0431\u0440\u043e\u0448\u0435\u043d\u044b \u0432\u0432\u0435\u0440\u0445 \u043f\u043e \u0441\u0442\u0435\u043a\u0443 \u0432\u044b\u0437\u043e\u0432\u043e\u0432. \u0418\u043d\u043e\u0433\u0434\u0430 \u0432\u043f\u043e\u043b\u043d\u0435 \u0434\u043e\u043f\u0443\u0441\u0442\u0438\u043c\u043e \u043e\u0442\u043b\u043e\u0432\u0438\u0442\u044c \u0435\u0433\u043e \u0432\u043d\u0443\u0442\u0440\u0438 \u043c\u0435\u0442\u043e\u0434\u0430 \u0438 \u0437\u0430\u043b\u043e\u0433\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u043e\u0431 \u044d\u0442\u043e\u043c.<\/p>\n<p>\u041f\u0440\u0435\u0434\u043f\u043e\u043b\u043e\u0436\u0438\u043c, \u0447\u0442\u043e \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u0435\u0449\u0435 \u043e\u0434\u0438\u043d <strong>\u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u043e\u043d\u043d\u044b\u0439<\/strong> \u0441\u0435\u0440\u0432\u0438\u0441, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u0442, \u043c\u043e\u0436\u0435\u0442 \u043b\u0438 \u0431\u044b\u0442\u044c \u0441\u043e\u0437\u0434\u0430\u043d \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u0441 \u043f\u0435\u0440\u0435\u0434\u0430\u043d\u043d\u044b\u043c \u0438\u043c\u0435\u043d\u0435\u043c. \u0415\u0441\u043b\u0438 \u043d\u0435\u0442, \u0432\u044b\u0431\u0440\u0430\u0441\u044b\u0432\u0430\u0435\u0442\u0441\u044f <code>IllegalArgumentException<\/code>.<\/p>\n<pre><code class=\"java\">@Service public class PersonValidateService {   @Autowired   private PersonRepository personRepository;    @Transactional   public void validateName(String name) {     if (name == null || name.isBlank() || personRepository.existsByFirstName(name)) {       throw new IllegalArgumentException(\"name is forbidden\");     }   } }<\/code><\/pre>\n<p>\u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u044e \u0432 \u043d\u0430\u0448 <code>PersonService<\/code>.<\/p>\n<pre><code class=\"java\">@Service @Slf4j public class PersonService {   @Autowired   private PersonRepository personRepository;   @Autowired   private PersonValidateService personValidateService;    @Transactional   public void addPeople(String name) {     personRepository.saveAndFlush(new Person(\"Jack\", \"Brown\"));     personRepository.saveAndFlush(new Person(\"Julia\", \"Green\"));     String resultName = name;     try {       personValidateService.validateName(name);     }     catch (IllegalArgumentException e) {       log.error(\"name is not allowed. Using default one\");       resultName = \"DefaultName\";     }     personRepository.saveAndFlush(new Person(resultName, \"Purple\"));   } }<\/code><\/pre>\n<p>\u0415\u0441\u043b\u0438 \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u044f \u043d\u0435 \u043f\u0440\u043e\u0445\u043e\u0434\u0438\u0442, \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0441 \u0438\u043c\u0435\u043d\u0435\u043c \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e.<\/p>\n<p>\u041e\u043a\u0435\u0439, \u0442\u0435\u043f\u0435\u0440\u044c \u043d\u0443\u0436\u043d\u043e \u043f\u0440\u043e\u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043d\u043e\u0432\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u044c.<\/p>\n<pre><code class=\"java\">@SpringBootTest @AutoConfigureTestDatabase class PersonServiceTest {   @Autowired   private PersonService personService;   @Autowired   private PersonRepository personRepository;    @BeforeEach   void beforeEach() {     personRepository.deleteAll();   }    @Test   void shouldCreatePersonWithDefaultName() {     assertDoesNotThrow(() -&gt; personService.addPeople(null));     Optional&lt;Person&gt; defaultPerson = personRepository.findByFirstName(\"DefaultName\");     assertTrue(defaultPerson.isPresent());   } }<\/code><\/pre>\n<p>\u041e\u0434\u043d\u0430\u043a\u043e \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u043d\u0435\u043e\u0436\u0438\u0434\u0430\u043d\u043d\u044b\u043c.<\/p>\n<pre><code>Unexpected exception thrown: org.springframework.transaction.UnexpectedRollbackException: Transaction silently rolled back because it has been marked as rollback-only<\/code><\/pre>\n<p>\u0421\u0442\u0440\u0430\u043d\u043d\u043e. \u041c\u044b \u043e\u0442\u043b\u043e\u0432\u0438\u043b\u0438 \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435. \u041f\u043e\u0447\u0435\u043c\u0443 \u0436\u0435 Spring \u043e\u0442\u043a\u0430\u0442\u0438\u043b \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u044e? \u041f\u0440\u0435\u0436\u0434\u0435 \u0432\u0441\u0435\u0433\u043e \u043d\u0443\u0436\u043d\u043e \u0440\u0430\u0437\u043e\u0431\u0440\u0430\u0442\u044c\u0441\u044f \u0441 \u0442\u0435\u043c, \u043a\u0430\u043a Spring \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0441 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u044f\u043c\u0438.<\/p>\n<p>\u041f\u043e\u0434 \u043a\u0430\u043f\u043e\u0442\u043e\u043c Spring \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0435\u0442 \u043f\u0430\u0442\u0442\u0435\u0440\u043d <a href=\"https:\/\/www.baeldung.com\/spring-aop\" rel=\"noopener noreferrer nofollow\">\u0430\u0441\u043f\u0435\u043a\u0442\u043d\u043e-\u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f<\/a>. \u041e\u043f\u0443\u0441\u043a\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u044b\u0435 \u0434\u0435\u0442\u0430\u043b\u0438, \u0438\u0434\u0435\u044f \u0437\u0430\u043a\u043b\u044e\u0447\u0430\u0435\u0442\u0441\u044f \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e bean \u043e\u0431\u043e\u0440\u0430\u0447\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u0432 \u043f\u0440\u043e\u043a\u0441\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u044e\u0442\u0441\u044f \u0432 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0435 \u0441\u0442\u0430\u0440\u0442\u0430 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f. \u0412\u043d\u0443\u0442\u0440\u0438 \u044d\u0442\u043e\u0433\u043e \u043f\u0440\u043e\u043a\u0441\u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u0442\u0440\u0435\u0431\u0443\u0435\u043c\u0430\u044f \u043b\u043e\u0433\u0438\u043a\u0430. \u0412 \u043d\u0430\u0448\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435, \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u044f\u043c\u0438. \u041a\u043e\u0433\u0434\u0430 \u043a\u0430\u043a\u043e\u0439-\u043d\u0438\u0431\u0443\u0434\u044c bean \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u043e\u043d\u043d\u044b\u0439 \u0441\u0435\u0440\u0432\u0438\u0441 \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 DI \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438, Spring \u043d\u0430 \u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435 \u0432\u043d\u0435\u0434\u0440\u044f\u0435\u0442 \u043f\u0440\u043e\u043a\u0441\u0438.<\/p>\n<p>\u041d\u0438\u0436\u0435 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d workflow \u0432\u044b\u0437\u043e\u0432\u0430 \u0432\u044b\u0448\u0435\u043d\u0430\u043f\u0438\u0441\u0430\u043d\u043d\u043e\u0433\u043e \u043c\u0435\u0442\u043e\u0434\u0430 <code>addPeople<\/code>.<\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p>\u041f\u0430\u0440\u0430\u043c\u0435\u0442\u0440 <code>propagation<\/code> \u0443 <code><a class=\"mention\" href=\"\/users\/transactional\">@Transactional<\/a><\/code> \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e \u0438\u043c\u0435\u0435\u0442 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 <code>REQUIRED<\/code>. \u042d\u0442\u043e \u0437\u043d\u0430\u0447\u0438\u0442, \u0447\u0442\u043e \u043d\u043e\u0432\u0430\u044f \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u044f \u0441\u043e\u0437\u0434\u0430\u0435\u0442\u0441\u044f, \u0435\u0441\u043b\u0438 \u043e\u043d\u0430 \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0443\u0435\u0442. \u0418\u043d\u0430\u0447\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u0435\u0442\u0441\u044f \u0432 \u0442\u0435\u043a\u0443\u0449\u0435\u0439. \u0422\u0430\u043a \u0447\u0442\u043e \u0432 \u043d\u0430\u0448\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0432\u0435\u0441\u044c \u0437\u0430\u043f\u0440\u043e\u0441 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u0432 \u0440\u0430\u043c\u043a\u0430\u0445 \u0435\u0434\u0438\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0439 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438.<\/p>\n<p>\u041e\u0434\u043d\u0430\u043a\u043e \u0437\u0434\u0435\u0441\u044c \u0435\u0441\u0442\u044c \u043d\u044e\u0430\u043d\u0441. \u0415\u0441\u043b\u0438 <code>RuntimeException<\/code> \u0431\u044b\u043b \u0432\u044b\u0431\u0440\u043e\u0448\u0435\u043d \u0438\u0437-\u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446 <em>transactional proxy<\/em>, \u0442\u043e Spring \u043e\u0442\u043c\u0435\u0447\u0430\u0435\u0442 \u0442\u0435\u043a\u0443\u0449\u0443\u044e \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u044e \u043a\u0430\u043a <strong>rollback-only<\/strong>. \u0417\u0434\u0435\u0441\u044c \u0443 \u043d\u0430\u0441 \u0438\u043c\u0435\u043d\u043d\u043e \u0442\u0430\u043a\u043e\u0439 \u0441\u043b\u0443\u0447\u0430\u0439. <code>PersonValidateService.validateName<\/code> \u0432\u044b\u0431\u0440\u043e\u0441\u0438\u043b <code>IllegalArgumentException<\/code>. Transactional proxy \u0432\u044b\u0441\u0442\u0430\u0432\u0438\u043b \u0444\u043b\u0430\u0433 <code>rollback<\/code>. \u0414\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0438\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0443\u0436\u0435 \u043d\u0435 \u0438\u043c\u0435\u044e\u0442 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f, \u0442\u0430\u043a \u043a\u0430\u043a \u0432 \u043a\u043e\u043d\u0435\u0447\u043d\u043e\u043c \u0438\u0442\u043e\u0433\u0435 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u044f \u043d\u0435 \u0437\u0430\u043a\u043e\u043c\u043c\u0438\u0442\u0438\u0442\u0441\u044f.<\/p>\n<p>\u041a\u0430\u043a\u043e\u0432\u043e \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b? \u0412\u043e\u043e\u0431\u0449\u0435 \u0438\u0445 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0430\u0442\u0440\u0438\u0431\u0443\u0442 <code>noRollbackFor<\/code> \u0432 <code>PersonValidateService<\/code>.<\/p>\n<pre><code class=\"java\">@Service public class PersonValidateService {   @Autowired   private PersonRepository personRepository;    @Transactional(noRollbackFor = IllegalArgumentException.class)   public void validateName(String name) {     if (name == null || name.isBlank() || personRepository.existsByFirstName(name)) {       throw new IllegalArgumentException(\"name is forbidden\");     }   } }<\/code><\/pre>\n<p>\u0415\u0441\u0442\u044c \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u043f\u043e\u043c\u0435\u043d\u044f\u0442\u044c <code>propagation<\/code> \u043d\u0430 <code>REQUIRES_NEW<\/code>. \u0412 \u044d\u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 <code>PersonValidateService.validateName<\/code> \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d \u0432 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e\u0439 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438. \u0422\u0430\u043a \u0447\u0442\u043e \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u0430\u044f \u043d\u0435 \u0431\u0443\u0434\u0435\u0442 \u043e\u0442\u043c\u0435\u043d\u0435\u043d\u0430.<\/p>\n<pre><code class=\"java\">@Service public class PersonValidateService {   @Autowired   private PersonRepository personRepository;    @Transactional(propagation = Propagation.REQUIRES_NEW)   public void validateName(String name) {     if (name == null || name.isBlank() || personRepository.existsByFirstName(name)) {       throw new IllegalArgumentException(\"name is forbidden\");     }   } }<\/code><\/pre>\n<h2>\u0412\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u0435 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b \u0441 Kotlin<\/h2>\n<p>\u0423 Kotlin \u043c\u043d\u043e\u0433\u043e \u0441\u0445\u043e\u0436\u0435\u0441\u0442\u0435\u0439 \u0441 Java. \u041d\u043e \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f\u043c\u0438 \u043d\u0435 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043e\u0434\u043d\u043e\u0439 \u0438\u0437 \u043d\u0438\u0445.<\/p>\n<p>Kotlin \u0443\u0431\u0440\u0430\u043b \u043f\u043e\u043d\u044f\u0442\u0438\u044f <em>\u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c\u044b\u0445<\/em> \u0438 <em>\u043d\u0435\u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c\u044b\u0445<\/em> \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0439. \u0421\u0442\u0440\u043e\u0433\u043e \u0433\u043e\u0432\u043e\u0440\u044f, \u0432\u0441\u0435 \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f \u0432 Kotlin <em>\u043d\u0435\u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c\u044b\u0435<\/em>, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u043d\u0430\u043c \u043d\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0442\u044c \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u0438 <code>throws SomeException<\/code> \u0432 \u0441\u0438\u0433\u043d\u0430\u0442\u0443\u0440\u0430\u0445 \u043c\u0435\u0442\u043e\u0434\u043e\u0432, \u0430 \u0442\u0430\u043a\u0436\u0435 \u043e\u0431\u043e\u0440\u0430\u0447\u0438\u0432\u0430\u0442\u044c \u0438\u0445 \u0432\u044b\u0437\u043e\u0432\u044b \u0432 <code>try-catch<\/code>. \u041e\u0431\u0441\u0443\u0436\u0434\u0435\u043d\u0438\u0435 \u043f\u043b\u044e\u0441\u043e\u0432 \u0438 \u043c\u0438\u043d\u0443\u0441\u043e\u0432 \u0442\u0430\u043a\u043e\u0433\u043e \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u2014 \u0442\u0435\u043c\u0430 \u0434\u043b\u044f \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0438. \u041d\u043e \u0441\u0435\u0439\u0447\u0430\u0441 \u044f \u0445\u043e\u0447\u0443 \u043f\u0440\u043e\u0434\u0435\u043c\u043e\u043d\u0441\u0442\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0432\u0430\u043c \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u043e\u0433\u0443\u0442 \u0438\u0437-\u0437\u0430 \u044d\u0442\u043e\u0433\u043e \u0432\u043e\u0437\u043d\u0438\u043a\u043d\u0443\u0442\u044c \u043f\u0440\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 Spring Data.<\/p>\n<p>\u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u0435\u0440\u0435\u043f\u0438\u0448\u0435\u043c \u0441\u0430\u043c\u044b\u0439 \u043f\u0435\u0440\u0432\u044b\u0439 \u043f\u0440\u0438\u043c\u0435\u0440 \u0441 <code>java.lang.Exception<\/code> \u043d\u0430 Kotlin.<\/p>\n<pre><code class=\"kotlin\">@Service class PersonService(     @Autowired     private val personRepository: PersonRepository ) {     @Transactional     fun addPeople(name: String?) {         personRepository.saveAndFlush(Person(\"Jack\", \"Brown\"))         personRepository.saveAndFlush(Person(\"Julia\", \"Green\"))         if (name == null) {             throw Exception(\"name cannot be null\")         }         personRepository.saveAndFlush(Person(name, \"Purple\"))     } }<\/code><\/pre>\n<pre><code class=\"kotlin\">@SpringBootTest @AutoConfigureTestDatabase internal class PersonServiceTest {     @Autowired     lateinit var personRepository: PersonRepository      @Autowired     lateinit var personService: PersonService      @BeforeEach     fun beforeEach() {         personRepository.deleteAll()     }      @Test     fun `should rollback transaction if name is null`() {         assertThrows(Exception::class.java) { personService.addPeople(null) }         assertEquals(0, personRepository.count())     } }<\/code><\/pre>\n<p>\u0422\u0435\u0441\u0442 \u043f\u0430\u0434\u0430\u0435\u0442 \u043a\u0430\u043a \u0432 Java.<\/p>\n<pre><code>expected: &lt;0&gt; but was: &lt;2&gt; Expected :0 Actual   :2<\/code><\/pre>\n<p>\u0417\u0434\u0435\u0441\u044c \u043d\u0435\u0442 \u043d\u0438\u0447\u0435\u0433\u043e \u0443\u0434\u0438\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0433\u043e. Spring \u0443\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u0442 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u044f\u043c\u0438 \u0432 Kotlin \u0440\u043e\u0432\u043d\u043e \u0442\u0430\u043a \u0436\u0435, \u043a\u0430\u043a \u0438 \u0432 Java. \u041d\u043e \u0432 Java \u043c\u044b \u043d\u0435 \u043c\u043e\u0436\u0435\u043c \u0432\u044b\u0437\u0432\u0430\u0442\u044c \u043c\u0435\u0442\u043e\u0434, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u044b\u0431\u0440\u0430\u0441\u044b\u0432\u0430\u0435\u0442 <code>java.lang.Exception<\/code>, \u043d\u0435 \u043e\u0431\u043e\u0440\u0430\u0447\u0438\u0432\u0430\u044f \u0438\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u044e \u0432 <code>try-catch<\/code> \u0438\u043b\u0438 \u043d\u0435 \u043f\u0440\u043e\u0431\u0440\u0430\u0441\u044b\u0432\u0430\u044f \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 \u0434\u0430\u043b\u044c\u0448\u0435. Kotlin \u0436\u0435 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442. \u042d\u0442\u043e \u043c\u043e\u0436\u0435\u0442 \u043f\u0440\u0438\u0432\u0435\u0441\u0442\u0438 \u043a \u043d\u0435\u043f\u0440\u0435\u0434\u0432\u0438\u0434\u0435\u043d\u043d\u044b\u043c \u043e\u0448\u0438\u0431\u043a\u0430\u043c \u0438 \u0442\u0440\u0443\u0434\u043d\u043e \u0443\u043b\u043e\u0432\u0438\u043c\u044b\u043c \u0431\u0430\u0433\u0430\u043c. \u0422\u0430\u043a \u0447\u0442\u043e \u043a \u0442\u0430\u043a\u0438\u043c \u0432\u0435\u0449\u0430\u043c \u0441\u043b\u0435\u0434\u0443\u0435\u0442 \u043e\u0442\u043d\u043e\u0441\u0438\u0442\u044c\u0441\u044f \u0432\u0434\u0432\u043e\u0439\u043d\u0435 \u0432\u043d\u0438\u043c\u0430\u0442\u0435\u043b\u044c\u043d\u0435\u0435.<\/p>\n<blockquote>\n<p>\u0421\u0442\u0440\u043e\u0433\u043e \u0433\u043e\u0432\u043e\u0440\u044f, \u0432 Java \u0435\u0441\u0442\u044c <a href=\"https:\/\/www.gamlor.info\/wordpress\/2010\/02\/throwing-checked-excpetions-like-unchecked-exceptions-in-java\/\" rel=\"noopener noreferrer nofollow\">\u0445\u0430\u043a<\/a>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0432\u044b\u0431\u0440\u043e\u0441\u0438\u0442\u044c checked \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f, \u043d\u0435 \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u044f <code>throws<\/code> \u0432 \u0441\u0438\u0433\u043d\u0430\u0442\u0443\u0440\u0435.<\/p>\n<\/blockquote>\n<h2>\u0417\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/h2>\n<p>\u042d<\/p>\n<\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-326342","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/326342","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=326342"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/326342\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=326342"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=326342"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=326342"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}