Skip to content

Commit 4fd7680

Browse files
rvansadreab8
authored andcommitted
HHH-9977 Consider options for passing Session to caching SPI calls
* Passing SessionImplementor to all the calls executed in transactional context
1 parent 7990630 commit 4fd7680

File tree

69 files changed

+528
-455
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+528
-455
lines changed

hibernate-core/src/main/java/org/hibernate/action/internal/CollectionAction.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public final void beforeExecutions() throws CacheException {
8383
session.getFactory(),
8484
session.getTenantIdentifier()
8585
);
86-
final SoftLock lock = cache.lockItem( ck, null );
86+
final SoftLock lock = cache.lockItem( session, ck, null );
8787
// the old behavior used key as opposed to getKey()
8888
afterTransactionProcess = new CacheCleanupProcess( key, persister, lock );
8989
}
@@ -136,7 +136,7 @@ protected final void evict() throws CacheException {
136136
session.getFactory(),
137137
session.getTenantIdentifier()
138138
);
139-
cache.remove( ck );
139+
cache.remove( session, ck);
140140
}
141141
}
142142

@@ -180,7 +180,7 @@ public void doAfterTransactionCompletion(boolean success, SessionImplementor ses
180180
session.getFactory(),
181181
session.getTenantIdentifier()
182182
);
183-
cache.unlockItem( ck, lock );
183+
cache.unlockItem( session, ck, lock );
184184
}
185185
}
186186

hibernate-core/src/main/java/org/hibernate/action/internal/EntityDeleteAction.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public void execute() throws HibernateException {
8888
if ( persister.hasCache() ) {
8989
final EntityRegionAccessStrategy cache = persister.getCacheAccessStrategy();
9090
ck = cache.generateCacheKey( id, persister, session.getFactory(), session.getTenantIdentifier() );
91-
lock = cache.lockItem( ck, version );
91+
lock = cache.lockItem( session, ck, version );
9292
}
9393
else {
9494
ck = null;
@@ -113,7 +113,7 @@ public void execute() throws HibernateException {
113113
persistenceContext.removeProxy( entry.getEntityKey() );
114114

115115
if ( persister.hasCache() ) {
116-
persister.getCacheAccessStrategy().remove( ck );
116+
persister.getCacheAccessStrategy().remove( session, ck);
117117
}
118118

119119
persistenceContext.getNaturalIdHelper().removeSharedNaturalIdCrossReference( persister, id, naturalIdValues );
@@ -194,7 +194,7 @@ public void doAfterTransactionCompletion(boolean success, SessionImplementor ses
194194
session.getFactory(),
195195
session.getTenantIdentifier()
196196
);
197-
cache.unlockItem( ck, lock );
197+
cache.unlockItem( session, ck, lock );
198198
}
199199
postCommitDelete( success );
200200
}

hibernate-core/src/main/java/org/hibernate/action/internal/EntityInsertAction.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,12 +138,13 @@ public void execute() throws HibernateException {
138138
}
139139

140140
private boolean cacheInsert(EntityPersister persister, Object ck) {
141+
SessionImplementor session = getSession();
141142
try {
142-
getSession().getEventListenerManager().cachePutStart();
143-
return persister.getCacheAccessStrategy().insert( ck, cacheEntry, version );
143+
session.getEventListenerManager().cachePutStart();
144+
return persister.getCacheAccessStrategy().insert( session, ck, cacheEntry, version);
144145
}
145146
finally {
146-
getSession().getEventListenerManager().cachePutEnd();
147+
session.getEventListenerManager().cachePutEnd();
147148
}
148149
}
149150

@@ -224,10 +225,11 @@ public void doAfterTransactionCompletion(boolean success, SessionImplementor ses
224225
}
225226

226227
private boolean cacheAfterInsert(EntityRegionAccessStrategy cache, Object ck) {
227-
final SessionEventListenerManager eventListenerManager = getSession().getEventListenerManager();
228+
SessionImplementor session = getSession();
229+
final SessionEventListenerManager eventListenerManager = session.getEventListenerManager();
228230
try {
229231
eventListenerManager.cachePutStart();
230-
return cache.afterInsert( ck, cacheEntry, version );
232+
return cache.afterInsert( session, ck, cacheEntry, version );
231233
}
232234
finally {
233235
eventListenerManager.cachePutEnd();

hibernate-core/src/main/java/org/hibernate/action/internal/EntityUpdateAction.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ public void execute() throws HibernateException {
135135
factory,
136136
session.getTenantIdentifier()
137137
);
138-
lock = cache.lockItem( ck, previousVersion );
138+
lock = cache.lockItem( session, ck, previousVersion );
139139
}
140140
else {
141141
ck = null;
@@ -186,7 +186,7 @@ public void execute() throws HibernateException {
186186

187187
if ( persister.hasCache() ) {
188188
if ( persister.isCacheInvalidationRequired() || entry.getStatus()!= Status.MANAGED ) {
189-
persister.getCacheAccessStrategy().remove( ck );
189+
persister.getCacheAccessStrategy().remove( session, ck);
190190
}
191191
else {
192192
//TODO: inefficient if that cache is just going to ignore the updated state!
@@ -216,12 +216,13 @@ public void execute() throws HibernateException {
216216
}
217217

218218
private boolean cacheUpdate(EntityPersister persister, Object previousVersion, Object ck) {
219+
final SessionImplementor session = getSession();
219220
try {
220-
getSession().getEventListenerManager().cachePutStart();
221-
return persister.getCacheAccessStrategy().update( ck, cacheEntry, nextVersion, previousVersion );
221+
session.getEventListenerManager().cachePutStart();
222+
return persister.getCacheAccessStrategy().update( session, ck, cacheEntry, nextVersion, previousVersion );
222223
}
223224
finally {
224-
getSession().getEventListenerManager().cachePutEnd();
225+
session.getEventListenerManager().cachePutEnd();
225226
}
226227
}
227228

@@ -327,17 +328,18 @@ public void doAfterTransactionCompletion(boolean success, SessionImplementor ses
327328
}
328329
}
329330
else {
330-
cache.unlockItem( ck, lock );
331+
cache.unlockItem(session, ck, lock );
331332
}
332333
}
333334
postCommitUpdate( success );
334335
}
335336

336337
private boolean cacheAfterUpdate(EntityRegionAccessStrategy cache, Object ck) {
337-
SessionEventListenerManager eventListenerManager = getSession().getEventListenerManager();
338+
final SessionImplementor session = getSession();
339+
SessionEventListenerManager eventListenerManager = session.getEventListenerManager();
338340
try {
339341
eventListenerManager.cachePutStart();
340-
return cache.afterUpdate( ck, cacheEntry, nextVersion, previousVersion, lock );
342+
return cache.afterUpdate( session, ck, cacheEntry, nextVersion, previousVersion, lock );
341343
}
342344
finally {
343345
eventListenerManager.cachePutEnd();

hibernate-core/src/main/java/org/hibernate/cache/internal/StandardQueryCache.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ public boolean put(
132132

133133
try {
134134
session.getEventListenerManager().cachePutStart();
135-
cacheRegion.put( key, cacheable );
135+
cacheRegion.put( session, key, cacheable );
136136
}
137137
finally {
138138
session.getEventListenerManager().cachePutEnd();
@@ -221,7 +221,7 @@ private List getCachedResults(QueryKey key, SessionImplementor session) {
221221
List cacheable = null;
222222
try {
223223
session.getEventListenerManager().cacheGetStart();
224-
cacheable = (List) cacheRegion.get( key );
224+
cacheable = (List) cacheRegion.get( session, key );
225225
}
226226
finally {
227227
session.getEventListenerManager().cacheGetEnd( cacheable != null );

hibernate-core/src/main/java/org/hibernate/cache/spi/GeneralDataRegion.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
package org.hibernate.cache.spi;
88

99
import org.hibernate.cache.CacheException;
10+
import org.hibernate.engine.spi.SessionImplementor;
1011

1112
/**
1213
* Contract for general-purpose cache regions.
@@ -18,20 +19,24 @@ public interface GeneralDataRegion extends Region {
1819
/**
1920
* Get an item from the cache.
2021
*
22+
*
23+
* @param session
2124
* @param key The key of the item to be retrieved.
2225
* @return the cached object or <tt>null</tt>
2326
* @throws org.hibernate.cache.CacheException Indicates a problem accessing the item or region.
2427
*/
25-
public Object get(Object key) throws CacheException;
28+
public Object get(SessionImplementor session, Object key) throws CacheException;
2629

2730
/**
2831
* Put an item into the cache.
2932
*
33+
*
34+
* @param session
3035
* @param key The key under which to cache the item.
3136
* @param value The item to cache.
3237
* @throws CacheException Indicates a problem accessing the region.
3338
*/
34-
public void put(Object key, Object value) throws CacheException;
39+
public void put(SessionImplementor session, Object key, Object value) throws CacheException;
3540

3641
/**
3742
* Evict an item from the cache immediately (without regard for transaction

hibernate-core/src/main/java/org/hibernate/cache/spi/UpdateTimestampsCache.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public void preInvalidate(Serializable[] spaces, SessionImplementor session) thr
9292

9393
//put() has nowait semantics, is this really appropriate?
9494
//note that it needs to be async replication, never local or sync
95-
region.put( space, ts );
95+
region.put( session, space, ts );
9696
}
9797
finally {
9898
session.getEventListenerManager().cachePutEnd();
@@ -128,7 +128,7 @@ public void invalidate(Serializable[] spaces, SessionImplementor session) throws
128128

129129
//put() has nowait semantics, is this really appropriate?
130130
//note that it needs to be async replication, never local or sync
131-
region.put( space, ts );
131+
region.put( session, space, ts );
132132
}
133133
finally {
134134
session.getEventListenerManager().cachePutEnd();
@@ -189,7 +189,7 @@ private Long getLastUpdateTimestampForSpace(Serializable space, SessionImplement
189189
Long ts = null;
190190
try {
191191
session.getEventListenerManager().cacheGetStart();
192-
ts = (Long) region.get( space );
192+
ts = (Long) region.get( session, space );
193193
}
194194
finally {
195195
session.getEventListenerManager().cacheGetEnd( ts != null );

hibernate-core/src/main/java/org/hibernate/cache/spi/access/EntityRegionAccessStrategy.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import org.hibernate.cache.CacheException;
1010
import org.hibernate.cache.spi.EntityRegion;
1111
import org.hibernate.engine.spi.SessionFactoryImplementor;
12+
import org.hibernate.engine.spi.SessionImplementor;
1213
import org.hibernate.persister.entity.EntityPersister;
1314

1415
/**
@@ -60,46 +61,51 @@ public interface EntityRegionAccessStrategy extends RegionAccessStrategy {
6061
* instead of calling evict().
6162
* This method is used by "synchronous" concurrency strategies.
6263
*
64+
* @param session Current session
6365
* @param key The item key
6466
* @param value The item
6567
* @param version The item's version value
6668
* @return Were the contents of the cache actual changed by this operation?
6769
* @throws CacheException Propagated from underlying {@link org.hibernate.cache.spi.Region}
6870
*/
69-
public boolean insert(Object key, Object value, Object version) throws CacheException;
71+
public boolean insert(SessionImplementor session, Object key, Object value, Object version) throws CacheException;
7072

7173
/**
7274
* Called after an item has been inserted (after the transaction completes),
7375
* instead of calling release().
7476
* This method is used by "asynchronous" concurrency strategies.
7577
*
78+
* @param session Current session
7679
* @param key The item key
7780
* @param value The item
7881
* @param version The item's version value
7982
* @return Were the contents of the cache actual changed by this operation?
8083
* @throws CacheException Propagated from underlying {@link org.hibernate.cache.spi.Region}
8184
*/
82-
public boolean afterInsert(Object key, Object value, Object version) throws CacheException;
85+
public boolean afterInsert(SessionImplementor session, Object key, Object value, Object version) throws CacheException;
8386

8487
/**
8588
* Called after an item has been updated (before the transaction completes),
8689
* instead of calling evict(). This method is used by "synchronous" concurrency
8790
* strategies.
8891
*
92+
*
93+
* @param session Current session
8994
* @param key The item key
9095
* @param value The item
9196
* @param currentVersion The item's current version value
9297
* @param previousVersion The item's previous version value
9398
* @return Were the contents of the cache actual changed by this operation?
9499
* @throws CacheException Propagated from underlying {@link org.hibernate.cache.spi.Region}
95100
*/
96-
public boolean update(Object key, Object value, Object currentVersion, Object previousVersion) throws CacheException;
101+
public boolean update(SessionImplementor session, Object key, Object value, Object currentVersion, Object previousVersion) throws CacheException;
97102

98103
/**
99104
* Called after an item has been updated (after the transaction completes),
100105
* instead of calling release(). This method is used by "asynchronous"
101106
* concurrency strategies.
102107
*
108+
* @param session Current session
103109
* @param key The item key
104110
* @param value The item
105111
* @param currentVersion The item's current version value
@@ -108,5 +114,5 @@ public interface EntityRegionAccessStrategy extends RegionAccessStrategy {
108114
* @return Were the contents of the cache actual changed by this operation?
109115
* @throws CacheException Propagated from underlying {@link org.hibernate.cache.spi.Region}
110116
*/
111-
public boolean afterUpdate(Object key, Object value, Object currentVersion, Object previousVersion, SoftLock lock) throws CacheException;
117+
public boolean afterUpdate(SessionImplementor session, Object key, Object value, Object currentVersion, Object previousVersion, SoftLock lock) throws CacheException;
112118
}

hibernate-core/src/main/java/org/hibernate/cache/spi/access/NaturalIdRegionAccessStrategy.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@
2727
* {@link #lockRegion} -> {@link #removeAll} -> {@link #unlockRegion}
2828
* <p/>
2929
* IMPORTANT : NaturalIds are not versioned so {@code null} will always be passed to the version parameter to:<ul>
30-
* <li>{@link #putFromLoad(Object, Object, long, Object)}</li>
31-
* <li>{@link #putFromLoad(Object, Object, long, Object, boolean)}</li>
32-
* <li>{@link #lockItem(Object, Object)}</li>
30+
* <li>{@link RegionAccessStrategy#putFromLoad(SessionImplementor, Object, Object, long, Object)}</li>
31+
* <li>{@link RegionAccessStrategy#putFromLoad(SessionImplementor, Object, Object, long, Object, boolean)}</li>
32+
* <li>{@link RegionAccessStrategy#lockItem(SessionImplementor, Object, Object)}</li>
3333
* </ul>
3434
*
3535
* @author Gavin King
@@ -68,47 +68,51 @@ public interface NaturalIdRegionAccessStrategy extends RegionAccessStrategy {
6868
* instead of calling evict().
6969
* This method is used by "synchronous" concurrency strategies.
7070
*
71+
* @param session Current session
7172
* @param key The item key
7273
* @param value The item
7374
* @return Were the contents of the cache actual changed by this operation?
7475
* @throws CacheException Propagated from underlying {@link org.hibernate.cache.spi.Region}
7576
*/
76-
public boolean insert(Object key, Object value) throws CacheException;
77+
public boolean insert(SessionImplementor session, Object key, Object value) throws CacheException;
7778

7879
/**
7980
* Called after an item has been inserted (after the transaction completes),
8081
* instead of calling release().
8182
* This method is used by "asynchronous" concurrency strategies.
8283
*
84+
* @param session Current session
8385
* @param key The item key
8486
* @param value The item
8587
* @return Were the contents of the cache actual changed by this operation?
8688
* @throws CacheException Propagated from underlying {@link org.hibernate.cache.spi.Region}
8789
*/
88-
public boolean afterInsert(Object key, Object value) throws CacheException;
90+
public boolean afterInsert(SessionImplementor session, Object key, Object value) throws CacheException;
8991

9092
/**
9193
* Called after an item has been updated (before the transaction completes),
9294
* instead of calling evict(). This method is used by "synchronous" concurrency
9395
* strategies.
9496
*
97+
* @param session Current session
9598
* @param key The item key
9699
* @param value The item
97100
* @return Were the contents of the cache actual changed by this operation?
98101
* @throws CacheException Propagated from underlying {@link org.hibernate.cache.spi.Region}
99102
*/
100-
public boolean update(Object key, Object value) throws CacheException;
103+
public boolean update(SessionImplementor session, Object key, Object value) throws CacheException;
101104

102105
/**
103106
* Called after an item has been updated (after the transaction completes),
104107
* instead of calling release(). This method is used by "asynchronous"
105108
* concurrency strategies.
106109
*
110+
* @param session Current session
107111
* @param key The item key
108112
* @param value The item
109113
* @param lock The lock previously obtained from {@link #lockItem}
110114
* @return Were the contents of the cache actual changed by this operation?
111115
* @throws CacheException Propogated from underlying {@link org.hibernate.cache.spi.Region}
112116
*/
113-
public boolean afterUpdate(Object key, Object value, SoftLock lock) throws CacheException;
117+
public boolean afterUpdate(SessionImplementor session, Object key, Object value, SoftLock lock) throws CacheException;
114118
}

0 commit comments

Comments
 (0)