|
|
|
@ -1,5 +1,5 @@
@@ -1,5 +1,5 @@
|
|
|
|
|
/* |
|
|
|
|
* Copyright 2002-2018 the original author or authors. |
|
|
|
|
* Copyright 2002-2019 the original author or authors. |
|
|
|
|
* |
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
|
|
|
* you may not use this file except in compliance with the License. |
|
|
|
@ -16,6 +16,7 @@
@@ -16,6 +16,7 @@
|
|
|
|
|
|
|
|
|
|
package org.springframework.transaction.annotation; |
|
|
|
|
|
|
|
|
|
import io.vavr.control.Try; |
|
|
|
|
import org.junit.Test; |
|
|
|
|
|
|
|
|
|
import org.springframework.aop.framework.ProxyFactory; |
|
|
|
@ -123,12 +124,12 @@ public class AnnotationTransactionInterceptorTests {
@@ -123,12 +124,12 @@ public class AnnotationTransactionInterceptorTests {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void withRollback() { |
|
|
|
|
public void withRollbackOnRuntimeException() { |
|
|
|
|
ProxyFactory proxyFactory = new ProxyFactory(); |
|
|
|
|
proxyFactory.setTarget(new TestWithRollback()); |
|
|
|
|
proxyFactory.setTarget(new TestWithExceptions()); |
|
|
|
|
proxyFactory.addAdvice(this.ti); |
|
|
|
|
|
|
|
|
|
TestWithRollback proxy = (TestWithRollback) proxyFactory.getProxy(); |
|
|
|
|
TestWithExceptions proxy = (TestWithExceptions) proxyFactory.getProxy(); |
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
proxy.doSomethingErroneous(); |
|
|
|
@ -147,6 +148,88 @@ public class AnnotationTransactionInterceptorTests {
@@ -147,6 +148,88 @@ public class AnnotationTransactionInterceptorTests {
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void withCommitOnCheckedException() { |
|
|
|
|
ProxyFactory proxyFactory = new ProxyFactory(); |
|
|
|
|
proxyFactory.setTarget(new TestWithExceptions()); |
|
|
|
|
proxyFactory.addAdvice(this.ti); |
|
|
|
|
|
|
|
|
|
TestWithExceptions proxy = (TestWithExceptions) proxyFactory.getProxy(); |
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
proxy.doSomethingElseWithCheckedException(); |
|
|
|
|
fail("Should throw Exception"); |
|
|
|
|
} |
|
|
|
|
catch (Exception ex) { |
|
|
|
|
assertGetTransactionAndCommitCount(1); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void withRollbackOnCheckedExceptionAndRollbackRule() { |
|
|
|
|
ProxyFactory proxyFactory = new ProxyFactory(); |
|
|
|
|
proxyFactory.setTarget(new TestWithExceptions()); |
|
|
|
|
proxyFactory.addAdvice(this.ti); |
|
|
|
|
|
|
|
|
|
TestWithExceptions proxy = (TestWithExceptions) proxyFactory.getProxy(); |
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
proxy.doSomethingElseWithCheckedExceptionAndRollbackRule(); |
|
|
|
|
fail("Should throw Exception"); |
|
|
|
|
} |
|
|
|
|
catch (Exception ex) { |
|
|
|
|
assertGetTransactionAndRollbackCount(1); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void withVavrTrySuccess() { |
|
|
|
|
ProxyFactory proxyFactory = new ProxyFactory(); |
|
|
|
|
proxyFactory.setTarget(new TestWithVavrTry()); |
|
|
|
|
proxyFactory.addAdvice(this.ti); |
|
|
|
|
|
|
|
|
|
TestWithVavrTry proxy = (TestWithVavrTry) proxyFactory.getProxy(); |
|
|
|
|
|
|
|
|
|
proxy.doSomething(); |
|
|
|
|
assertGetTransactionAndCommitCount(1); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void withVavrTryRuntimeException() { |
|
|
|
|
ProxyFactory proxyFactory = new ProxyFactory(); |
|
|
|
|
proxyFactory.setTarget(new TestWithVavrTry()); |
|
|
|
|
proxyFactory.addAdvice(this.ti); |
|
|
|
|
|
|
|
|
|
TestWithVavrTry proxy = (TestWithVavrTry) proxyFactory.getProxy(); |
|
|
|
|
|
|
|
|
|
proxy.doSomethingErroneous(); |
|
|
|
|
assertGetTransactionAndRollbackCount(1); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void withVavrTryCheckedException() { |
|
|
|
|
ProxyFactory proxyFactory = new ProxyFactory(); |
|
|
|
|
proxyFactory.setTarget(new TestWithVavrTry()); |
|
|
|
|
proxyFactory.addAdvice(this.ti); |
|
|
|
|
|
|
|
|
|
TestWithVavrTry proxy = (TestWithVavrTry) proxyFactory.getProxy(); |
|
|
|
|
|
|
|
|
|
proxy.doSomethingErroneousWithCheckedException(); |
|
|
|
|
assertGetTransactionAndCommitCount(1); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void withVavrTryCheckedExceptionAndRollbackRule() { |
|
|
|
|
ProxyFactory proxyFactory = new ProxyFactory(); |
|
|
|
|
proxyFactory.setTarget(new TestWithVavrTry()); |
|
|
|
|
proxyFactory.addAdvice(this.ti); |
|
|
|
|
|
|
|
|
|
TestWithVavrTry proxy = (TestWithVavrTry) proxyFactory.getProxy(); |
|
|
|
|
|
|
|
|
|
proxy.doSomethingErroneousWithCheckedExceptionAndRollbackRule(); |
|
|
|
|
assertGetTransactionAndRollbackCount(1); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void withInterface() { |
|
|
|
|
ProxyFactory proxyFactory = new ProxyFactory(); |
|
|
|
@ -352,8 +435,8 @@ public class AnnotationTransactionInterceptorTests {
@@ -352,8 +435,8 @@ public class AnnotationTransactionInterceptorTests {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Transactional(rollbackFor = IllegalStateException.class) |
|
|
|
|
public static class TestWithRollback { |
|
|
|
|
@Transactional |
|
|
|
|
public static class TestWithExceptions { |
|
|
|
|
|
|
|
|
|
public void doSomethingErroneous() { |
|
|
|
|
assertTrue(TransactionSynchronizationManager.isActualTransactionActive()); |
|
|
|
@ -361,12 +444,55 @@ public class AnnotationTransactionInterceptorTests {
@@ -361,12 +444,55 @@ public class AnnotationTransactionInterceptorTests {
|
|
|
|
|
throw new IllegalStateException(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Transactional(rollbackFor = IllegalArgumentException.class) |
|
|
|
|
public void doSomethingElseErroneous() { |
|
|
|
|
assertTrue(TransactionSynchronizationManager.isActualTransactionActive()); |
|
|
|
|
assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly()); |
|
|
|
|
throw new IllegalArgumentException(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Transactional |
|
|
|
|
public void doSomethingElseWithCheckedException() throws Exception { |
|
|
|
|
assertTrue(TransactionSynchronizationManager.isActualTransactionActive()); |
|
|
|
|
assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly()); |
|
|
|
|
throw new Exception(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Transactional(rollbackFor = Exception.class) |
|
|
|
|
public void doSomethingElseWithCheckedExceptionAndRollbackRule() throws Exception { |
|
|
|
|
assertTrue(TransactionSynchronizationManager.isActualTransactionActive()); |
|
|
|
|
assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly()); |
|
|
|
|
throw new Exception(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Transactional |
|
|
|
|
public static class TestWithVavrTry { |
|
|
|
|
|
|
|
|
|
public Try<String> doSomething() { |
|
|
|
|
assertTrue(TransactionSynchronizationManager.isActualTransactionActive()); |
|
|
|
|
assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly()); |
|
|
|
|
return Try.success("ok"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public Try<String> doSomethingErroneous() { |
|
|
|
|
assertTrue(TransactionSynchronizationManager.isActualTransactionActive()); |
|
|
|
|
assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly()); |
|
|
|
|
return Try.failure(new IllegalStateException()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public Try<String> doSomethingErroneousWithCheckedException() { |
|
|
|
|
assertTrue(TransactionSynchronizationManager.isActualTransactionActive()); |
|
|
|
|
assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly()); |
|
|
|
|
return Try.failure(new Exception()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Transactional(rollbackFor = Exception.class) |
|
|
|
|
public Try<String> doSomethingErroneousWithCheckedExceptionAndRollbackRule() { |
|
|
|
|
assertTrue(TransactionSynchronizationManager.isActualTransactionActive()); |
|
|
|
|
assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly()); |
|
|
|
|
return Try.failure(new Exception()); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|