HHH-12440 - Manage the SessionFactory's UUID on SessionFactoryOptions - wider availability

This commit is contained in:
Steve Ebersole 2018-03-28 14:37:31 -05:00
parent 9ba05c1e6b
commit 048f142351
5 changed files with 50 additions and 21 deletions

View File

@ -35,15 +35,11 @@ import org.hibernate.resource.jdbc.spi.StatementInspector;
import org.hibernate.tuple.entity.EntityTuplizer;
import org.hibernate.tuple.entity.EntityTuplizerFactory;
import org.jboss.logging.Logger;
/**
* @author Gail Badner
* @author Steve Ebersole
*/
public class SessionFactoryBuilderImpl implements SessionFactoryBuilderImplementor {
private static final Logger log = Logger.getLogger( SessionFactoryBuilderImpl.class );
private final MetadataImplementor metadata;
private final SessionFactoryOptionsBuilder optionsBuilder;

View File

@ -15,6 +15,7 @@ import java.util.Map;
import java.util.TimeZone;
import java.util.function.Supplier;
import org.hibernate.AssertionFailure;
import org.hibernate.ConnectionAcquisitionMode;
import org.hibernate.ConnectionReleaseMode;
import org.hibernate.CustomEntityDirtinessStrategy;
@ -44,6 +45,8 @@ import org.hibernate.engine.config.spi.ConfigurationService;
import org.hibernate.engine.jdbc.env.spi.ExtractedDatabaseMetaData;
import org.hibernate.engine.jdbc.spi.JdbcServices;
import org.hibernate.hql.spi.id.MultiTableBulkIdStrategy;
import org.hibernate.id.IdentifierGenerator;
import org.hibernate.id.UUIDGenerator;
import org.hibernate.internal.log.DeprecationLogger;
import org.hibernate.internal.util.config.ConfigurationHelper;
import org.hibernate.jpa.JpaCompliance;
@ -132,6 +135,9 @@ import static org.hibernate.jpa.AvailableSettings.DISCARD_PC_ON_CLOSE;
public class SessionFactoryOptionsBuilder implements SessionFactoryOptions {
private static final Logger log = Logger.getLogger( SessionFactoryOptionsBuilder.class );
private static final IdentifierGenerator UUID_GENERATOR = UUIDGenerator.buildSessionFactoryUniqueIdentifierGenerator();
private final String uuid;
private final StandardServiceRegistry serviceRegistry;
// integration
@ -230,6 +236,13 @@ public class SessionFactoryOptionsBuilder implements SessionFactoryOptions {
@SuppressWarnings({"WeakerAccess", "deprecation"})
public SessionFactoryOptionsBuilder(StandardServiceRegistry serviceRegistry) {
try {
uuid = (String) UUID_GENERATOR.generate( null, null );
}
catch (Exception e) {
throw new AssertionFailure( "Could not generate UUID");
}
this.serviceRegistry = serviceRegistry;
final StrategySelector strategySelector = serviceRegistry.getService( StrategySelector.class );
@ -593,6 +606,11 @@ public class SessionFactoryOptionsBuilder implements SessionFactoryOptions {
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// SessionFactoryOptionsState
@Override
public String getUuid() {
return this.uuid;
}
@Override
public StandardServiceRegistry getServiceRegistry() {
return serviceRegistry;

View File

@ -51,6 +51,11 @@ public class AbstractDelegatingSessionFactoryOptions implements SessionFactoryOp
return delegate;
}
@Override
public String getUuid() {
return delegate().getUuid();
}
@Override
public StandardServiceRegistry getServiceRegistry() {
return delegate.getServiceRegistry();

View File

@ -26,6 +26,7 @@ import org.hibernate.cache.spi.TimestampsCacheFactory;
import org.hibernate.cfg.BaselineSessionEventsListenerBuilder;
import org.hibernate.context.spi.CurrentTenantIdentifierResolver;
import org.hibernate.dialect.function.SQLFunction;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.hql.spi.id.MultiTableBulkIdStrategy;
import org.hibernate.jpa.JpaCompliance;
import org.hibernate.loader.BatchFetchStyle;
@ -41,6 +42,20 @@ import org.hibernate.tuple.entity.EntityTuplizerFactory;
* @since 5.0
*/
public interface SessionFactoryOptions {
/**
* Get the UUID unique to this SessionFactoryOptions. Will be the
* same value available as {@link SessionFactoryImplementor#getUuid()}.
*
* @apiNote The value is generated as a {@link java.util.UUID}, but kept
* as a String.
*
* @return The UUID for this SessionFactory.
*
* @see org.hibernate.internal.SessionFactoryRegistry#getSessionFactory
* @see SessionFactoryImplementor#getUuid
*/
String getUuid();
/**
* The service registry to use in building the factory.
*

View File

@ -31,7 +31,6 @@ import javax.persistence.SynchronizationType;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.spi.PersistenceUnitTransactionType;
import org.hibernate.AssertionFailure;
import org.hibernate.ConnectionAcquisitionMode;
import org.hibernate.ConnectionReleaseMode;
import org.hibernate.CustomEntityDirtinessStrategy;
@ -87,7 +86,6 @@ import org.hibernate.event.service.spi.EventListenerGroup;
import org.hibernate.event.service.spi.EventListenerRegistry;
import org.hibernate.event.spi.EventType;
import org.hibernate.id.IdentifierGenerator;
import org.hibernate.id.UUIDGenerator;
import org.hibernate.id.factory.IdentifierGeneratorFactory;
import org.hibernate.integrator.spi.Integrator;
import org.hibernate.integrator.spi.IntegratorService;
@ -155,10 +153,9 @@ import static org.hibernate.metamodel.internal.JpaMetaModelPopulationSetting.det
public final class SessionFactoryImpl implements SessionFactoryImplementor {
private static final CoreMessageLogger LOG = CoreLogging.messageLogger( SessionFactoryImpl.class );
private static final IdentifierGenerator UUID_GENERATOR = UUIDGenerator.buildSessionFactoryUniqueIdentifierGenerator();
private final String name;
private final String uuid;
private transient boolean isClosed;
private final transient SessionFactoryObserverChain observer = new SessionFactoryObserverChain();
@ -216,12 +213,7 @@ public final class SessionFactoryImpl implements SessionFactoryImplementor {
}
this.name = sfName;
try {
uuid = (String) UUID_GENERATOR.generate( null, null );
}
catch (Exception e) {
throw new AssertionFailure("Could not generate UUID");
}
this.uuid = options.getUuid();
final JdbcServices jdbcServices = serviceRegistry.getService( JdbcServices.class );
@ -374,7 +366,7 @@ public final class SessionFactoryImpl implements SessionFactoryImplementor {
this.observer.sessionFactoryCreated( this );
SessionFactoryRegistry.INSTANCE.addSessionFactory(
uuid,
getUuid(),
name,
settings.isSessionFactoryNameAlsoJndiName(),
this,
@ -538,7 +530,10 @@ public final class SessionFactoryImpl implements SessionFactoryImplementor {
return new DeserializationResolver() {
@Override
public SessionFactoryImplementor resolve() {
return (SessionFactoryImplementor) SessionFactoryRegistry.INSTANCE.findSessionFactory( uuid, name );
return (SessionFactoryImplementor) SessionFactoryRegistry.INSTANCE.findSessionFactory(
uuid,
name
);
}
};
}
@ -770,7 +765,7 @@ public final class SessionFactoryImpl implements SessionFactoryImplementor {
}
SessionFactoryRegistry.INSTANCE.removeSessionFactory(
uuid,
getUuid(),
name,
settings.isSessionFactoryNameAlsoJndiName(),
serviceRegistry.getService( JndiService.class )
@ -1504,7 +1499,7 @@ public final class SessionFactoryImpl implements SessionFactoryImplementor {
* @throws IOException Can be thrown by the stream
*/
private void writeObject(ObjectOutputStream out) throws IOException {
LOG.debugf( "Serializing: %s", uuid );
LOG.debugf( "Serializing: %s", getUuid() );
out.defaultWriteObject();
LOG.trace( "Serialized" );
}
@ -1520,7 +1515,7 @@ public final class SessionFactoryImpl implements SessionFactoryImplementor {
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
LOG.trace( "Deserializing" );
in.defaultReadObject();
LOG.debugf( "Deserialized: %s", uuid );
LOG.debugf( "Deserialized: %s", getUuid() );
}
/**
@ -1534,7 +1529,7 @@ public final class SessionFactoryImpl implements SessionFactoryImplementor {
*/
private Object readResolve() throws InvalidObjectException {
LOG.trace( "Resolving serialized SessionFactory" );
return locateSessionFactoryOnDeserialization( uuid, name );
return locateSessionFactoryOnDeserialization( getUuid(), name );
}
private static SessionFactory locateSessionFactoryOnDeserialization(String uuid, String name) throws InvalidObjectException{
@ -1564,7 +1559,7 @@ public final class SessionFactoryImpl implements SessionFactoryImplementor {
* @throws IOException Indicates problems writing out the serial data stream
*/
void serialize(ObjectOutputStream oos) throws IOException {
oos.writeUTF( uuid );
oos.writeUTF( getUuid() );
oos.writeBoolean( name != null );
if ( name != null ) {
oos.writeUTF( name );