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.EntityTuplizer;
import org.hibernate.tuple.entity.EntityTuplizerFactory; import org.hibernate.tuple.entity.EntityTuplizerFactory;
import org.jboss.logging.Logger;
/** /**
* @author Gail Badner * @author Gail Badner
* @author Steve Ebersole * @author Steve Ebersole
*/ */
public class SessionFactoryBuilderImpl implements SessionFactoryBuilderImplementor { public class SessionFactoryBuilderImpl implements SessionFactoryBuilderImplementor {
private static final Logger log = Logger.getLogger( SessionFactoryBuilderImpl.class );
private final MetadataImplementor metadata; private final MetadataImplementor metadata;
private final SessionFactoryOptionsBuilder optionsBuilder; private final SessionFactoryOptionsBuilder optionsBuilder;

View File

@ -15,6 +15,7 @@ import java.util.Map;
import java.util.TimeZone; import java.util.TimeZone;
import java.util.function.Supplier; import java.util.function.Supplier;
import org.hibernate.AssertionFailure;
import org.hibernate.ConnectionAcquisitionMode; import org.hibernate.ConnectionAcquisitionMode;
import org.hibernate.ConnectionReleaseMode; import org.hibernate.ConnectionReleaseMode;
import org.hibernate.CustomEntityDirtinessStrategy; 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.env.spi.ExtractedDatabaseMetaData;
import org.hibernate.engine.jdbc.spi.JdbcServices; import org.hibernate.engine.jdbc.spi.JdbcServices;
import org.hibernate.hql.spi.id.MultiTableBulkIdStrategy; 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.log.DeprecationLogger;
import org.hibernate.internal.util.config.ConfigurationHelper; import org.hibernate.internal.util.config.ConfigurationHelper;
import org.hibernate.jpa.JpaCompliance; import org.hibernate.jpa.JpaCompliance;
@ -132,6 +135,9 @@ import static org.hibernate.jpa.AvailableSettings.DISCARD_PC_ON_CLOSE;
public class SessionFactoryOptionsBuilder implements SessionFactoryOptions { public class SessionFactoryOptionsBuilder implements SessionFactoryOptions {
private static final Logger log = Logger.getLogger( SessionFactoryOptionsBuilder.class ); 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; private final StandardServiceRegistry serviceRegistry;
// integration // integration
@ -230,6 +236,13 @@ public class SessionFactoryOptionsBuilder implements SessionFactoryOptions {
@SuppressWarnings({"WeakerAccess", "deprecation"}) @SuppressWarnings({"WeakerAccess", "deprecation"})
public SessionFactoryOptionsBuilder(StandardServiceRegistry serviceRegistry) { 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; this.serviceRegistry = serviceRegistry;
final StrategySelector strategySelector = serviceRegistry.getService( StrategySelector.class ); final StrategySelector strategySelector = serviceRegistry.getService( StrategySelector.class );
@ -593,6 +606,11 @@ public class SessionFactoryOptionsBuilder implements SessionFactoryOptions {
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// SessionFactoryOptionsState // SessionFactoryOptionsState
@Override
public String getUuid() {
return this.uuid;
}
@Override @Override
public StandardServiceRegistry getServiceRegistry() { public StandardServiceRegistry getServiceRegistry() {
return serviceRegistry; return serviceRegistry;

View File

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

View File

@ -26,6 +26,7 @@ import org.hibernate.cache.spi.TimestampsCacheFactory;
import org.hibernate.cfg.BaselineSessionEventsListenerBuilder; import org.hibernate.cfg.BaselineSessionEventsListenerBuilder;
import org.hibernate.context.spi.CurrentTenantIdentifierResolver; import org.hibernate.context.spi.CurrentTenantIdentifierResolver;
import org.hibernate.dialect.function.SQLFunction; import org.hibernate.dialect.function.SQLFunction;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.hql.spi.id.MultiTableBulkIdStrategy; import org.hibernate.hql.spi.id.MultiTableBulkIdStrategy;
import org.hibernate.jpa.JpaCompliance; import org.hibernate.jpa.JpaCompliance;
import org.hibernate.loader.BatchFetchStyle; import org.hibernate.loader.BatchFetchStyle;
@ -41,6 +42,20 @@ import org.hibernate.tuple.entity.EntityTuplizerFactory;
* @since 5.0 * @since 5.0
*/ */
public interface SessionFactoryOptions { 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. * 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.criteria.CriteriaBuilder;
import javax.persistence.spi.PersistenceUnitTransactionType; import javax.persistence.spi.PersistenceUnitTransactionType;
import org.hibernate.AssertionFailure;
import org.hibernate.ConnectionAcquisitionMode; import org.hibernate.ConnectionAcquisitionMode;
import org.hibernate.ConnectionReleaseMode; import org.hibernate.ConnectionReleaseMode;
import org.hibernate.CustomEntityDirtinessStrategy; 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.service.spi.EventListenerRegistry;
import org.hibernate.event.spi.EventType; import org.hibernate.event.spi.EventType;
import org.hibernate.id.IdentifierGenerator; import org.hibernate.id.IdentifierGenerator;
import org.hibernate.id.UUIDGenerator;
import org.hibernate.id.factory.IdentifierGeneratorFactory; import org.hibernate.id.factory.IdentifierGeneratorFactory;
import org.hibernate.integrator.spi.Integrator; import org.hibernate.integrator.spi.Integrator;
import org.hibernate.integrator.spi.IntegratorService; import org.hibernate.integrator.spi.IntegratorService;
@ -155,10 +153,9 @@ import static org.hibernate.metamodel.internal.JpaMetaModelPopulationSetting.det
public final class SessionFactoryImpl implements SessionFactoryImplementor { public final class SessionFactoryImpl implements SessionFactoryImplementor {
private static final CoreMessageLogger LOG = CoreLogging.messageLogger( SessionFactoryImpl.class ); private static final CoreMessageLogger LOG = CoreLogging.messageLogger( SessionFactoryImpl.class );
private static final IdentifierGenerator UUID_GENERATOR = UUIDGenerator.buildSessionFactoryUniqueIdentifierGenerator();
private final String name; private final String name;
private final String uuid; private final String uuid;
private transient boolean isClosed; private transient boolean isClosed;
private final transient SessionFactoryObserverChain observer = new SessionFactoryObserverChain(); private final transient SessionFactoryObserverChain observer = new SessionFactoryObserverChain();
@ -216,12 +213,7 @@ public final class SessionFactoryImpl implements SessionFactoryImplementor {
} }
this.name = sfName; this.name = sfName;
try { this.uuid = options.getUuid();
uuid = (String) UUID_GENERATOR.generate( null, null );
}
catch (Exception e) {
throw new AssertionFailure("Could not generate UUID");
}
final JdbcServices jdbcServices = serviceRegistry.getService( JdbcServices.class ); final JdbcServices jdbcServices = serviceRegistry.getService( JdbcServices.class );
@ -374,7 +366,7 @@ public final class SessionFactoryImpl implements SessionFactoryImplementor {
this.observer.sessionFactoryCreated( this ); this.observer.sessionFactoryCreated( this );
SessionFactoryRegistry.INSTANCE.addSessionFactory( SessionFactoryRegistry.INSTANCE.addSessionFactory(
uuid, getUuid(),
name, name,
settings.isSessionFactoryNameAlsoJndiName(), settings.isSessionFactoryNameAlsoJndiName(),
this, this,
@ -538,7 +530,10 @@ public final class SessionFactoryImpl implements SessionFactoryImplementor {
return new DeserializationResolver() { return new DeserializationResolver() {
@Override @Override
public SessionFactoryImplementor resolve() { 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( SessionFactoryRegistry.INSTANCE.removeSessionFactory(
uuid, getUuid(),
name, name,
settings.isSessionFactoryNameAlsoJndiName(), settings.isSessionFactoryNameAlsoJndiName(),
serviceRegistry.getService( JndiService.class ) serviceRegistry.getService( JndiService.class )
@ -1504,7 +1499,7 @@ public final class SessionFactoryImpl implements SessionFactoryImplementor {
* @throws IOException Can be thrown by the stream * @throws IOException Can be thrown by the stream
*/ */
private void writeObject(ObjectOutputStream out) throws IOException { private void writeObject(ObjectOutputStream out) throws IOException {
LOG.debugf( "Serializing: %s", uuid ); LOG.debugf( "Serializing: %s", getUuid() );
out.defaultWriteObject(); out.defaultWriteObject();
LOG.trace( "Serialized" ); LOG.trace( "Serialized" );
} }
@ -1520,7 +1515,7 @@ public final class SessionFactoryImpl implements SessionFactoryImplementor {
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
LOG.trace( "Deserializing" ); LOG.trace( "Deserializing" );
in.defaultReadObject(); 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 { private Object readResolve() throws InvalidObjectException {
LOG.trace( "Resolving serialized SessionFactory" ); LOG.trace( "Resolving serialized SessionFactory" );
return locateSessionFactoryOnDeserialization( uuid, name ); return locateSessionFactoryOnDeserialization( getUuid(), name );
} }
private static SessionFactory locateSessionFactoryOnDeserialization(String uuid, String name) throws InvalidObjectException{ 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 * @throws IOException Indicates problems writing out the serial data stream
*/ */
void serialize(ObjectOutputStream oos) throws IOException { void serialize(ObjectOutputStream oos) throws IOException {
oos.writeUTF( uuid ); oos.writeUTF( getUuid() );
oos.writeBoolean( name != null ); oos.writeBoolean( name != null );
if ( name != null ) { if ( name != null ) {
oos.writeUTF( name ); oos.writeUTF( name );