Skip to content

Commit

Permalink
HHH-16557 Testcase and bugfix proposal (revised by beikov)
Browse files Browse the repository at this point in the history
  • Loading branch information
pb00068 committed May 8, 2024
1 parent b097355 commit e9a6223
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ protected void postDeleteLoaded(
final EntityKey key = entry.getEntityKey();
persistenceContext.removeEntityHolder( key );
removeCacheItem( ck );
persistenceContext.getNaturalIdResolutions().removeSharedResolution( id, naturalIdValues, persister );
persistenceContext.getNaturalIdResolutions().removeSharedResolution( id, naturalIdValues, persister, true);
postDelete();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,11 @@ private void manageSharedResolution(

@Override
public void removeSharedResolution(Object id, Object naturalId, EntityMappingType entityDescriptor) {
removeSharedResolution( id, naturalId, entityDescriptor, false );
}

@Override
public void removeSharedResolution(Object id, Object naturalId, EntityMappingType entityDescriptor, boolean delayToAfterTransactionCompletion) {
final NaturalIdMapping naturalIdMapping = entityDescriptor.getNaturalIdMapping();
if ( naturalIdMapping == null ) {
// nothing to do
Expand All @@ -453,7 +458,18 @@ public void removeSharedResolution(Object id, Object naturalId, EntityMappingTyp
final EntityPersister persister = locatePersisterForKey( entityDescriptor.getEntityPersister() );

final Object naturalIdCacheKey = cacheAccess.generateCacheKey( naturalId, persister, session() );
cacheAccess.evict( naturalIdCacheKey );
if ( delayToAfterTransactionCompletion ) {
session().asEventSource().getActionQueue().registerProcess(
(success, session) -> {
if ( success ) {
cacheAccess.evict( naturalIdCacheKey );
}
}
);
}
else {
cacheAccess.evict( naturalIdCacheKey );
}

// if ( sessionCachedNaturalIdValues != null
// && !Arrays.equals( sessionCachedNaturalIdValues, deletedNaturalIdValues ) ) {
Expand All @@ -479,7 +495,7 @@ public void handleSynchronization(Object pk, Object entity, EntityMappingType en
cacheResolution( pk, naturalIdValuesFromCurrentObjectState, persister );
stashInvalidNaturalIdReference( persister, cachedNaturalIdValues );

removeSharedResolution( pk, cachedNaturalIdValues, persister );
removeSharedResolution( pk, cachedNaturalIdValues, persister, false );
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,12 @@ void manageSharedResolution(
/**
* Removes any cross-reference from the L2 cache
*/
void removeSharedResolution(Object id, Object naturalId, EntityMappingType entityDescriptor);

void removeSharedResolution(Object id, Object naturalId, EntityMappingType entityDescriptor, boolean delayToAfterTransactionCompletion);

default void removeSharedResolution(Object id, Object naturalId, EntityMappingType entityDescriptor) {
removeSharedResolution( id, naturalId, entityDescriptor, false );
}

/**
* Find the cached natural-id for the given identifier
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4115,7 +4115,7 @@ private void handleNaturalIdReattachment(Object entity, SharedSessionContractImp
? null
: naturalIdMapping.extractNaturalIdFromEntityState( entitySnapshot );

naturalIdResolutions.removeSharedResolution( id, naturalIdSnapshot, this );
naturalIdResolutions.removeSharedResolution( id, naturalIdSnapshot, this, false );
final Object naturalId = naturalIdMapping.extractNaturalIdFromEntity( entity );
naturalIdResolutions.manageLocalResolution( id, naturalId, this, CachedNaturalIdValueSource.UPDATE );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,40 @@ public void testBySimpleNaturalIdResolveEntityFrom2LCacheSubClass(SessionFactory
}
);
}

@Test
@TestForIssue( jiraKey = "HHH-16557" )
public void testCreateDeleteRecreate(SessionFactoryScope scope) {

final Integer id = scope.fromTransaction(
(session) -> {
AllCached it = new AllCached( "it" );
session.persist(it);
session.remove(it);
// insert-remove might happen in an app driven by users GUI interactions
return it.getId();
}
);

// now recreate with same naturalId value
scope.inTransaction(
(session) -> {
AllCached it = new AllCached( "it" );
session.persist(it);
// resolving from first level cache
assertNotNull(session.bySimpleNaturalId( AllCached.class ).load( "it" ));
}
);

scope.inTransaction(
(session) -> {
// should resolve from second level cache
final AllCached shouldBeThere = session.bySimpleNaturalId( AllCached.class ).load( "it" );
assertNotNull( shouldBeThere );
assert(id.compareTo(shouldBeThere.getId()) != 0);
}
);
}

@Test
@JiraKey("HHH-16558")
Expand Down

0 comments on commit e9a6223

Please sign in to comment.