Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright 2002-2023 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.transaction.reactive;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import reactor.core.publisher.Mono;
import org.springframework.core.annotation.AnnotationAwareOrderComparator;
import org.springframework.lang.Nullable;
import org.springframework.transaction.NoTransactionException;
import org.springframework.util.Assert;
/**
* Central delegate that manages resources and transaction synchronizations per
* subscriber context. To be used by resource management code but not by typical
* application code.
*
*
Supports one resource per key without overwriting, that is, a resource needs
* to be removed before a new one can be set for the same key.
* Supports a list of transaction synchronizations if synchronization is active.
*
*
Resource management code should check for context-bound resources, e.g.
* database connections, via {@code getResource}. Such code is normally not
* supposed to bind resources to units of work, as this is the responsibility
* of transaction managers. A further option is to lazily bind on first use if
* transaction synchronization is active, for performing transactions that span
* an arbitrary number of resources.
*
*
Transaction synchronization must be activated and deactivated by a transaction
* manager via {@link #initSynchronization()} and {@link #clearSynchronization()}.
* This is automatically supported by {@link AbstractReactiveTransactionManager},
* and thus by all standard Spring transaction managers.
*
*
Resource management code should only register synchronizations when this
* manager is active, which can be checked via {@link #isSynchronizationActive};
* it should perform immediate resource cleanup else. If transaction synchronization
* isn't active, there is either no current transaction, or the transaction manager
* doesn't support transaction synchronization.
*
*
Synchronization is for example used to always return the same resources within
* a transaction, e.g. a database connection for any given connection factory.
*
* @author Mark Paluch
* @author Juergen Hoeller
* @since 5.2
* @see #isSynchronizationActive
* @see #registerSynchronization
* @see TransactionSynchronization
*/
public class TransactionSynchronizationManager {
private final TransactionContext transactionContext;
public TransactionSynchronizationManager(TransactionContext transactionContext) {
Assert.notNull(transactionContext, "TransactionContext must not be null");
this.transactionContext = transactionContext;
}
/**
* Get the {@link TransactionSynchronizationManager} that is associated with
* the current transaction context.
*
Mainly intended for code that wants to bind resources or synchronizations.
* @throws NoTransactionException if the transaction info cannot be found —
* for example, because the method was invoked outside a managed transaction
*/
public static Mono forCurrentTransaction() {
return TransactionContextManager.currentContext().map(TransactionSynchronizationManager::new);
}
/**
* Check if there is a resource for the given key bound to the current context.
* @param key the key to check (usually the resource factory)
* @return if there is a value bound to the current context
*/
public boolean hasResource(Object key) {
Object actualKey = TransactionSynchronizationUtils.unwrapResourceIfNecessary(key);
Object value = doGetResource(actualKey);
return (value != null);
}
/**
* Retrieve a resource for the given key that is bound to the current context.
* @param key the key to check (usually the resource factory)
* @return a value bound to the current context (usually the active
* resource object), or {@code null} if none
*/
@Nullable
public Object getResource(Object key) {
Object actualKey = TransactionSynchronizationUtils.unwrapResourceIfNecessary(key);
return doGetResource(actualKey);
}
/**
* Actually check the value of the resource that is bound for the given key.
*/
@Nullable
private Object doGetResource(Object actualKey) {
return this.transactionContext.getResources().get(actualKey);
}
/**
* Bind the given resource for the given key to the current context.
* @param key the key to bind the value to (usually the resource factory)
* @param value the value to bind (usually the active resource object)
* @throws IllegalStateException if there is already a value bound to the context
*/
public void bindResource(Object key, Object value) throws IllegalStateException {
Object actualKey = TransactionSynchronizationUtils.unwrapResourceIfNecessary(key);
Assert.notNull(value, "Value must not be null");
Map