HHH-7160 - NaturalIdXrefDelegate#cache() needs to remove obsolete entries in shared cache when NaturalId values changed
This commit is contained in:
parent
fb0b90bd2d
commit
4f64b56a88
|
@ -26,7 +26,6 @@ package org.hibernate.action.internal;
|
|||
import java.io.Serializable;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.Set;
|
||||
|
||||
|
@ -36,6 +35,7 @@ import org.hibernate.action.spi.BeforeTransactionCompletionProcess;
|
|||
import org.hibernate.action.spi.Executable;
|
||||
import org.hibernate.cache.spi.access.CollectionRegionAccessStrategy;
|
||||
import org.hibernate.cache.spi.access.EntityRegionAccessStrategy;
|
||||
import org.hibernate.cache.spi.access.NaturalIdRegionAccessStrategy;
|
||||
import org.hibernate.cache.spi.access.SoftLock;
|
||||
import org.hibernate.engine.spi.SessionFactoryImplementor;
|
||||
import org.hibernate.engine.spi.SessionImplementor;
|
||||
|
@ -60,6 +60,7 @@ public class BulkOperationCleanupAction implements Executable, Serializable {
|
|||
|
||||
private final Set<EntityCleanup> entityCleanups = new HashSet<EntityCleanup>();
|
||||
private final Set<CollectionCleanup> collectionCleanups = new HashSet<CollectionCleanup>();
|
||||
private final Set<NaturalIdCleanup> naturalIdCleanups = new HashSet<NaturalIdCleanup>();
|
||||
|
||||
/**
|
||||
* Constructs an action to cleanup "affected cache regions" based on the
|
||||
|
@ -80,6 +81,9 @@ public class BulkOperationCleanupAction implements Executable, Serializable {
|
|||
if ( persister.hasCache() ) {
|
||||
entityCleanups.add( new EntityCleanup( persister.getCacheAccessStrategy() ) );
|
||||
}
|
||||
if ( persister.hasNaturalIdentifier() && persister.hasNaturalIdCache() ) {
|
||||
naturalIdCleanups.add( new NaturalIdCleanup( persister.getNaturalIdCacheAccessStrategy() ) );
|
||||
}
|
||||
|
||||
Set<String> roles = factory.getCollectionRolesByEntityParticipant( persister.getEntityName() );
|
||||
if ( roles != null ) {
|
||||
|
@ -122,6 +126,10 @@ public class BulkOperationCleanupAction implements Executable, Serializable {
|
|||
if ( persister.hasCache() ) {
|
||||
entityCleanups.add( new EntityCleanup( persister.getCacheAccessStrategy() ) );
|
||||
}
|
||||
if ( persister.hasNaturalIdentifier() && persister.hasNaturalIdCache() ) {
|
||||
naturalIdCleanups.add( new NaturalIdCleanup( persister.getNaturalIdCacheAccessStrategy() ) );
|
||||
}
|
||||
|
||||
Set<String> roles = session.getFactory().getCollectionRolesByEntityParticipant( persister.getEntityName() );
|
||||
if ( roles != null ) {
|
||||
for ( String role : roles ) {
|
||||
|
@ -180,17 +188,21 @@ public class BulkOperationCleanupAction implements Executable, Serializable {
|
|||
return new AfterTransactionCompletionProcess() {
|
||||
@Override
|
||||
public void doAfterTransactionCompletion(boolean success, SessionImplementor session) {
|
||||
Iterator itr = entityCleanups.iterator();
|
||||
while ( itr.hasNext() ) {
|
||||
final EntityCleanup cleanup = ( EntityCleanup ) itr.next();
|
||||
for ( EntityCleanup cleanup : entityCleanups ) {
|
||||
cleanup.release();
|
||||
}
|
||||
entityCleanups.clear();
|
||||
|
||||
itr = collectionCleanups.iterator();
|
||||
while ( itr.hasNext() ) {
|
||||
final CollectionCleanup cleanup = ( CollectionCleanup ) itr.next();
|
||||
for ( NaturalIdCleanup cleanup : naturalIdCleanups ) {
|
||||
cleanup.release();
|
||||
|
||||
}
|
||||
entityCleanups.clear();
|
||||
|
||||
for ( CollectionCleanup cleanup : collectionCleanups ) {
|
||||
cleanup.release();
|
||||
}
|
||||
collectionCleanups.clear();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
@ -234,4 +246,19 @@ public class BulkOperationCleanupAction implements Executable, Serializable {
|
|||
cacheAccess.unlockRegion( cacheLock );
|
||||
}
|
||||
}
|
||||
|
||||
private class NaturalIdCleanup {
|
||||
private final NaturalIdRegionAccessStrategy naturalIdCacheAccessStrategy;
|
||||
private final SoftLock cacheLock;
|
||||
|
||||
public NaturalIdCleanup(NaturalIdRegionAccessStrategy naturalIdCacheAccessStrategy) {
|
||||
this.naturalIdCacheAccessStrategy = naturalIdCacheAccessStrategy;
|
||||
this.cacheLock = naturalIdCacheAccessStrategy.lockRegion();
|
||||
naturalIdCacheAccessStrategy.removeAll();
|
||||
}
|
||||
|
||||
private void release() {
|
||||
naturalIdCacheAccessStrategy.unlockRegion( cacheLock );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -29,9 +29,10 @@ import org.hibernate.cache.spi.EntityRegion;
|
|||
/**
|
||||
* Contract for managing transactional and concurrent access to cached entity
|
||||
* data. The expected call sequences related to various operations are:<ul>
|
||||
* <li><b>INSERTS</b> : {@link #insert} -> {@link #afterInsert}</li>
|
||||
* <li><b>UPDATES</b> : {@link #lockItem} -> {@link #update} -> {@link #afterUpdate}</li>
|
||||
* <li><b>DELETES</b> : {@link #lockItem} -> {@link #remove} -> {@link #unlockItem}</li>
|
||||
* <li><b>INSERTS</b> : {@link #insert} -> {@link #afterInsert}</li>
|
||||
* <li><b>UPDATES</b> : {@link #lockItem} -> {@link #update} -> {@link #afterUpdate}</li>
|
||||
* <li><b>DELETES</b> : {@link #lockItem} -> {@link #remove} -> {@link #unlockItem}</li>
|
||||
* <li><b>LOADS</b> : {@link @putFromLoad}</li>
|
||||
* </ul>
|
||||
* <p/>
|
||||
* There is another usage pattern that is used to invalidate entries
|
||||
|
|
|
@ -28,17 +28,24 @@ import org.hibernate.cache.spi.NaturalIdRegion;
|
|||
|
||||
/**
|
||||
* Contract for managing transactional and concurrent access to cached naturalId
|
||||
* data. For cached naturalId data, all modification actions actually just
|
||||
* invalidate the entry(s). The call sequence here is:
|
||||
* {@link #lockItem} -> {@link #remove} -> {@link #unlockItem}
|
||||
* data. The expected call sequences related to various operations are:<ul>
|
||||
* <li><b>INSERTS</b> : {@link #insert} -> {@link #afterInsert}</li>
|
||||
* <li><b>UPDATES</b> : {@link #lockItem} -> {@link #remove} -> {@link #update} -> {@link #afterUpdate}</li>
|
||||
* <li><b>DELETES</b> : {@link #lockItem} -> {@link #remove} -> {@link #unlockItem}</li>
|
||||
* <li><b>LOADS</b> : {@link @putFromLoad}</li>
|
||||
* </ul>
|
||||
* Note the special case of <b>UPDATES</b> above. Because the cache key itself has changed here we need to remove the
|
||||
* old entry as well as
|
||||
* <p/>
|
||||
* There is another usage pattern that is used to invalidate entries
|
||||
* after performing "bulk" HQL/SQL operations:
|
||||
* {@link #lockRegion} -> {@link #removeAll} -> {@link #unlockRegion}
|
||||
* <p/>
|
||||
* NaturalIds are not versioned so null will always be passed to the version parameter for
|
||||
* {@link #putFromLoad(Object, Object, long, Object)}, {@link #putFromLoad(Object, Object, long, Object, boolean)},
|
||||
* and {@link #lockItem(Object, Object)}
|
||||
* IMPORTANT : NaturalIds are not versioned so {@code null} will always be passed to the version parameter to:<ul>
|
||||
* <li>{@link #putFromLoad(Object, Object, long, Object)}</li>
|
||||
* <li>{@link #putFromLoad(Object, Object, long, Object, boolean)}</li>
|
||||
* <li>{@link #lockItem(Object, Object)}</li>
|
||||
* </ul>
|
||||
*
|
||||
* @author Gavin King
|
||||
* @author Steve Ebersole
|
||||
|
|
|
@ -71,6 +71,10 @@ public class NaturalIdXrefDelegate {
|
|||
persister = locatePersisterForKey( persister );
|
||||
validateNaturalId( persister, naturalIdValues );
|
||||
|
||||
Object[] previousNaturalIdValues = valueSource == CachedNaturalIdValueSource.UPDATE
|
||||
? persistenceContext.getNaturalIdSnapshot( pk, persister )
|
||||
: null;
|
||||
|
||||
NaturalIdResolutionCache entityNaturalIdResolutionCache = naturalIdResolutionCacheMap.get( persister );
|
||||
if ( entityNaturalIdResolutionCache == null ) {
|
||||
entityNaturalIdResolutionCache = new NaturalIdResolutionCache( persister );
|
||||
|
@ -125,6 +129,10 @@ public class NaturalIdXrefDelegate {
|
|||
break;
|
||||
}
|
||||
case UPDATE: {
|
||||
final NaturalIdCacheKey previousCacheKey = new NaturalIdCacheKey( previousNaturalIdValues, persister, session() );
|
||||
final SoftLock removalLock = naturalIdCacheAccessStrategy.lockItem( previousCacheKey, null );
|
||||
naturalIdCacheAccessStrategy.remove( previousCacheKey );
|
||||
|
||||
final SoftLock lock = naturalIdCacheAccessStrategy.lockItem( naturalIdCacheKey, null );
|
||||
naturalIdCacheAccessStrategy.update( naturalIdCacheKey, pk );
|
||||
|
||||
|
@ -132,6 +140,7 @@ public class NaturalIdXrefDelegate {
|
|||
new AfterTransactionCompletionProcess() {
|
||||
@Override
|
||||
public void doAfterTransactionCompletion(boolean success, SessionImplementor session) {
|
||||
naturalIdCacheAccessStrategy.unlockRegion( removalLock );
|
||||
final boolean put = naturalIdCacheAccessStrategy.afterUpdate( naturalIdCacheKey, pk, lock );
|
||||
|
||||
if ( put && justAddedToLocalCache && factory.getStatistics().isStatisticsEnabled() ) {
|
||||
|
|
|
@ -0,0 +1,69 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* Copyright (c) 2012, Red Hat Inc. or third-party contributors as
|
||||
* indicated by the @author tags or express copyright attribution
|
||||
* statements applied by the authors. All third-party contributions are
|
||||
* distributed under license by Red Hat Inc.
|
||||
*
|
||||
* This copyrighted material is made available to anyone wishing to use, modify,
|
||||
* copy, or redistribute it subject to the terms and conditions of the GNU
|
||||
* Lesser General Public License, as published by the Free Software Foundation.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this distribution; if not, write to:
|
||||
* Free Software Foundation, Inc.
|
||||
* 51 Franklin Street, Fifth Floor
|
||||
* Boston, MA 02110-1301 USA
|
||||
*/
|
||||
package org.hibernate.test.naturalid.mutable.cached;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
|
||||
import org.hibernate.annotations.GenericGenerator;
|
||||
import org.hibernate.annotations.NaturalId;
|
||||
import org.hibernate.annotations.NaturalIdCache;
|
||||
|
||||
/**
|
||||
* @author Steve Ebersole
|
||||
*/
|
||||
@Entity
|
||||
@NaturalIdCache
|
||||
public class Another {
|
||||
private Integer id;
|
||||
private String name;
|
||||
|
||||
public Another() {
|
||||
}
|
||||
|
||||
public Another(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@Id
|
||||
@GeneratedValue( generator = "increment" )
|
||||
@GenericGenerator( name = "increment", strategy = "increment" )
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Integer id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
@NaturalId(mutable = true)
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,127 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* Copyright (c) 2012, Red Hat Inc. or third-party contributors as
|
||||
* indicated by the @author tags or express copyright attribution
|
||||
* statements applied by the authors. All third-party contributions are
|
||||
* distributed under license by Red Hat Inc.
|
||||
*
|
||||
* This copyrighted material is made available to anyone wishing to use, modify,
|
||||
* copy, or redistribute it subject to the terms and conditions of the GNU
|
||||
* Lesser General Public License, as published by the Free Software Foundation.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this distribution; if not, write to:
|
||||
* Free Software Foundation, Inc.
|
||||
* 51 Franklin Street, Fifth Floor
|
||||
* Boston, MA 02110-1301 USA
|
||||
*/
|
||||
package org.hibernate.test.naturalid.mutable.cached;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
import org.hibernate.HibernateException;
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.Transaction;
|
||||
import org.hibernate.cfg.Configuration;
|
||||
import org.hibernate.cfg.Environment;
|
||||
import org.hibernate.criterion.Restrictions;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
|
||||
import org.hibernate.test.naturalid.mutable.User;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNotSame;
|
||||
import static org.junit.Assert.assertNull;
|
||||
|
||||
/**
|
||||
* Tests of mutable natural ids stored in second level cache
|
||||
*
|
||||
* @author Guenther Demetz
|
||||
* @author Steve Ebersole
|
||||
*/
|
||||
public class CachedMutableNaturalIdTest extends BaseCoreFunctionalTestCase {
|
||||
@Override
|
||||
protected Class<?>[] getAnnotatedClasses() {
|
||||
return new Class[] {Another.class};
|
||||
}
|
||||
|
||||
@Override
|
||||
public void configure(Configuration cfg) {
|
||||
cfg.setProperty( Environment.USE_SECOND_LEVEL_CACHE, "true" );
|
||||
cfg.setProperty( Environment.GENERATE_STATISTICS, "true" );
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNaturalIdChangedWhileAttached() {
|
||||
Session session = openSession();
|
||||
session.beginTransaction();
|
||||
Another it = new Another( "it" );
|
||||
session.save( it );
|
||||
session.getTransaction().commit();
|
||||
session.close();
|
||||
|
||||
session = openSession();
|
||||
session.beginTransaction();
|
||||
it = (Another) session.bySimpleNaturalId( Another.class ).load( "it" );
|
||||
assertNotNull( it );
|
||||
// change it's name
|
||||
it.setName( "it2" );
|
||||
session.getTransaction().commit();
|
||||
session.close();
|
||||
|
||||
session = openSession();
|
||||
session.beginTransaction();
|
||||
it = (Another) session.bySimpleNaturalId( Another.class ).load( "it" );
|
||||
assertNull( it );
|
||||
it = (Another) session.bySimpleNaturalId( Another.class ).load( "it2" );
|
||||
assertNotNull( it );
|
||||
session.delete( it );
|
||||
session.getTransaction().commit();
|
||||
session.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNaturalIdChangedWhileDetached() {
|
||||
Session session = openSession();
|
||||
session.beginTransaction();
|
||||
Another it = new Another( "it" );
|
||||
session.save( it );
|
||||
session.getTransaction().commit();
|
||||
session.close();
|
||||
|
||||
session = openSession();
|
||||
session.beginTransaction();
|
||||
it = (Another) session.bySimpleNaturalId( Another.class ).load( "it" );
|
||||
assertNotNull( it );
|
||||
session.getTransaction().commit();
|
||||
session.close();
|
||||
|
||||
it.setName( "it2" );
|
||||
|
||||
session = openSession();
|
||||
session.beginTransaction();
|
||||
session.update( it );
|
||||
session.getTransaction().commit();
|
||||
session.close();
|
||||
|
||||
session = openSession();
|
||||
session.beginTransaction();
|
||||
it = (Another) session.bySimpleNaturalId( Another.class ).load( "it" );
|
||||
assertNull( it );
|
||||
it = (Another) session.bySimpleNaturalId( Another.class ).load( "it2" );
|
||||
assertNotNull( it );
|
||||
session.delete( it );
|
||||
session.getTransaction().commit();
|
||||
session.close();
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue