org.springframework.transaction.reactive.TransactionSynchronizationManager Java Examples

The following examples show how to use org.springframework.transaction.reactive.TransactionSynchronizationManager. 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: ReactiveRedissonTransactionManager.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Override
protected Mono<Void> doBegin(TransactionSynchronizationManager synchronizationManager, Object transaction, TransactionDefinition definition) throws TransactionException {
    ReactiveRedissonTransactionObject tObject = (ReactiveRedissonTransactionObject) transaction;

    TransactionOptions options = TransactionOptions.defaults();
    if (definition.getTimeout() != TransactionDefinition.TIMEOUT_DEFAULT) {
        options.timeout(definition.getTimeout(), TimeUnit.SECONDS);
    }

    RTransactionReactive trans = redissonClient.createTransaction(options);
    ReactiveRedissonResourceHolder holder = new ReactiveRedissonResourceHolder();
    holder.setTransaction(trans);
    tObject.setResourceHolder(holder);
    synchronizationManager.bindResource(redissonClient, holder);

    return Mono.empty();
}
 
Example #2
Source File: ReactiveNeo4jTransactionManagerTest.java    From sdn-rx with Apache License 2.0 6 votes vote down vote up
@Test
void shouldUseTxFromNeo4jTxManager() {

	ReactiveNeo4jTransactionManager txManager = new ReactiveNeo4jTransactionManager(driver, ReactiveDatabaseSelectionProvider
		.createStaticDatabaseSelectionProvider(databaseName));
	TransactionalOperator transactionalOperator = TransactionalOperator.create(txManager);

	transactionalOperator
		.execute(transactionStatus -> TransactionSynchronizationManager
			.forCurrentTransaction().doOnNext(tsm -> {
				assertThat(tsm.hasResource(driver)).isTrue();
				transactionStatus.setRollbackOnly();
			}).then(retrieveReactiveTransaction(driver, databaseName))
		)
		.as(StepVerifier::create)
		.expectNextCount(1L)
		.verifyComplete();

	verify(driver).rxSession(any(SessionConfig.class));

	verify(session).beginTransaction(any(TransactionConfig.class));
	verify(session).close();
	verify(transaction).rollback();
	verify(transaction, never()).commit();
}
 
Example #3
Source File: ReactiveFirestoreTransactionManager.java    From spring-cloud-gcp with Apache License 2.0 6 votes vote down vote up
@Override
protected Mono<Void> doCommit(TransactionSynchronizationManager transactionSynchronizationManager,
		GenericReactiveTransaction genericReactiveTransaction) throws TransactionException {
	return Mono.defer(() -> {
		ReactiveFirestoreResourceHolder resourceHolder = extractFirestoreTransaction(genericReactiveTransaction)
				.getResourceHolder();

		CommitRequest.Builder builder = CommitRequest.newBuilder()
				.setDatabase(this.databasePath)
				.setTransaction(resourceHolder.getTransactionId());

		resourceHolder.getWrites().forEach(builder::addWrites);

		return ObservableReactiveUtil
				.<CommitResponse>unaryCall(obs -> this.firestore.commit(builder.build(), obs))
				.then();
	});
}
 
Example #4
Source File: ReactiveNeo4jTransactionManagerTest.java    From sdn-rx with Apache License 2.0 6 votes vote down vote up
@Test
void shouldSynchronizeWithExternalWithCommit() {

	R2dbcTransactionManager t = new R2dbcTransactionManager(new H2ConnectionFactory(
		H2ConnectionConfiguration.builder().inMemory("test").build()));

	TransactionalOperator transactionalOperator = TransactionalOperator.create(t);

	transactionalOperator
		.execute(transactionStatus -> TransactionSynchronizationManager
			.forCurrentTransaction().doOnNext(tsm -> assertThat(tsm.hasResource(driver)).isFalse())
			.then(retrieveReactiveTransaction(driver, databaseName))
			.flatMap(ignoredNativeTx -> TransactionSynchronizationManager.forCurrentTransaction()
				.doOnNext(tsm -> assertThat(tsm.hasResource(driver)).isTrue()))
		)
		.as(StepVerifier::create)
		.expectNextCount(1L)
		.verifyComplete();

	verify(driver).rxSession(any(SessionConfig.class));

	verify(session).beginTransaction(any(TransactionConfig.class));
	verify(session).close();
	verify(transaction).commit();
	verify(transaction, never()).rollback();
}
 
Example #5
Source File: ReactiveNeo4jTransactionManagerTest.java    From sdn-rx with Apache License 2.0 5 votes vote down vote up
@Test
void shouldSynchronizeWithExternalWithRollback() {

	R2dbcTransactionManager t = new R2dbcTransactionManager(new H2ConnectionFactory(
		H2ConnectionConfiguration.builder().inMemory("test").build()));

	TransactionalOperator transactionalOperator = TransactionalOperator.create(t);

	transactionalOperator
		.execute(transactionStatus -> TransactionSynchronizationManager
			.forCurrentTransaction()
			.doOnNext(tsm -> {
				assertThat(tsm.hasResource(driver)).isFalse();
				transactionStatus.setRollbackOnly();
			})
			.then(retrieveReactiveTransaction(driver, databaseName))
			.flatMap(ignoredNativeTx -> TransactionSynchronizationManager.forCurrentTransaction()
				.doOnNext(tsm -> assertThat(tsm.hasResource(driver)).isTrue()))
		)
		.as(StepVerifier::create)
		.expectNextCount(1L)
		.verifyComplete();

	verify(driver).rxSession(any(SessionConfig.class));

	verify(session).beginTransaction(any(TransactionConfig.class));
	verify(session).close();
	verify(transaction).rollback();
	verify(transaction, never()).commit();
}
 
Example #6
Source File: ReactiveRedissonTransactionManager.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Override
protected Mono<Void> doCleanupAfterCompletion(TransactionSynchronizationManager synchronizationManager, Object transaction) {
    return Mono.fromRunnable(() -> {
        synchronizationManager.unbindResource(redissonClient);
        ReactiveRedissonTransactionObject to = (ReactiveRedissonTransactionObject) transaction;
        to.getResourceHolder().setTransaction(null);
    });
}
 
Example #7
Source File: ReactiveRedissonTransactionManager.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Override
protected Mono<Void> doSetRollbackOnly(TransactionSynchronizationManager synchronizationManager, GenericReactiveTransaction status) throws TransactionException {
    return Mono.fromRunnable(() -> {
        ReactiveRedissonTransactionObject to = (ReactiveRedissonTransactionObject) status.getTransaction();
        to.getResourceHolder().setRollbackOnly();
    });
}
 
Example #8
Source File: ReactiveRedissonTransactionManager.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Override
protected Mono<Object> doSuspend(TransactionSynchronizationManager synchronizationManager, Object transaction) throws TransactionException {
    return Mono.fromSupplier(() -> {
        ReactiveRedissonTransactionObject to = (ReactiveRedissonTransactionObject) transaction;
        to.setResourceHolder(null);
        return synchronizationManager.unbindResource(redissonClient);
    });
}
 
Example #9
Source File: ReactiveRedissonTransactionManager.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Override
protected Mono<Void> doRollback(TransactionSynchronizationManager synchronizationManager, GenericReactiveTransaction status) throws TransactionException {
    ReactiveRedissonTransactionObject to = (ReactiveRedissonTransactionObject) status.getTransaction();
    return to.getResourceHolder().getTransaction().rollback().onErrorMap(ex -> {
        return new TransactionSystemException("Unable to rollback transaction", ex);
    });
}
 
Example #10
Source File: ReactiveRedissonTransactionManager.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Override
protected Mono<Void> doCommit(TransactionSynchronizationManager synchronizationManager, GenericReactiveTransaction status) throws TransactionException {
    ReactiveRedissonTransactionObject to = (ReactiveRedissonTransactionObject) status.getTransaction();
    return to.getResourceHolder().getTransaction().commit().onErrorMap(ex -> {
        return new TransactionSystemException("Unable to commit transaction " + to.getResourceHolder().getTransaction(), ex);
    });
}
 
Example #11
Source File: ReactiveRedissonTransactionManager.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Override
protected Object doGetTransaction(TransactionSynchronizationManager synchronizationManager) throws TransactionException {
    ReactiveRedissonTransactionObject transactionObject = new ReactiveRedissonTransactionObject();

    ReactiveRedissonResourceHolder holder = (ReactiveRedissonResourceHolder) synchronizationManager.getResource(redissonClient);
    transactionObject.setResourceHolder(holder);
    return transactionObject;
}
 
Example #12
Source File: ReactiveRedissonTransactionManager.java    From redisson with Apache License 2.0 5 votes vote down vote up
public Mono<RTransactionReactive> getCurrentTransaction() {
    return TransactionSynchronizationManager.forCurrentTransaction().map(manager -> {
        ReactiveRedissonResourceHolder holder = (ReactiveRedissonResourceHolder) manager.getResource(redissonClient);
        if (holder == null) {
            throw new NoTransactionException("No transaction is available for the current thread");
        } else {
            return holder.getTransaction();
        }
    });
}
 
Example #13
Source File: ReactiveFirestoreTransactionManager.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
@Override
protected Mono<Void> doRollback(TransactionSynchronizationManager transactionSynchronizationManager,
		GenericReactiveTransaction genericReactiveTransaction) throws TransactionException {

	return ObservableReactiveUtil
			.<Empty>unaryCall(
					obs -> this.firestore.rollback(RollbackRequest.newBuilder()
							.setTransaction(
									extractFirestoreTransaction(genericReactiveTransaction).getTransactionId())
							.setDatabase(this.databasePath)
							.build(), obs))
			.then();
}
 
Example #14
Source File: ReactiveFirestoreTransactionManager.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
@Override
protected Mono<Void> doBegin(TransactionSynchronizationManager synchronizationManager, Object transactionObject,
		TransactionDefinition transactionDefinition) throws TransactionException {
	return Mono.defer(() -> {
		Mono<ReactiveFirestoreResourceHolder> holder = startTransaction(transactionDefinition);

		return holder.doOnNext(extractFirestoreTransaction(transactionObject)::setResourceHolder)
				.onErrorMap(
						ex -> new TransactionSystemException("Could not start Firestore transaction", ex))
				.doOnSuccess(resourceHolder -> {
					synchronizationManager.bindResource(this.firestore, resourceHolder);
				})
				.then();
	});
}
 
Example #15
Source File: ReactiveFirestoreTransactionManager.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
@Override
protected Object doGetTransaction(TransactionSynchronizationManager synchronizationManager)
		throws TransactionException {
	ReactiveFirestoreResourceHolder resourceHolder = (ReactiveFirestoreResourceHolder) synchronizationManager
			.getResource(this.firestore);
	return new ReactiveFirestoreTransactionObject(resourceHolder);
}
 
Example #16
Source File: ReactiveNeo4jTransactionManagerTest.java    From sdn-rx with Apache License 2.0 5 votes vote down vote up
@Test
void usesBookmarksCorrectly() throws Exception {

	ReactiveNeo4jTransactionManager txManager = new ReactiveNeo4jTransactionManager(driver, ReactiveDatabaseSelectionProvider
		.createStaticDatabaseSelectionProvider(databaseName));

	Neo4jBookmarkManager bookmarkManager = spy(new Neo4jBookmarkManager());
	injectBookmarkManager(txManager, bookmarkManager);

	Bookmark bookmark = new Bookmark() {
		@Override public Set<String> values() {
			return Collections.singleton("blubb");
		}

		@Override public boolean isEmpty() {
			return false;
		}
	};
	when(session.lastBookmark()).thenReturn(bookmark);

	TransactionalOperator transactionalOperator = TransactionalOperator.create(txManager);

	transactionalOperator
		.execute(transactionStatus -> TransactionSynchronizationManager
			.forCurrentTransaction()
			.doOnNext(tsm -> assertThat(tsm.hasResource(driver)).isTrue())
			.then(retrieveReactiveTransaction(driver, databaseName))
		)
		.as(StepVerifier::create)
		.expectNextCount(1L)
		.verifyComplete();

	verify(driver).rxSession(any(SessionConfig.class));
	verify(session).beginTransaction(any(TransactionConfig.class));
	verify(bookmarkManager).getBookmarks();
	verify(session).close();
	verify(transaction).commit();
	verify(bookmarkManager).updateBookmarks(anyCollection(), eq(bookmark));
}
 
Example #17
Source File: ReactiveNeo4jTransactionManager.java    From sdn-rx with Apache License 2.0 5 votes vote down vote up
@Override
protected Mono<Void> doSetRollbackOnly(TransactionSynchronizationManager synchronizationManager,
	GenericReactiveTransaction genericReactiveTransaction) throws TransactionException {

	return Mono.fromRunnable(() -> {
		ReactiveNeo4jTransactionObject transactionObject = extractNeo4jTransaction(genericReactiveTransaction);
		transactionObject.getRequiredResourceHolder().setRollbackOnly();
	});
}
 
Example #18
Source File: ReactiveNeo4jTransactionManager.java    From sdn-rx with Apache License 2.0 5 votes vote down vote up
@Override
protected Mono<Void> doResume(TransactionSynchronizationManager synchronizationManager, Object transaction, Object suspendedResources) throws TransactionException {

	return Mono
		.just(extractNeo4jTransaction(transaction))
		.doOnNext(r -> r.setResourceHolder((ReactiveNeo4jTransactionHolder) suspendedResources))
		.then(Mono.fromRunnable(() -> synchronizationManager.bindResource(driver, suspendedResources)));
}
 
Example #19
Source File: ReactiveNeo4jTransactionManager.java    From sdn-rx with Apache License 2.0 5 votes vote down vote up
@Override
protected Mono<Object> doSuspend(TransactionSynchronizationManager synchronizationManager, Object transaction) throws TransactionException {

	return Mono
		.just(extractNeo4jTransaction(transaction))
		.doOnNext(r -> r.setResourceHolder(null))
		.then(Mono.fromSupplier(() -> synchronizationManager.unbindResource(driver)));
}
 
Example #20
Source File: ReactiveNeo4jTransactionManager.java    From sdn-rx with Apache License 2.0 5 votes vote down vote up
@Override
protected Mono<Void> doRollback(TransactionSynchronizationManager transactionSynchronizationManager,
		GenericReactiveTransaction genericReactiveTransaction) throws TransactionException {

	ReactiveNeo4jTransactionHolder holder = extractNeo4jTransaction(genericReactiveTransaction)
		.getRequiredResourceHolder();
	return holder.rollback();
}
 
Example #21
Source File: ReactiveNeo4jTransactionManager.java    From sdn-rx with Apache License 2.0 5 votes vote down vote up
@Override
protected Mono<Void> doCommit(TransactionSynchronizationManager transactionSynchronizationManager,
		GenericReactiveTransaction genericReactiveTransaction) throws TransactionException {

	ReactiveNeo4jTransactionHolder holder = extractNeo4jTransaction(genericReactiveTransaction)
		.getRequiredResourceHolder();
	return holder.commit()
		.doOnNext(bookmark -> bookmarkManager.updateBookmarks(holder.getBookmarks(), bookmark))
		.then();
}
 
Example #22
Source File: ReactiveNeo4jTransactionManager.java    From sdn-rx with Apache License 2.0 5 votes vote down vote up
@Override
protected Mono<Void> doCleanupAfterCompletion(TransactionSynchronizationManager transactionSynchronizationManager,
	Object transaction) {

	return Mono
		.just(extractNeo4jTransaction(transaction))
		.map(r -> {
			ReactiveNeo4jTransactionHolder holder = r.getRequiredResourceHolder();
			r.setResourceHolder(null);
			return holder;
		})
		.flatMap(ReactiveNeo4jTransactionHolder::close)
		.then(Mono.fromRunnable(() -> transactionSynchronizationManager.unbindResource(driver)));
}
 
Example #23
Source File: ReactiveNeo4jTransactionManager.java    From sdn-rx with Apache License 2.0 5 votes vote down vote up
@Override
protected Mono<Void> doBegin(TransactionSynchronizationManager transactionSynchronizationManager, Object transaction,
		TransactionDefinition transactionDefinition) throws TransactionException {

	return Mono.defer(() -> {
		ReactiveNeo4jTransactionObject transactionObject = extractNeo4jTransaction(transaction);

		TransactionConfig transactionConfig = createTransactionConfigFrom(transactionDefinition);
		boolean readOnly = transactionDefinition.isReadOnly();

		transactionSynchronizationManager.setCurrentTransactionReadOnly(readOnly);

		return databaseSelectionProvider.getDatabaseSelection()
			.switchIfEmpty(Mono.just(DatabaseSelection.undecided()))
			.map(databaseName -> new Neo4jTransactionContext(databaseName.getValue(), bookmarkManager.getBookmarks()))
			.map(context -> Tuples.of(context, this.driver.rxSession(sessionConfig(readOnly, context.getBookmarks(), context.getDatabaseName()))))
			.flatMap(contextAndSession -> Mono
					.from(contextAndSession.getT2().beginTransaction(transactionConfig))
					.map(nativeTransaction -> new ReactiveNeo4jTransactionHolder(contextAndSession.getT1(), contextAndSession.getT2(), nativeTransaction))
			)
			.doOnNext(transactionHolder -> {
				transactionHolder.setSynchronizedWithTransaction(true);
				transactionObject.setResourceHolder(transactionHolder);
				transactionSynchronizationManager.bindResource(this.driver, transactionHolder);
			});

	}).then();
}
 
Example #24
Source File: ReactiveNeo4jTransactionManager.java    From sdn-rx with Apache License 2.0 5 votes vote down vote up
@Override
protected Object doGetTransaction(TransactionSynchronizationManager transactionSynchronizationManager)
		throws TransactionException {

	ReactiveNeo4jTransactionHolder resourceHolder = (ReactiveNeo4jTransactionHolder) transactionSynchronizationManager
			.getResource(driver);
	return new ReactiveNeo4jTransactionObject(resourceHolder);
}
 
Example #25
Source File: ReactiveRedissonTransactionManager.java    From redisson with Apache License 2.0 4 votes vote down vote up
@Override
protected Mono<Void> doResume(TransactionSynchronizationManager synchronizationManager, Object transaction, Object suspendedResources) throws TransactionException {
    return Mono.fromRunnable(() -> {
        synchronizationManager.bindResource(redissonClient, suspendedResources);
    });
}
 
Example #26
Source File: ReactiveNeo4jTransactionManager.java    From sdn-rx with Apache License 2.0 4 votes vote down vote up
public static Mono<RxTransaction> retrieveReactiveTransaction(final Driver driver, final String targetDatabase) {

		return TransactionSynchronizationManager.forCurrentTransaction() // Do we have a Transaction context?
			// Bail out early if synchronization between transaction managers is not active
			.filter(TransactionSynchronizationManager::isSynchronizationActive)
			.flatMap(tsm -> {
				// Get an existing holder
				ReactiveNeo4jTransactionHolder existingTxHolder = (ReactiveNeo4jTransactionHolder) tsm
					.getResource(driver);

				// And use it if there is any
				if (existingTxHolder != null) {
					return Mono.just(existingTxHolder);
				}

				// Otherwise open up a new native transaction
				return Mono.defer(() -> {
					RxSession session = driver.rxSession(defaultSessionConfig(targetDatabase));
					return Mono.from(session.beginTransaction(TransactionConfig.empty())).map(tx -> {

						ReactiveNeo4jTransactionHolder newConnectionHolder = new ReactiveNeo4jTransactionHolder(
							new Neo4jTransactionContext(targetDatabase), session, tx);
						newConnectionHolder.setSynchronizedWithTransaction(true);

						tsm.registerSynchronization(
							new ReactiveNeo4jSessionSynchronization(tsm, newConnectionHolder, driver));

						tsm.bindResource(driver, newConnectionHolder);
						return newConnectionHolder;
					});
				});
			})
			.map(connectionHolder -> {
					RxTransaction transaction = connectionHolder.getTransaction(targetDatabase);
					if (transaction == null) {
						throw new IllegalStateException(
							formatOngoingTxInAnotherDbErrorMessage(connectionHolder.getDatabaseName(), targetDatabase));
					}
					return transaction;
				}
			)
			// If not, than just don't open a transaction
			.onErrorResume(NoTransactionException.class, nte -> Mono.empty());
	}
 
Example #27
Source File: ReactiveNeo4jSessionSynchronization.java    From sdn-rx with Apache License 2.0 3 votes vote down vote up
ReactiveNeo4jSessionSynchronization(TransactionSynchronizationManager transactionSynchronizationManager,
	ReactiveNeo4jTransactionHolder transactionHolder, Driver driver) {

	super(transactionHolder, driver, transactionSynchronizationManager);

	this.transactionHolder = transactionHolder;
}