org.springframework.transaction.ReactiveTransactionManager Java Examples

The following examples show how to use org.springframework.transaction.ReactiveTransactionManager. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AbstractReactiveTransactionAspectTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * Check that a transaction is created and committed.
 */
@Test
public void transactionShouldSucceed() throws Exception {
	TransactionAttribute txatt = new DefaultTransactionAttribute();

	MapTransactionAttributeSource tas = new MapTransactionAttributeSource();
	tas.register(getNameMethod, txatt);

	ReactiveTransaction status = mock(ReactiveTransaction.class);
	ReactiveTransactionManager rtm = mock(ReactiveTransactionManager.class);
	// expect a transaction
	given(rtm.getReactiveTransaction(txatt)).willReturn(Mono.just(status));
	given(rtm.commit(status)).willReturn(Mono.empty());

	DefaultTestBean tb = new DefaultTestBean();
	TestBean itb = (TestBean) advised(tb, rtm, tas);

	itb.getName()
			.as(StepVerifier::create)
			.verifyComplete();

	verify(rtm).commit(status);
}
 
Example #2
Source File: AbstractReactiveTransactionAspectTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void noTransaction() throws Exception {
	ReactiveTransactionManager rtm = mock(ReactiveTransactionManager.class);

	DefaultTestBean tb = new DefaultTestBean();
	TransactionAttributeSource tas = new MapTransactionAttributeSource();

	// All the methods in this class use the advised() template method
	// to obtain a transaction object, configured with the when PlatformTransactionManager
	// and transaction attribute source
	TestBean itb = (TestBean) advised(tb, rtm, tas);

	checkReactiveTransaction(false);
	itb.getName();
	checkReactiveTransaction(false);

	// expect no calls
	verifyZeroInteractions(rtm);
}
 
Example #3
Source File: AbstractReactiveTransactionAspectTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * Check that a transaction is created and committed.
 */
@Test
public void transactionShouldSucceedWithNotNew() throws Exception {
	TransactionAttribute txatt = new DefaultTransactionAttribute();

	MapTransactionAttributeSource tas = new MapTransactionAttributeSource();
	tas.register(getNameMethod, txatt);

	ReactiveTransaction status = mock(ReactiveTransaction.class);
	ReactiveTransactionManager rtm = mock(ReactiveTransactionManager.class);
	// expect a transaction
	given(rtm.getReactiveTransaction(txatt)).willReturn(Mono.just(status));
	given(rtm.commit(status)).willReturn(Mono.empty());

	DefaultTestBean tb = new DefaultTestBean();
	TestBean itb = (TestBean) advised(tb, rtm, tas);

	itb.getName()
			.as(StepVerifier::create)
			.verifyComplete();

	verify(rtm).commit(status);
}
 
Example #4
Source File: ReactiveTransactionSupportTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void existingTransaction() {
	ReactiveTransactionManager tm = new ReactiveTestTransactionManager(true, true);

	tm.getReactiveTransaction(new DefaultTransactionDefinition(TransactionDefinition.PROPAGATION_SUPPORTS))
			.subscriberContext(TransactionContextManager.createTransactionContext()).cast(GenericReactiveTransaction.class)
			.as(StepVerifier::create).consumeNextWith(actual -> {
				assertNotNull(actual.getTransaction());
				assertFalse(actual.isNewTransaction());
			}).verifyComplete();

	tm.getReactiveTransaction(new DefaultTransactionDefinition(TransactionDefinition.PROPAGATION_REQUIRED))
			.subscriberContext(TransactionContextManager.createTransactionContext()).cast(GenericReactiveTransaction.class)
			.as(StepVerifier::create).consumeNextWith(actual -> {
				assertNotNull(actual.getTransaction());
				assertFalse(actual.isNewTransaction());
			}).verifyComplete();

	tm.getReactiveTransaction(new DefaultTransactionDefinition(TransactionDefinition.PROPAGATION_MANDATORY))
			.subscriberContext(TransactionContextManager.createTransactionContext()).cast(GenericReactiveTransaction.class)
			.as(StepVerifier::create).consumeNextWith(actual -> {
				assertNotNull(actual.getTransaction());
				assertFalse(actual.isNewTransaction());
			}).verifyComplete();
}
 
Example #5
Source File: ReactiveTransactionSupportTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void noExistingTransaction() {
	ReactiveTransactionManager tm = new ReactiveTestTransactionManager(false, true);

	tm.getReactiveTransaction(new DefaultTransactionDefinition(TransactionDefinition.PROPAGATION_SUPPORTS))
			.subscriberContext(TransactionContextManager.createTransactionContext()).cast(GenericReactiveTransaction.class)
			.as(StepVerifier::create).consumeNextWith(actual ->
				assertFalse(actual.hasTransaction())
			).verifyComplete();

	tm.getReactiveTransaction(new DefaultTransactionDefinition(TransactionDefinition.PROPAGATION_REQUIRED))
			.cast(GenericReactiveTransaction.class).subscriberContext(TransactionContextManager.createTransactionContext())
			.as(StepVerifier::create).consumeNextWith(actual -> {
				assertTrue(actual.hasTransaction());
				assertTrue(actual.isNewTransaction());
			}).verifyComplete();

	tm.getReactiveTransaction(new DefaultTransactionDefinition(TransactionDefinition.PROPAGATION_MANDATORY))
			.subscriberContext(TransactionContextManager.createTransactionContext()).cast(GenericReactiveTransaction.class)
			.as(StepVerifier::create).expectError(IllegalTransactionStateException.class).verify();
}
 
Example #6
Source File: TransactionAspectSupport.java    From spring-analysis-note with MIT License 6 votes vote down vote up
private ReactiveTransactionInfo prepareTransactionInfo(@Nullable ReactiveTransactionManager tm,
		@Nullable TransactionAttribute txAttr, String joinpointIdentification,
		@Nullable ReactiveTransaction transaction) {

	ReactiveTransactionInfo txInfo = new ReactiveTransactionInfo(tm, txAttr, joinpointIdentification);
	if (txAttr != null) {
		// We need a transaction for this method...
		if (logger.isTraceEnabled()) {
			logger.trace("Getting transaction for [" + txInfo.getJoinpointIdentification() + "]");
		}
		// The transaction manager will flag an error if an incompatible tx already exists.
		txInfo.newReactiveTransaction(transaction);
	}
	else {
		// The TransactionInfo.hasTransaction() method will return false. We created it only
		// to preserve the integrity of the ThreadLocal stack maintained in this class.
		if (logger.isTraceEnabled()) {
			logger.trace("Don't need to create transaction for [" + joinpointIdentification +
					"]: This method isn't transactional.");
		}
	}

	return txInfo;
}
 
Example #7
Source File: TransactionAspectSupport.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@SuppressWarnings("serial")
private Mono<ReactiveTransactionInfo> createTransactionIfNecessary(@Nullable ReactiveTransactionManager tm,
		@Nullable TransactionAttribute txAttr, final String joinpointIdentification) {

	// If no name specified, apply method identification as transaction name.
	if (txAttr != null && txAttr.getName() == null) {
		txAttr = new DelegatingTransactionAttribute(txAttr) {
			@Override
			public String getName() {
				return joinpointIdentification;
			}
		};
	}
	TransactionAttribute attrToUse = txAttr;

	Mono<ReactiveTransaction> tx = Mono.empty();
	if (txAttr != null) {
		if (tm != null) {
			tx = tm.getReactiveTransaction(txAttr);
		}
		else {
			if (logger.isDebugEnabled()) {
				logger.debug("Skipping transactional joinpoint [" + joinpointIdentification +
						"] because no transaction manager has been configured");
			}
		}
	}

	return tx.map(it -> prepareTransactionInfo(tm, attrToUse, joinpointIdentification, it)).switchIfEmpty(
			Mono.defer(() -> Mono.just(prepareTransactionInfo(tm, attrToUse, joinpointIdentification, null))));
}
 
Example #8
Source File: AbstractReactiveTransactionAspectTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Check that two transactions are created and committed.
 */
@Test
public void twoTransactionsShouldSucceed() throws Exception {
	TransactionAttribute txatt = new DefaultTransactionAttribute();

	MapTransactionAttributeSource tas1 = new MapTransactionAttributeSource();
	tas1.register(getNameMethod, txatt);
	MapTransactionAttributeSource tas2 = new MapTransactionAttributeSource();
	tas2.register(setNameMethod, txatt);

	ReactiveTransaction status = mock(ReactiveTransaction.class);
	ReactiveTransactionManager rtm = mock(ReactiveTransactionManager.class);
	// expect a transaction
	given(rtm.getReactiveTransaction(txatt)).willReturn(Mono.just(status));
	given(rtm.commit(status)).willReturn(Mono.empty());

	DefaultTestBean tb = new DefaultTestBean();
	TestBean itb = (TestBean) advised(tb, rtm, new TransactionAttributeSource[] {tas1, tas2});

	itb.getName()
			.as(StepVerifier::create)
			.verifyComplete();

	Mono.from(itb.setName("myName"))
			.as(StepVerifier::create)
			.verifyComplete();

	verify(rtm, times(2)).commit(status);
}
 
Example #9
Source File: ReactiveTransactionInterceptorTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Template method to create an advised object given the
 * target object and transaction setup.
 * Creates a TransactionInterceptor and applies it.
 */
@Override
protected Object advised(Object target, ReactiveTransactionManager ptm, TransactionAttributeSource tas) {
	TransactionInterceptor ti = new TransactionInterceptor();
	ti.setTransactionManager(ptm);

	assertThat(ti.getTransactionManager()).isEqualTo(ptm);
	ti.setTransactionAttributeSource(tas);
	assertThat(ti.getTransactionAttributeSource()).isEqualTo(tas);

	ProxyFactory pf = new ProxyFactory(target);
	pf.addAdvice(0, ti);
	return pf.getProxy();
}
 
Example #10
Source File: ReactiveTransactionInterceptorTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
protected Object advised(Object target, ReactiveTransactionManager ptm, TransactionAttributeSource[] tas) {
	TransactionInterceptor ti = new TransactionInterceptor();
	ti.setTransactionManager(ptm);
	ti.setTransactionAttributeSources(tas);

	ProxyFactory pf = new ProxyFactory(target);
	pf.addAdvice(0, ti);
	return pf.getProxy();
}
 
Example #11
Source File: AbstractReactiveTransactionAspectTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Simulate a transaction infrastructure failure.
 * Shouldn't invoke target method.
 */
@Test
public void cannotCreateTransaction() throws Exception {
	TransactionAttribute txatt = new DefaultTransactionAttribute();

	Method m = getNameMethod;
	MapTransactionAttributeSource tas = new MapTransactionAttributeSource();
	tas.register(m, txatt);

	ReactiveTransactionManager rtm = mock(ReactiveTransactionManager.class);
	// Expect a transaction
	CannotCreateTransactionException ex = new CannotCreateTransactionException("foobar", null);
	given(rtm.getReactiveTransaction(txatt)).willThrow(ex);

	DefaultTestBean tb = new DefaultTestBean() {
		@Override
		public Mono<String> getName() {
			throw new UnsupportedOperationException(
					"Shouldn't have invoked target method when couldn't create transaction for transactional method");
		}
	};
	TestBean itb = (TestBean) advised(tb, rtm, tas);

	itb.getName()
			.as(StepVerifier::create)
			.expectError(CannotCreateTransactionException.class)
			.verify();
}
 
Example #12
Source File: AbstractReactiveTransactionAspectTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Simulate failure of the underlying transaction infrastructure to commit.
 * Check that the target method was invoked, but that the transaction
 * infrastructure exception was thrown to the client
 */
@Test
public void cannotCommitTransaction() throws Exception {
	TransactionAttribute txatt = new DefaultTransactionAttribute();

	Method m = setNameMethod;
	MapTransactionAttributeSource tas = new MapTransactionAttributeSource();
	tas.register(m, txatt);
	// Method m2 = getNameMethod;
	// No attributes for m2

	ReactiveTransactionManager rtm = mock(ReactiveTransactionManager.class);

	ReactiveTransaction status = mock(ReactiveTransaction.class);
	given(rtm.getReactiveTransaction(txatt)).willReturn(Mono.just(status));
	UnexpectedRollbackException ex = new UnexpectedRollbackException("foobar", null);
	given(rtm.commit(status)).willReturn(Mono.error(ex));
	given(rtm.rollback(status)).willReturn(Mono.empty());

	DefaultTestBean tb = new DefaultTestBean();
	TestBean itb = (TestBean) advised(tb, rtm, tas);

	String name = "new name";

	Mono.from(itb.setName(name))
			.as(StepVerifier::create)
			.consumeErrorWith(throwable -> {
				assertEquals(RuntimeException.class, throwable.getClass());
				assertEquals(ex, throwable.getCause());
			})
			.verify();

	// Should have invoked target and changed name

	itb.getName()
			.as(StepVerifier::create)
			.expectNext(name)
			.verifyComplete();
}
 
Example #13
Source File: TransactionAspectSupport.java    From spring-analysis-note with MIT License 5 votes vote down vote up
public ReactiveTransactionInfo(@Nullable ReactiveTransactionManager transactionManager,
		@Nullable TransactionAttribute transactionAttribute, String joinpointIdentification) {

	this.transactionManager = transactionManager;
	this.transactionAttribute = transactionAttribute;
	this.joinpointIdentification = joinpointIdentification;
}
 
Example #14
Source File: TransactionAspectSupport.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Nullable
private ReactiveTransactionManager asReactiveTransactionManager(@Nullable Object transactionManager) {
	if (transactionManager == null || transactionManager instanceof ReactiveTransactionManager) {
		return (ReactiveTransactionManager) transactionManager;
	}
	else {
		throw new IllegalStateException(
				"Specified transaction manager is not a ReactiveTransactionManager: " + transactionManager);
	}
}
 
Example #15
Source File: TransactionAspectSupport.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private ReactiveTransactionManager determineQualifiedTransactionManager(BeanFactory beanFactory, String qualifier) {
	ReactiveTransactionManager txManager = asReactiveTransactionManager(transactionManagerCache.get(qualifier));
	if (txManager == null) {
		txManager = BeanFactoryAnnotationUtils.qualifiedBeanOfType(
				beanFactory, ReactiveTransactionManager.class, qualifier);
		transactionManagerCache.putIfAbsent(qualifier, txManager);
	}
	return txManager;
}
 
Example #16
Source File: TransactionAspectSupport.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Nullable
private ReactiveTransactionManager determineTransactionManager(@Nullable TransactionAttribute txAttr) {
	// Do not attempt to lookup tx manager if no tx attributes are set
	if (txAttr == null || beanFactory == null) {
		return asReactiveTransactionManager(getTransactionManager());
	}

	String qualifier = txAttr.getQualifier();
	if (StringUtils.hasText(qualifier)) {
		return determineQualifiedTransactionManager(beanFactory, qualifier);
	}
	else if (StringUtils.hasText(transactionManagerBeanName)) {
		return determineQualifiedTransactionManager(beanFactory, transactionManagerBeanName);
	}
	else {
		ReactiveTransactionManager defaultTransactionManager = asReactiveTransactionManager(getTransactionManager());
		if (defaultTransactionManager == null) {
			defaultTransactionManager = asReactiveTransactionManager(
					transactionManagerCache.get(DEFAULT_TRANSACTION_MANAGER_KEY));
			if (defaultTransactionManager == null) {
				defaultTransactionManager = beanFactory.getBean(ReactiveTransactionManager.class);
				transactionManagerCache.putIfAbsent(
						DEFAULT_TRANSACTION_MANAGER_KEY, defaultTransactionManager);
			}
		}
		return defaultTransactionManager;
	}
}
 
Example #17
Source File: ReactiveDataNeo4jTestIT.java    From sdn-rx with Apache License 2.0 5 votes vote down vote up
@Test
void didProvideOnlyReactiveTransactionManager() {

	assertThat(this.applicationContext.getBean(ReactiveTransactionManager.class)).isInstanceOf(
		ReactiveNeo4jTransactionManager.class);
	assertThatExceptionOfType(NoSuchBeanDefinitionException.class)
		.isThrownBy(() -> this.applicationContext.getBean(PlatformTransactionManager.class));
}
 
Example #18
Source File: Neo4jDataAutoConfigurationTest.java    From sdn-rx with Apache License 2.0 5 votes vote down vote up
@Test
@DisplayName("…should honour existing transaction manager")
void shouldHonourExisting() {
	contextRunner
		.withUserConfiguration(ConfigurationWithExistingReactiveTransactionManager.class)
		.run(ctx -> assertThat(ctx)
			.hasSingleBean(ReactiveTransactionManager.class)
			.hasBean("myCustomReactiveTransactionManager")
		);
}
 
Example #19
Source File: Neo4jDataAutoConfigurationTest.java    From sdn-rx with Apache License 2.0 5 votes vote down vote up
@Test
@DisplayName("Should require all needed classes")
void shouldRequireAllNeededClasses() {
	contextRunner
		.withClassLoader(
			new FilteredClassLoader(ReactiveNeo4jTransactionManager.class, ReactiveTransactionManager.class, Flux.class))
		.run(ctx -> assertThat(ctx)
			.doesNotHaveBean(ReactiveNeo4jClient.class)
			.doesNotHaveBean(ReactiveNeo4jTemplate.class)
			.doesNotHaveBean(ReactiveNeo4jTransactionManager.class)
		);
}
 
Example #20
Source File: Neo4jReactiveDataConfiguration.java    From sdn-rx with Apache License 2.0 5 votes vote down vote up
@Bean(ReactiveNeo4jRepositoryConfigurationExtension.DEFAULT_TRANSACTION_MANAGER_BEAN_NAME)
@ConditionalOnMissingBean(ReactiveTransactionManager.class)
public ReactiveTransactionManager transactionManager(Driver driver,
	ReactiveDatabaseSelectionProvider databaseNameProvider) {

	return new ReactiveNeo4jTransactionManager(driver, databaseNameProvider);
}
 
Example #21
Source File: ConnectionFactoryConfig.java    From spring-data-examples with Apache License 2.0 4 votes vote down vote up
@Bean
public ReactiveTransactionManager transactionManager(ConnectionFactory connectionFactory) {
  return new R2dbcTransactionManager(connectionFactory);
}
 
Example #22
Source File: AbstractReactiveTransactionAspectTests.java    From spring-analysis-note with MIT License 4 votes vote down vote up
protected Object advised(
		Object target, ReactiveTransactionManager rtm, TransactionAttributeSource[] tas) throws Exception {

	return advised(target, rtm, new CompositeTransactionAttributeSource(tas));
}
 
Example #23
Source File: DatabaseConfig.java    From spring-reactive-sample with GNU General Public License v3.0 4 votes vote down vote up
@Bean
ReactiveTransactionManager transactionManager(ConnectionFactory connectionFactory) {
    return new R2dbcTransactionManager(connectionFactory);
}
 
Example #24
Source File: AbstractReactiveTransactionAspectTests.java    From spring-analysis-note with MIT License 4 votes vote down vote up
/**
 * Check that the when exception thrown by the target can produce the
 * desired behavior with the appropriate transaction attribute.
 * @param ex exception to be thrown by the target
 * @param shouldRollback whether this should cause a transaction rollback
 */
@SuppressWarnings("serial")
protected void doTestRollbackOnException(
		final Exception ex, final boolean shouldRollback, boolean rollbackException) throws Exception {

	TransactionAttribute txatt = new DefaultTransactionAttribute() {
		@Override
		public boolean rollbackOn(Throwable t) {
			assertThat(t).isSameAs(ex);
			return shouldRollback;
		}
	};

	Method m = exceptionalMethod;
	MapTransactionAttributeSource tas = new MapTransactionAttributeSource();
	tas.register(m, txatt);

	ReactiveTransaction status = mock(ReactiveTransaction.class);
	ReactiveTransactionManager rtm = mock(ReactiveTransactionManager.class);
	// Gets additional call(s) from TransactionControl

	given(rtm.getReactiveTransaction(txatt)).willReturn(Mono.just(status));

	TransactionSystemException tex = new TransactionSystemException("system exception");
	if (rollbackException) {
		if (shouldRollback) {
			given(rtm.rollback(status)).willReturn(Mono.error(tex));
		}
		else {
			given(rtm.commit(status)).willReturn(Mono.error(tex));
		}
	}
	else {
		given(rtm.commit(status)).willReturn(Mono.empty());
		given(rtm.rollback(status)).willReturn(Mono.empty());
	}

	DefaultTestBean tb = new DefaultTestBean();
	TestBean itb = (TestBean) advised(tb, rtm, tas);

	itb.exceptional(ex)
			.as(StepVerifier::create)
			.expectErrorSatisfies(actual -> {
				if (rollbackException) {
					assertThat(actual).isEqualTo(tex);
				}
				else {
					assertThat(actual).isEqualTo(ex);
				}
			}).verify();

	if (!rollbackException) {
		if (shouldRollback) {
			verify(rtm).rollback(status);
		}
		else {
			verify(rtm).commit(status);
		}
	}
}
 
Example #25
Source File: DatabaseConfig.java    From spring-reactive-sample with GNU General Public License v3.0 4 votes vote down vote up
@Bean
ReactiveTransactionManager transactionManager(ConnectionFactory connectionFactory) {
    return new R2dbcTransactionManager(connectionFactory);
}
 
Example #26
Source File: ReactiveManagedTransitionServiceTests.java    From spring-data-examples with Apache License 2.0 4 votes vote down vote up
@Bean
ReactiveTransactionManager transactionManager(ReactiveMongoDatabaseFactory dbFactory) {
	return new ReactiveMongoTransactionManager(dbFactory);
}
 
Example #27
Source File: TransactionalOperatorImpl.java    From spring-analysis-note with MIT License 4 votes vote down vote up
/**
 * Return the transaction management strategy to be used.
 */
public ReactiveTransactionManager getTransactionManager() {
	return this.transactionManager;
}
 
Example #28
Source File: TransactionAspectSupport.java    From spring-analysis-note with MIT License 4 votes vote down vote up
public ReactiveTransactionManager getTransactionManager() {
	Assert.state(this.transactionManager != null, "No ReactiveTransactionManager set");
	return this.transactionManager;
}
 
Example #29
Source File: ReactiveRepositoryIT.java    From sdn-rx with Apache License 2.0 4 votes vote down vote up
ReactiveTransactionManager getTransactionManager() {
	return transactionManager;
}
 
Example #30
Source File: ReactiveCallbacksIT.java    From sdn-rx with Apache License 2.0 4 votes vote down vote up
@Autowired ReactiveCallbacksIT(Driver driver, ReactiveTransactionManager transactionManager) {

		super(driver);
		this.transactionManager = transactionManager;
	}