Author: stliu
Date: 2010-09-28 02:47:19 -0400 (Tue, 28 Sep 2010)
New Revision: 20731
Modified:
core/branches/Branch_3_2_4_SP1_CP/build.xml
core/branches/Branch_3_2_4_SP1_CP/src/org/hibernate/action/EntityIdentityInsertAction.java
core/branches/Branch_3_2_4_SP1_CP/src/org/hibernate/action/EntityInsertAction.java
core/branches/Branch_3_2_4_SP1_CP/src/org/hibernate/engine/SessionImplementor.java
core/branches/Branch_3_2_4_SP1_CP/src/org/hibernate/engine/TwoPhaseLoad.java
core/branches/Branch_3_2_4_SP1_CP/src/org/hibernate/impl/SessionImpl.java
core/branches/Branch_3_2_4_SP1_CP/src/org/hibernate/impl/StatelessSessionImpl.java
core/branches/Branch_3_2_4_SP1_CP/test/org/hibernate/test/propertyref/component/complete/CompleteComponentPropertyRefTest.java
core/branches/Branch_3_2_4_SP1_CP/test/org/hibernate/test/propertyref/component/partial/PartialComponentPropertyRefTest.java
Log:
JBPAPP-4895 HHH-5490 dirty data be inserted into 2L cache
Modified: core/branches/Branch_3_2_4_SP1_CP/build.xml
===================================================================
--- core/branches/Branch_3_2_4_SP1_CP/build.xml 2010-09-28 06:15:52 UTC (rev 20730)
+++ core/branches/Branch_3_2_4_SP1_CP/build.xml 2010-09-28 06:47:19 UTC (rev 20731)
@@ -60,7 +60,7 @@
<property name="javac.debug" value="on"/>
<property name="javac.optimize" value="off"/>
<property name="javac.target" value="1.5"/>
- <property name="javac.source" value="1.4"/>
+ <property name="javac.source" value="1.5"/>
<property name="jar.driver" value="${dir.jdbc}/hsqldb.jar"/>
<!-- JAR and dist file names -->
Modified:
core/branches/Branch_3_2_4_SP1_CP/src/org/hibernate/action/EntityIdentityInsertAction.java
===================================================================
---
core/branches/Branch_3_2_4_SP1_CP/src/org/hibernate/action/EntityIdentityInsertAction.java 2010-09-28
06:15:52 UTC (rev 20730)
+++
core/branches/Branch_3_2_4_SP1_CP/src/org/hibernate/action/EntityIdentityInsertAction.java 2010-09-28
06:47:19 UTC (rev 20731)
@@ -52,6 +52,7 @@
//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.getEntityMode() );
+ getSession().registerInsertedKey( getPersister(), generatedId );
}
Modified:
core/branches/Branch_3_2_4_SP1_CP/src/org/hibernate/action/EntityInsertAction.java
===================================================================
---
core/branches/Branch_3_2_4_SP1_CP/src/org/hibernate/action/EntityInsertAction.java 2010-09-28
06:15:52 UTC (rev 20730)
+++
core/branches/Branch_3_2_4_SP1_CP/src/org/hibernate/action/EntityInsertAction.java 2010-09-28
06:47:19 UTC (rev 20731)
@@ -70,6 +70,8 @@
entry.postUpdate(instance, state, version);
}
+ getSession().registerInsertedKey( getPersister(), getId() );
+
}
final SessionFactoryImplementor factory = getSession().getFactory();
Modified:
core/branches/Branch_3_2_4_SP1_CP/src/org/hibernate/engine/SessionImplementor.java
===================================================================
---
core/branches/Branch_3_2_4_SP1_CP/src/org/hibernate/engine/SessionImplementor.java 2010-09-28
06:15:52 UTC (rev 20730)
+++
core/branches/Branch_3_2_4_SP1_CP/src/org/hibernate/engine/SessionImplementor.java 2010-09-28
06:47:19 UTC (rev 20731)
@@ -299,4 +299,23 @@
* @return True if the session is closed; false otherwise.
*/
public boolean isClosed();
+
+ /**
+ * 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);
+
}
Modified: core/branches/Branch_3_2_4_SP1_CP/src/org/hibernate/engine/TwoPhaseLoad.java
===================================================================
---
core/branches/Branch_3_2_4_SP1_CP/src/org/hibernate/engine/TwoPhaseLoad.java 2010-09-28
06:15:52 UTC (rev 20730)
+++
core/branches/Branch_3_2_4_SP1_CP/src/org/hibernate/engine/TwoPhaseLoad.java 2010-09-28
06:47:19 UTC (rev 20731)
@@ -153,19 +153,27 @@
session.getEntityMode(),
session.getFactory()
);
- boolean put = persister.getCache().put(
- cacheKey,
- persister.getCacheEntryStructure().structure(entry),
- session.getTimestamp(),
- version,
- persister.isVersioned() ?
- persister.getVersionType().getComparator() :
- null,
- useMinimalPuts(session, entityEntry)
- ); //we could use persister.hasLazyProperties() instead of true
-
- if ( put && factory.getStatistics().isStatisticsEnabled() ) {
- factory.getStatisticsImplementor().secondLevelCachePut(
persister.getCache().getRegionName() );
+ if (session.wasInsertedDuringTransaction(persister, id)) {
+ persister.getCache().update(cacheKey,
+ persister.getCacheEntryStructure().structure(entry),
+ version, version);
+ } else {
+ boolean put = persister.getCache().put(
+ cacheKey,
+ persister.getCacheEntryStructure().structure(entry),
+ session.getTimestamp(),
+ version,
+ persister.isVersioned() ? persister.getVersionType()
+ .getComparator() : null,
+ useMinimalPuts(session, entityEntry)); // we could use
+ // persister.hasLazyProperties()
+ // instead of
+ // true
+
+ if (put && factory.getStatistics().isStatisticsEnabled()) {
+ factory.getStatisticsImplementor().secondLevelCachePut(
+ persister.getCache().getRegionName());
+ }
}
}
Modified: core/branches/Branch_3_2_4_SP1_CP/src/org/hibernate/impl/SessionImpl.java
===================================================================
--- core/branches/Branch_3_2_4_SP1_CP/src/org/hibernate/impl/SessionImpl.java 2010-09-28
06:15:52 UTC (rev 20730)
+++ core/branches/Branch_3_2_4_SP1_CP/src/org/hibernate/impl/SessionImpl.java 2010-09-28
06:47:19 UTC (rev 20731)
@@ -6,6 +6,7 @@
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.sql.Connection;
+import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
@@ -147,7 +148,6 @@
private transient Session rootSession;
private transient Map childSessionsByEntityMode;
-
/**
* Constructor used in building "child sessions".
*
@@ -420,6 +420,7 @@
public void afterTransactionCompletion(boolean success, Transaction tx) {
log.trace( "after transaction completion" );
+ cleanUpInsertedKeysAfterTransaction();
persistenceContext.afterTransactionCompletion();
actionQueue.afterTransactionCompletion(success);
if ( rootSession == null && tx != null ) {
@@ -1020,7 +1021,48 @@
flush();
}
+ private HashMap<String,List<Serializable>> insertedKeysMap;
+ /**
+ * {@inheritDoc}
+ */
+ public void registerInsertedKey(EntityPersister persister, Serializable id) {
+ // we only are about registering 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();
+ }
+ }
public Filter getEnabledFilter(String filterName) {
checkTransactionSynchStatus();
return (Filter) enabledFilters.get(filterName);
Modified:
core/branches/Branch_3_2_4_SP1_CP/src/org/hibernate/impl/StatelessSessionImpl.java
===================================================================
---
core/branches/Branch_3_2_4_SP1_CP/src/org/hibernate/impl/StatelessSessionImpl.java 2010-09-28
06:15:52 UTC (rev 20730)
+++
core/branches/Branch_3_2_4_SP1_CP/src/org/hibernate/impl/StatelessSessionImpl.java 2010-09-28
06:47:19 UTC (rev 20731)
@@ -627,4 +627,17 @@
return result;
}
+
+ public void registerInsertedKey(EntityPersister persister, Serializable id) {
+ // nothing to do
+
+ }
+
+
+ public boolean wasInsertedDuringTransaction(EntityPersister persister,
+ Serializable id) {
+ // not in any meaning we need to worry about here.
+ return false;
+ }
+
}
Modified:
core/branches/Branch_3_2_4_SP1_CP/test/org/hibernate/test/propertyref/component/complete/CompleteComponentPropertyRefTest.java
===================================================================
---
core/branches/Branch_3_2_4_SP1_CP/test/org/hibernate/test/propertyref/component/complete/CompleteComponentPropertyRefTest.java 2010-09-28
06:15:52 UTC (rev 20730)
+++
core/branches/Branch_3_2_4_SP1_CP/test/org/hibernate/test/propertyref/component/complete/CompleteComponentPropertyRefTest.java 2010-09-28
06:47:19 UTC (rev 20731)
@@ -26,6 +26,8 @@
}
public void testComponentPropertyRef() {
+ Session s = openSession();
+ s.beginTransaction();
Person p = new Person();
p.setIdentity( new Identity() );
Account a = new Account();
@@ -33,13 +35,14 @@
a.setOwner(p);
p.getIdentity().setName("Gavin");
p.getIdentity().setSsn("123-12-1234");
- Session s = openSession();
- Transaction tx = s.beginTransaction();
+
s.persist(p);
s.persist(a);
- s.flush();
- s.clear();
-
+ s.getTransaction().commit();
+ s.close();
+
+ s = openSession();
+ s.beginTransaction();
a = (Account) s.createQuery("from Account a left join fetch
a.owner").uniqueResult();
assertTrue( Hibernate.isInitialized( a.getOwner() ) );
assertNotNull( a.getOwner() );
@@ -65,7 +68,7 @@
s.delete( a );
s.delete( a.getOwner() );
- tx.commit();
+ s.getTransaction().commit();
s.close();
}
}
Modified:
core/branches/Branch_3_2_4_SP1_CP/test/org/hibernate/test/propertyref/component/partial/PartialComponentPropertyRefTest.java
===================================================================
---
core/branches/Branch_3_2_4_SP1_CP/test/org/hibernate/test/propertyref/component/partial/PartialComponentPropertyRefTest.java 2010-09-28
06:15:52 UTC (rev 20730)
+++
core/branches/Branch_3_2_4_SP1_CP/test/org/hibernate/test/propertyref/component/partial/PartialComponentPropertyRefTest.java 2010-09-28
06:47:19 UTC (rev 20731)
@@ -27,6 +27,8 @@
}
public void testComponentPropertyRef() {
+ Session s = openSession();
+ s.beginTransaction();
Person p = new Person();
p.setIdentity( new Identity() );
Account a = new Account();
@@ -34,13 +36,15 @@
a.setOwner(p);
p.getIdentity().setName("Gavin");
p.getIdentity().setSsn("123-12-1234");
- Session s = openSession();
- Transaction tx = s.beginTransaction();
+
s.persist(p);
s.persist(a);
- s.flush();
- s.clear();
+ s.getTransaction().commit();
+ s.close();
+ s = openSession();
+ s.beginTransaction();
+
a = (Account) s.createQuery("from Account a left join fetch
a.owner").uniqueResult();
assertTrue( Hibernate.isInitialized( a.getOwner() ) );
assertNotNull( a.getOwner() );
@@ -66,7 +70,7 @@
s.delete( a );
s.delete( a.getOwner() );
- tx.commit();
+ s.getTransaction().commit();
s.close();
}
}