HHH-10097 - Expose the UUID from SessionFactoryImplementor

This commit is contained in:
Steve Ebersole 2015-10-02 18:35:16 -05:00
parent 4d39b38342
commit eaf28166d2
7 changed files with 28 additions and 31 deletions

View File

@ -10,7 +10,6 @@ package org.hibernate.bytecode.enhance.spi.interceptor;
import java.io.Serializable;
import java.util.Collection;
import java.util.Set;
import javax.naming.NamingException;
import org.hibernate.LockMode;
import org.hibernate.bytecode.enhance.internal.tracker.SimpleFieldTracker;
@ -51,12 +50,7 @@ public class LazyAttributeLoader implements PersistentAttributeInterceptor, Cons
this.allowLoadOutsideTransaction = session.getFactory().getSessionFactoryOptions().isInitializeLazyStateOutsideTransactionsEnabled();
if ( this.allowLoadOutsideTransaction ) {
try {
this.sessionFactoryUuid = (String) session.getFactory().getReference().get( "uuid" ).getContent();
}
catch (NamingException e) {
log.debug( "Unable to determine SF UUID in preparation for `allowLoadOutsideTransaction`" );
}
this.sessionFactoryUuid = session.getFactory().getUuid();
}
}

View File

@ -15,8 +15,6 @@ import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import javax.naming.NamingException;
import org.hibernate.AssertionFailure;
import org.hibernate.FlushMode;
import org.hibernate.HibernateException;
@ -39,7 +37,6 @@ import org.hibernate.internal.util.collections.IdentitySet;
import org.hibernate.persister.collection.CollectionPersister;
import org.hibernate.persister.entity.EntityPersister;
import org.hibernate.pretty.MessageHelper;
import org.hibernate.type.ComponentType;
import org.hibernate.type.CompositeType;
import org.hibernate.type.IntegerType;
import org.hibernate.type.LongType;
@ -601,12 +598,7 @@ public abstract class AbstractPersistentCollection implements Serializable, Pers
allowLoadOutsideTransaction = session.getFactory().getSessionFactoryOptions().isInitializeLazyStateOutsideTransactionsEnabled();
if ( allowLoadOutsideTransaction && sessionFactoryUuid == null ) {
try {
sessionFactoryUuid = (String) session.getFactory().getReference().get( "uuid" ).getContent();
}
catch (NamingException e) {
//not much we can do if this fails...
}
sessionFactoryUuid = session.getFactory().getUuid();
}
}
}

View File

@ -407,6 +407,11 @@ public class SessionFactoryDelegatingImpl implements SessionFactoryImplementor,
return delegate.getReferencedPropertyType( className, propertyName );
}
@Override
public String getUuid() {
return delegate.getUuid();
}
@Override
public Reference getReference() throws NamingException {
return delegate.getReference();

View File

@ -69,6 +69,16 @@ public interface SessionFactoryImplementor extends Mapping, SessionFactory {
*/
Properties getProperties();
/**
* Get the UUID for this SessionFactory. 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
*/
String getUuid();
/**
* Get the persister for the named entity
*

View File

@ -719,6 +719,11 @@ public final class SessionFactoryImpl implements SessionFactoryImplementor {
return properties;
}
@Override
public String getUuid() {
return uuid;
}
public IdentifierGeneratorFactory getIdentifierGeneratorFactory() {
return null;
}
@ -870,7 +875,7 @@ public final class SessionFactoryImpl implements SessionFactoryImplementor {
LOG.debug( "Returning a Reference to the SessionFactory" );
return new Reference(
SessionFactoryImpl.class.getName(),
new StringRefAddr("uuid", uuid),
new StringRefAddr("uuid", getUuid()),
SessionFactoryRegistry.ObjectFactoryImpl.class.getName(),
null
);

View File

@ -7,7 +7,6 @@
package org.hibernate.proxy;
import java.io.Serializable;
import javax.naming.NamingException;
import org.hibernate.FlushMode;
import org.hibernate.HibernateException;
@ -223,15 +222,10 @@ public abstract class AbstractLazyInitializer implements LazyInitializer {
protected void prepareForPossibleLoadingOutsideTransaction() {
if ( session != null ) {
allowLoadOutsideTransaction = session.getFactory().getSettings().isInitializeLazyStateOutsideTransactionsEnabled();
allowLoadOutsideTransaction = session.getFactory().getSessionFactoryOptions().isInitializeLazyStateOutsideTransactionsEnabled();
if ( allowLoadOutsideTransaction && sessionFactoryUuid == null ) {
try {
sessionFactoryUuid = (String) session.getFactory().getReference().get( "uuid" ).getContent();
}
catch (NamingException e) {
//not much we can do if this fails...
}
sessionFactoryUuid = session.getFactory().getUuid();
}
}
}

View File

@ -15,6 +15,7 @@ import org.junit.Test;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.cfg.Configuration;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.internal.SessionFactoryRegistry;
import org.hibernate.internal.util.SerializationHelper;
import org.hibernate.testing.junit4.BaseUnitTestCase;
@ -39,9 +40,7 @@ public class SessionFactorySerializationTest extends BaseUnitTestCase {
// we need to do some tricking here so that Hibernate thinks the deserialization happens in a
// different VM
Reference reference = factory.getReference();
StringRefAddr refAddr = (StringRefAddr) reference.get( "uuid" );
String uuid = (String) refAddr.getContent();
String uuid = ( (SessionFactoryImplementor) factory ).getUuid();
// deregister under this uuid...
SessionFactoryRegistry.INSTANCE.removeSessionFactory( uuid, NAME, false, null );
// and then register under a different uuid...
@ -66,9 +65,7 @@ public class SessionFactorySerializationTest extends BaseUnitTestCase {
// we need to do some tricking here so that Hibernate thinks the deserialization happens in a
// different VM
Reference reference = factory.getReference();
StringRefAddr refAddr = (StringRefAddr) reference.get( "uuid" );
String uuid = (String) refAddr.getContent();
String uuid = ( (SessionFactoryImplementor) factory ).getUuid();
// deregister under this uuid...
SessionFactoryRegistry.INSTANCE.removeSessionFactory( uuid, null, false, null );
// and then register under a different uuid...