HHH-5609 - Move SessionImplementor#wasInsertedDuringTransaction to PersistenceContext

git-svn-id: https://svn.jboss.org/repos/hibernate/core/trunk@20760 1b8cb986-b30d-0410-93ca-fae66ebed9b2
This commit is contained in:
Steve Ebersole 2010-09-30 17:47:19 +00:00
parent 747076abb1
commit e1cea7d8e1
7 changed files with 67 additions and 65 deletions

View File

@ -75,7 +75,7 @@ public final class EntityIdentityInsertAction extends EntityAction {
//need to do that here rather than in the save event listener to let
//the post insert events to have a id-filled entity when IDENTITY is used (EJB3)
persister.setIdentifier( instance, generatedId, session );
getSession().registerInsertedKey( getPersister(), generatedId );
getSession().getPersistenceContext().registerInsertedKey( getPersister(), generatedId );
}

View File

@ -93,7 +93,7 @@ public final class EntityInsertAction extends EntityAction {
entry.postUpdate(instance, state, version);
}
getSession().registerInsertedKey( getPersister(), getId() );
getSession().getPersistenceContext().registerInsertedKey( getPersister(), getId() );
}
final SessionFactoryImplementor factory = getSession().getFactory();

View File

@ -583,4 +583,22 @@ public interface PersistenceContext {
* @param parent
*/
public void removeChildParent(Object child);
/**
* Register keys inserted during the current transaction
*
* @param persister The entity persister
* @param id The id
*/
public void registerInsertedKey(EntityPersister persister, Serializable id);
/**
* Allows callers to check to see if the identified entity was inserted during the current transaction.
*
* @param persister The entity persister
* @param id The id
*
* @return True if inserted during this transaction, false otherwise.
*/
public boolean wasInsertedDuringTransaction(EntityPersister persister, Serializable id);
}

View File

@ -365,22 +365,4 @@ public interface SessionImplementor extends Serializable {
* should never be null.
*/
public LoadQueryInfluencers getLoadQueryInfluencers();
/**
* Register keys inserted during the current transaction
*
* @param persister The entity persister
* @param id The id
*/
public void registerInsertedKey(EntityPersister persister, Serializable id);
/**
* Allows callers to check to see if the identified entity was inserted during the current transaction.
*
* @param persister The entity persister
* @param id The id
*
* @return True if inserted during this transaction, false otherwise.
*/
public boolean wasInsertedDuringTransaction(EntityPersister persister, Serializable id);
}

View File

@ -269,6 +269,7 @@ public class StatefulPersistenceContext implements PersistenceContext {
}
public void afterTransactionCompletion() {
cleanUpInsertedKeysAfterTransaction();
// Downgrade locks
Iterator iter = entityEntries.values().iterator();
while ( iter.hasNext() ) {
@ -1622,4 +1623,49 @@ public class StatefulPersistenceContext implements PersistenceContext {
public void removeChildParent(Object child) {
parentsByChild.remove(child);
}
private HashMap<String,List<Serializable>> insertedKeysMap;
/**
* {@inheritDoc}
*/
public void registerInsertedKey(EntityPersister persister, Serializable id) {
// we only are about regsitering these if the persister defines caching
if ( persister.hasCache() ) {
if ( insertedKeysMap == null ) {
insertedKeysMap = new HashMap<String, List<Serializable>>();
}
final String rootEntityName = persister.getRootEntityName();
List<Serializable> insertedEntityIds = insertedKeysMap.get( rootEntityName );
if ( insertedEntityIds == null ) {
insertedEntityIds = new ArrayList<Serializable>();
insertedKeysMap.put( rootEntityName, insertedEntityIds );
}
insertedEntityIds.add( id );
}
}
/**
* {@inheritDoc}
*/
public boolean wasInsertedDuringTransaction(EntityPersister persister, Serializable id) {
// again, we only really care if the entity is cached
if ( persister.hasCache() ) {
if ( insertedKeysMap != null ) {
List<Serializable> insertedEntityIds = insertedKeysMap.get( persister.getRootEntityName() );
if ( insertedEntityIds != null ) {
return insertedEntityIds.contains( id );
}
}
}
return false;
}
private void cleanUpInsertedKeysAfterTransaction() {
if ( insertedKeysMap != null ) {
insertedKeysMap.clear();
}
}
}

View File

@ -183,7 +183,7 @@ public final class TwoPhaseLoad {
// 2) Session#clear + some form of load
//
// we need to be careful not to clobber the lock here in the cache so that it can be rolled back if need be
if ( session.wasInsertedDuringTransaction( persister, id ) ) {
if ( session.getPersistenceContext().wasInsertedDuringTransaction( persister, id ) ) {
persister.getCacheAccessStrategy().update(
cacheKey,
persister.getCacheEntryStructure().structure( entry ),

View File

@ -598,7 +598,6 @@ public final class SessionImpl extends AbstractSessionImpl
public void afterTransactionCompletion(boolean success, Transaction tx) {
log.trace( "after transaction completion" );
cleanUpInsertedKeysAfterTransaction();
persistenceContext.afterTransactionCompletion();
actionQueue.afterTransactionCompletion(success);
if ( rootSession == null && tx != null ) {
@ -2017,49 +2016,6 @@ public final class SessionImpl extends AbstractSessionImpl
return loadQueryInfluencers;
}
private HashMap<String,List<Serializable>> insertedKeysMap;
/**
* {@inheritDoc}
*/
public void registerInsertedKey(EntityPersister persister, Serializable id) {
// we only are about regsitering these if the persister defines caching
if ( persister.hasCache() ) {
if ( insertedKeysMap == null ) {
insertedKeysMap = new HashMap<String, List<Serializable>>();
}
final String rootEntityName = persister.getRootEntityName();
List<Serializable> insertedEntityIds = insertedKeysMap.get( rootEntityName );
if ( insertedEntityIds == null ) {
insertedEntityIds = new ArrayList<Serializable>();
insertedKeysMap.put( rootEntityName, insertedEntityIds );
}
insertedEntityIds.add( id );
}
}
/**
* {@inheritDoc}
*/
public boolean wasInsertedDuringTransaction(EntityPersister persister, Serializable id) {
// again, we only really care if the entity is cached
if ( persister.hasCache() ) {
if ( insertedKeysMap != null ) {
List<Serializable> insertedEntityIds = insertedKeysMap.get( persister.getRootEntityName() );
if ( insertedEntityIds != null ) {
return insertedEntityIds.contains( id );
}
}
}
return false;
}
private void cleanUpInsertedKeysAfterTransaction() {
if ( insertedKeysMap != null ) {
insertedKeysMap.clear();
}
}
// filter support ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/**