Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HHH-16557 Testcase and bugfix proposal (revised by beikov) #8335

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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