use of org.hibernate.engine.spi.SessionFactoryImplementor in project hibernate-orm by hibernate.
the class Collections method processReachableCollection.
/**
* Initialize the role of the collection.
*
* @param collection The collection to be updated by reachability.
* @param type The type of the collection.
* @param entity The owner of the collection.
* @param session The session from which this request originates
*/
public static void processReachableCollection(PersistentCollection collection, CollectionType type, Object entity, SessionImplementor session) {
collection.setOwner(entity);
final CollectionEntry ce = session.getPersistenceContext().getCollectionEntry(collection);
if (ce == null) {
// refer to comment in StatefulPersistenceContext.addCollection()
throw new HibernateException("Found two representations of same collection: " + type.getRole());
}
final SessionFactoryImplementor factory = session.getFactory();
final CollectionPersister persister = factory.getMetamodel().collectionPersister(type.getRole());
ce.setCurrentPersister(persister);
//TODO: better to pass the id in as an argument?
ce.setCurrentKey(type.getKeyOfOwner(entity, session));
final boolean isBytecodeEnhanced = persister.getOwnerEntityPersister().getInstrumentationMetadata().isEnhancedForLazyLoading();
if (isBytecodeEnhanced && !collection.wasInitialized()) {
// skip it
LOG.debugf("Skipping uninitialized bytecode-lazy collection: %s", MessageHelper.collectionInfoString(persister, collection, ce.getCurrentKey(), session));
ce.setReached(true);
ce.setProcessed(true);
} else {
// who set up circular or shared references between/to collections.
if (ce.isReached()) {
// We've been here beforeQuery
throw new HibernateException("Found shared references to a collection: " + type.getRole());
}
ce.setReached(true);
if (LOG.isDebugEnabled()) {
if (collection.wasInitialized()) {
LOG.debugf("Collection found: %s, was: %s (initialized)", MessageHelper.collectionInfoString(persister, collection, ce.getCurrentKey(), session), MessageHelper.collectionInfoString(ce.getLoadedPersister(), collection, ce.getLoadedKey(), session));
} else {
LOG.debugf("Collection found: %s, was: %s (uninitialized)", MessageHelper.collectionInfoString(persister, collection, ce.getCurrentKey(), session), MessageHelper.collectionInfoString(ce.getLoadedPersister(), collection, ce.getLoadedKey(), session));
}
}
prepareCollectionForUpdate(collection, ce, factory);
}
}
use of org.hibernate.engine.spi.SessionFactoryImplementor in project hibernate-orm by hibernate.
the class NaturalIdXrefDelegate method findCachedNaturalIdResolution.
/**
* Given a persister and natural-id value(s), find the locally cross-referenced primary key. Will return
* {@link PersistenceContext.NaturalIdHelper#INVALID_NATURAL_ID_REFERENCE} if the given natural ids are known to
* be invalid (see {@link #stashInvalidNaturalIdReference}).
*
* @param persister The persister representing the entity type.
* @param naturalIdValues The natural id value(s)
*
* @return The corresponding cross-referenced primary key,
* {@link PersistenceContext.NaturalIdHelper#INVALID_NATURAL_ID_REFERENCE},
* or {@code null} if none
*/
public Serializable findCachedNaturalIdResolution(EntityPersister persister, Object[] naturalIdValues) {
persister = locatePersisterForKey(persister);
validateNaturalId(persister, naturalIdValues);
NaturalIdResolutionCache entityNaturalIdResolutionCache = naturalIdResolutionCacheMap.get(persister);
Serializable pk;
final CachedNaturalId cachedNaturalId = new CachedNaturalId(persister, naturalIdValues);
if (entityNaturalIdResolutionCache != null) {
pk = entityNaturalIdResolutionCache.naturalIdToPkMap.get(cachedNaturalId);
// Found in session cache
if (pk != null) {
if (LOG.isTraceEnabled()) {
LOG.trace("Resolved natural key -> primary key resolution in session cache: " + persister.getRootEntityName() + "#[" + Arrays.toString(naturalIdValues) + "]");
}
return pk;
}
// if we did not find a hit, see if we know about these natural ids as invalid...
if (entityNaturalIdResolutionCache.containsInvalidNaturalIdReference(naturalIdValues)) {
return PersistenceContext.NaturalIdHelper.INVALID_NATURAL_ID_REFERENCE;
}
}
// Session cache miss, see if second-level caching is enabled
if (!persister.hasNaturalIdCache()) {
return null;
}
// Try resolution from second-level cache
final NaturalIdRegionAccessStrategy naturalIdCacheAccessStrategy = persister.getNaturalIdCacheAccessStrategy();
final Object naturalIdCacheKey = naturalIdCacheAccessStrategy.generateCacheKey(naturalIdValues, persister, session());
pk = CacheHelper.fromSharedCache(session(), naturalIdCacheKey, naturalIdCacheAccessStrategy);
// Found in second-level cache, store in session cache
final SessionFactoryImplementor factory = session().getFactory();
if (pk != null) {
if (factory.getStatistics().isStatisticsEnabled()) {
factory.getStatisticsImplementor().naturalIdCacheHit(naturalIdCacheAccessStrategy.getRegion().getName());
}
if (LOG.isTraceEnabled()) {
// protected to avoid Arrays.toString call unless needed
LOG.tracef("Found natural key [%s] -> primary key [%s] xref in second-level cache for %s", Arrays.toString(naturalIdValues), pk, persister.getRootEntityName());
}
if (entityNaturalIdResolutionCache == null) {
entityNaturalIdResolutionCache = new NaturalIdResolutionCache(persister);
NaturalIdResolutionCache existingCache = naturalIdResolutionCacheMap.putIfAbsent(persister, entityNaturalIdResolutionCache);
if (existingCache != null) {
entityNaturalIdResolutionCache = existingCache;
}
}
entityNaturalIdResolutionCache.pkToNaturalIdMap.put(pk, cachedNaturalId);
entityNaturalIdResolutionCache.naturalIdToPkMap.put(cachedNaturalId, pk);
} else if (factory.getStatistics().isStatisticsEnabled()) {
factory.getStatisticsImplementor().naturalIdCacheMiss(naturalIdCacheAccessStrategy.getRegion().getName());
}
return pk;
}
use of org.hibernate.engine.spi.SessionFactoryImplementor in project hibernate-orm by hibernate.
the class PessimisticReadSelectLockingStrategy method lock.
@Override
public void lock(Serializable id, Object version, Object object, int timeout, SharedSessionContractImplementor session) {
final String sql = determineSql(timeout);
final SessionFactoryImplementor factory = session.getFactory();
try {
try {
final PreparedStatement st = session.getJdbcCoordinator().getStatementPreparer().prepareStatement(sql);
try {
getLockable().getIdentifierType().nullSafeSet(st, id, 1, session);
if (getLockable().isVersioned()) {
getLockable().getVersionType().nullSafeSet(st, version, getLockable().getIdentifierType().getColumnSpan(factory) + 1, session);
}
final ResultSet rs = session.getJdbcCoordinator().getResultSetReturn().extract(st);
try {
if (!rs.next()) {
if (factory.getStatistics().isStatisticsEnabled()) {
factory.getStatistics().optimisticFailure(getLockable().getEntityName());
}
throw new StaleObjectStateException(getLockable().getEntityName(), id);
}
} finally {
session.getJdbcCoordinator().getLogicalConnection().getResourceRegistry().release(rs, st);
}
} finally {
session.getJdbcCoordinator().getLogicalConnection().getResourceRegistry().release(st);
session.getJdbcCoordinator().afterStatementExecution();
}
} catch (SQLException e) {
throw session.getJdbcServices().getSqlExceptionHelper().convert(e, "could not lock: " + MessageHelper.infoString(getLockable(), id, session.getFactory()), sql);
}
} catch (JDBCException e) {
throw new PessimisticEntityLockException(object, "could not obtain pessimistic lock", e);
}
}
use of org.hibernate.engine.spi.SessionFactoryImplementor in project hibernate-orm by hibernate.
the class PessimisticReadUpdateLockingStrategy method generateLockString.
protected String generateLockString() {
final SessionFactoryImplementor factory = lockable.getFactory();
final Update update = new Update(factory.getDialect());
update.setTableName(lockable.getRootTableName());
update.addPrimaryKeyColumns(lockable.getRootTableIdentifierColumnNames());
update.setVersionColumnName(lockable.getVersionColumnName());
update.addColumn(lockable.getVersionColumnName());
if (factory.getSessionFactoryOptions().isCommentsEnabled()) {
update.setComment(lockMode + " lock " + lockable.getEntityName());
}
return update.toStatementString();
}
use of org.hibernate.engine.spi.SessionFactoryImplementor in project hibernate-orm by hibernate.
the class PessimisticWriteUpdateLockingStrategy method generateLockString.
protected String generateLockString() {
final SessionFactoryImplementor factory = lockable.getFactory();
final Update update = new Update(factory.getDialect());
update.setTableName(lockable.getRootTableName());
update.addPrimaryKeyColumns(lockable.getRootTableIdentifierColumnNames());
update.setVersionColumnName(lockable.getVersionColumnName());
update.addColumn(lockable.getVersionColumnName());
if (factory.getSessionFactoryOptions().isCommentsEnabled()) {
update.setComment(lockMode + " lock " + lockable.getEntityName());
}
return update.toStatementString();
}
Aggregations