HHH-9231 Avoid updating collection 2LC if contains entity added in tx

(cherry picked from commit 3b098a94d3463086a230d6dbc1254e9ecbf76cda)

Conflicts:
	hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/functional/BasicTransactionalTestCase.java
This commit is contained in:
Galder Zamarreño 2014-06-11 12:01:09 +02:00 committed by Gail Badner
parent 31bcb0ea0a
commit 36e35207f6
2 changed files with 30 additions and 19 deletions

View File

@ -45,6 +45,8 @@ import org.hibernate.engine.spi.Status;
import org.hibernate.internal.CoreLogging;
import org.hibernate.internal.CoreMessageLogger;
import org.hibernate.persister.collection.CollectionPersister;
import org.hibernate.persister.collection.QueryableCollection;
import org.hibernate.persister.entity.EntityPersister;
import org.hibernate.pretty.MessageHelper;
/**
@ -349,22 +351,34 @@ public class CollectionLoadContext {
final CollectionCacheEntry entry = new CollectionCacheEntry( lce.getCollection(), persister );
final CacheKey cacheKey = session.generateCacheKey( lce.getKey(), persister.getKeyType(), persister.getRole() );
try {
session.getEventListenerManager().cachePutStart();
final boolean put = persister.getCacheAccessStrategy().putFromLoad(
cacheKey,
persister.getCacheEntryStructure().structure( entry ),
session.getTimestamp(),
version,
factory.getSettings().isMinimalPutsEnabled() && session.getCacheMode()!= CacheMode.REFRESH
);
if ( put && factory.getStatistics().isStatisticsEnabled() ) {
factory.getStatisticsImplementor().secondLevelCachePut( persister.getCacheAccessStrategy().getRegion().getName() );
boolean isPutFromLoad = true;
for (Serializable id : entry.getState()) {
EntityPersister entityPersister = ((QueryableCollection) persister).getElementPersister();
if ( session.getPersistenceContext().wasInsertedDuringTransaction( entityPersister, id ) ) {
isPutFromLoad = false;
break;
}
}
finally {
session.getEventListenerManager().cachePutEnd();
// CollectionRegionAccessStrategy has no update, so avoid putting uncommitted data via putFromLoad
if (isPutFromLoad) {
try {
session.getEventListenerManager().cachePutStart();
final boolean put = persister.getCacheAccessStrategy().putFromLoad(
cacheKey,
persister.getCacheEntryStructure().structure( entry ),
session.getTimestamp(),
version,
factory.getSettings().isMinimalPutsEnabled() && session.getCacheMode()!= CacheMode.REFRESH
);
if ( put && factory.getStatistics().isStatisticsEnabled() ) {
factory.getStatisticsImplementor().secondLevelCachePut( persister.getCacheAccessStrategy().getRegion().getName() );
}
}
finally {
session.getEventListenerManager().cachePutEnd();
}
}
}

View File

@ -37,7 +37,6 @@ import org.hibernate.NaturalIdLoadAccess;
import org.hibernate.cache.infinispan.access.PutFromLoadValidator;
import org.hibernate.criterion.Restrictions;
import org.junit.After;
import org.junit.Assert;
import org.junit.Test;
import org.hibernate.Session;
@ -46,7 +45,6 @@ import org.hibernate.cache.spi.entry.CacheEntry;
import org.hibernate.cfg.Configuration;
import org.hibernate.stat.SecondLevelCacheStatistics;
import org.hibernate.stat.Statistics;
import org.hibernate.testing.FailureExpected;
import org.hibernate.testing.TestForIssue;
import static junit.framework.Assert.assertEquals;
@ -145,7 +143,7 @@ public class BasicTransactionalTestCase extends AbstractFunctionalTestCase {
}
@Test
@TestForIssue( jiraKey = "HHH-9231")
@TestForIssue( jiraKey = "HHH-9231" )
public void testAddNewOneToManyElementInitFlushLeaveCacheConsistent() throws Exception {
Statistics stats = sessionFactory().getStatistics();
stats.clear();
@ -226,7 +224,7 @@ public class BasicTransactionalTestCase extends AbstractFunctionalTestCase {
}
@Test
@TestForIssue( jiraKey = "HHH-9231")
@TestForIssue( jiraKey = "HHH-9231" )
public void testAddNewOneToManyElementNoInitFlushLeaveCacheConsistent() throws Exception {
Statistics stats = sessionFactory().getStatistics();
stats.clear();
@ -307,7 +305,6 @@ public class BasicTransactionalTestCase extends AbstractFunctionalTestCase {
}
@Test
@FailureExpected( jiraKey = "HHH-9231")
public void testAddNewOneToManyElementNoInitFlushInitLeaveCacheConsistent() throws Exception {
Statistics stats = sessionFactory().getStatistics();
stats.clear();