liquibase.ThreadLocalScopeManager Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of liquibase-core Show documentation
Show all versions of liquibase-core Show documentation
Liquibase is a tool for managing and executing database changes.
package liquibase;
/**
* An alternative to {@link SingletonScopeManager} which manages a separate Scope per thread.
* Integrations that would prefer to use this scope manager can call Scope.setScopeManager(new ThreadLocalScopeManager())
.
*
* The value of Scope.getCurrentScope() at the time of the ThreadLocalScopeManger's creation will be the basis of all scopes created after setScopeManager() is changed,
* so you will generally want to setScopeManager as soon as possible.
*/
@SuppressWarnings("java:S5164")
public class ThreadLocalScopeManager extends ScopeManager {
private final Scope rootScope;
private final ThreadLocal threadLocalScopes = new ThreadLocal<>();
public ThreadLocalScopeManager() {
this(Scope.getCurrentScope());
}
public ThreadLocalScopeManager(Scope rootScope) {
this.rootScope = rootScope;
}
@Override
public synchronized Scope getCurrentScope() {
Scope current = threadLocalScopes.get();
if (current == null) {
threadLocalScopes.set(rootScope);
current = rootScope;
}
return current;
}
@Override
protected synchronized void setCurrentScope(Scope scope) {
this.threadLocalScopes.set(scope);
}
@Override
protected Scope init(Scope scope) throws Exception {
return rootScope;
}
}