HHH-10427 Protect from double initialization and from concurrent destroy actions
This commit is contained in:
parent
b626f289b7
commit
f7174c9207
|
@ -11,6 +11,7 @@ import java.util.HashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.ListIterator;
|
import java.util.ListIterator;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
|
|
||||||
import org.hibernate.boot.registry.BootstrapServiceRegistry;
|
import org.hibernate.boot.registry.BootstrapServiceRegistry;
|
||||||
import org.hibernate.cfg.Environment;
|
import org.hibernate.cfg.Environment;
|
||||||
|
@ -65,6 +66,8 @@ public abstract class AbstractServiceRegistryImpl
|
||||||
// Guarded by synchronization on this.
|
// Guarded by synchronization on this.
|
||||||
private Set<ServiceRegistryImplementor> childRegistries;
|
private Set<ServiceRegistryImplementor> childRegistries;
|
||||||
|
|
||||||
|
private final AtomicBoolean active = new AtomicBoolean( true );
|
||||||
|
|
||||||
@SuppressWarnings( {"UnusedDeclaration"})
|
@SuppressWarnings( {"UnusedDeclaration"})
|
||||||
protected AbstractServiceRegistryImpl() {
|
protected AbstractServiceRegistryImpl() {
|
||||||
this( (ServiceRegistryImplementor) null );
|
this( (ServiceRegistryImplementor) null );
|
||||||
|
@ -187,11 +190,18 @@ public abstract class AbstractServiceRegistryImpl
|
||||||
return service;
|
return service;
|
||||||
}
|
}
|
||||||
|
|
||||||
final ServiceBinding<R> serviceBinding = locateServiceBinding( serviceRole );
|
//Any service initialization needs synchronization
|
||||||
if ( serviceBinding == null ) {
|
synchronized ( this ) {
|
||||||
throw new UnknownServiceException( serviceRole );
|
// Check again after having acquired the lock:
|
||||||
}
|
service = serviceRole.cast( initializedServiceByRole.get( serviceRole ) );
|
||||||
synchronized (this) {
|
if ( service != null ) {
|
||||||
|
return service;
|
||||||
|
}
|
||||||
|
|
||||||
|
final ServiceBinding<R> serviceBinding = locateServiceBinding( serviceRole );
|
||||||
|
if ( serviceBinding == null ) {
|
||||||
|
throw new UnknownServiceException( serviceRole );
|
||||||
|
}
|
||||||
service = serviceBinding.getService();
|
service = serviceBinding.getService();
|
||||||
if ( service == null ) {
|
if ( service == null ) {
|
||||||
service = initializeService( serviceBinding );
|
service = initializeService( serviceBinding );
|
||||||
|
@ -331,44 +341,38 @@ public abstract class AbstractServiceRegistryImpl
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean active = true;
|
|
||||||
|
|
||||||
public boolean isActive() {
|
public boolean isActive() {
|
||||||
return active;
|
return active.get();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@SuppressWarnings( {"unchecked"})
|
@SuppressWarnings( {"unchecked"})
|
||||||
public void destroy() {
|
public synchronized void destroy() {
|
||||||
if ( !active ) {
|
if ( active.compareAndSet( true, false ) ) {
|
||||||
return;
|
try {
|
||||||
}
|
//First thing, make sure that the fast path read is disabled so that
|
||||||
|
//threads not owning the synchronization lock can't get an invalid Service:
|
||||||
active = false;
|
initializedServiceByRole.clear();
|
||||||
try {
|
synchronized (serviceBindingList) {
|
||||||
synchronized (serviceBindingList) {
|
ListIterator<ServiceBinding> serviceBindingsIterator = serviceBindingList.listIterator(
|
||||||
ListIterator<ServiceBinding> serviceBindingsIterator = serviceBindingList.listIterator(
|
serviceBindingList.size()
|
||||||
serviceBindingList.size()
|
);
|
||||||
);
|
while ( serviceBindingsIterator.hasPrevious() ) {
|
||||||
while ( serviceBindingsIterator.hasPrevious() ) {
|
final ServiceBinding serviceBinding = serviceBindingsIterator.previous();
|
||||||
final ServiceBinding serviceBinding = serviceBindingsIterator.previous();
|
serviceBinding.getLifecycleOwner().stopService( serviceBinding );
|
||||||
serviceBinding.getLifecycleOwner().stopService( serviceBinding );
|
}
|
||||||
|
serviceBindingList.clear();
|
||||||
}
|
}
|
||||||
serviceBindingList.clear();
|
serviceBindingMap.clear();
|
||||||
|
} finally {
|
||||||
|
parent.deRegisterChild( this );
|
||||||
}
|
}
|
||||||
serviceBindingMap.clear();
|
|
||||||
initializedServiceByRole.clear();
|
|
||||||
}
|
|
||||||
finally {
|
|
||||||
parent.deRegisterChild( this );
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <R extends Service> void stopService(ServiceBinding<R> binding) {
|
public <R extends Service> void stopService(ServiceBinding<R> binding) {
|
||||||
final Service service = binding.getService();
|
final Service service = binding.getService();
|
||||||
// TODO: it would be nice to remove service from initializedServiceByRole,
|
|
||||||
// but ConcurrentServiceBinding does not allow removing an entry.
|
|
||||||
if ( Stoppable.class.isInstance( service ) ) {
|
if ( Stoppable.class.isInstance( service ) ) {
|
||||||
try {
|
try {
|
||||||
( (Stoppable) service ).stop();
|
( (Stoppable) service ).stop();
|
||||||
|
|
|
@ -31,7 +31,7 @@ public final class ServiceBinding<R extends Service> {
|
||||||
private final ServiceLifecycleOwner lifecycleOwner;
|
private final ServiceLifecycleOwner lifecycleOwner;
|
||||||
private final Class<R> serviceRole;
|
private final Class<R> serviceRole;
|
||||||
private final ServiceInitiator<R> serviceInitiator;
|
private final ServiceInitiator<R> serviceInitiator;
|
||||||
private R service;
|
private volatile R service;
|
||||||
|
|
||||||
public ServiceBinding(ServiceLifecycleOwner lifecycleOwner, Class<R> serviceRole, R service) {
|
public ServiceBinding(ServiceLifecycleOwner lifecycleOwner, Class<R> serviceRole, R service) {
|
||||||
this.lifecycleOwner = lifecycleOwner;
|
this.lifecycleOwner = lifecycleOwner;
|
||||||
|
|
Loading…
Reference in New Issue