Skip to content

Commit 984125e

Browse files
rvansagalderz
authored andcommitted
HHH-9868, HHH-9881 Replaced access to TransactionManager with Session
1 parent 1f24fa6 commit 984125e

File tree

11 files changed

+199
-202
lines changed

11 files changed

+199
-202
lines changed

hibernate-infinispan/hibernate-infinispan.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ dependencies {
2424
testCompile( libraries.jnp_client )
2525
testCompile( libraries.jnp_server )
2626
testCompile( libraries.rhq )
27+
testCompile( libraries.mockito )
2728
testCompile ('mysql:mysql-connector-java:5.1.17')
2829
}
2930

hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/access/PutFromLoadValidator.java

Lines changed: 41 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
*/
77
package org.hibernate.cache.infinispan.access;
88

9-
import javax.transaction.RollbackException;
109
import javax.transaction.Status;
1110
import javax.transaction.SystemException;
1211
import javax.transaction.Transaction;
@@ -24,7 +23,6 @@
2423
import java.util.concurrent.TimeUnit;
2524
import java.util.concurrent.locks.ReentrantLock;
2625

27-
import org.hibernate.cache.CacheException;
2826
import org.hibernate.cache.infinispan.InfinispanRegionFactory;
2927
import org.hibernate.cache.infinispan.util.CacheCommandInitializer;
3028
import org.hibernate.cache.spi.RegionFactory;
@@ -53,9 +51,9 @@
5351
* not find data is:
5452
* <p/>
5553
* <ol>
56-
* <li> Call {@link #registerPendingPut(Object, long)}</li>
54+
* <li> Call {@link #registerPendingPut(SessionImplementor, Object, long)}</li>
5755
* <li> Read the database</li>
58-
* <li> Call {@link #acquirePutFromLoadLock(Object, long)}
56+
* <li> Call {@link #acquirePutFromLoadLock(SessionImplementor, Object, long)}
5957
* <li> if above returns <code>null</code>, the thread should not cache the data;
6058
* only if above returns instance of <code>AcquiredLock</code>, put data in the cache and...</li>
6159
* <li> then call {@link #releasePutFromLoadLock(Object, Lock)}</li>
@@ -68,18 +66,18 @@
6866
* call
6967
* <p/>
7068
* <ul>
71-
* <li> {@link #beginInvalidatingKey(Object)} (for a single key invalidation)</li>
69+
* <li> {@link #beginInvalidatingKey(SessionImplementor, Object)} (for a single key invalidation)</li>
7270
* <li>or {@link #beginInvalidatingRegion()} followed by {@link #endInvalidatingRegion()}
7371
* (for a general invalidation all pending puts)</li>
7472
* </ul>
75-
* After transaction commit (when the DB is updated) {@link #endInvalidatingKey(Object)} should
73+
* After transaction commit (when the DB is updated) {@link #endInvalidatingKey(SessionImplementor, Object)} should
7674
* be called in order to allow further attempts to cache entry.
7775
* </p>
7876
* <p/>
7977
* <p>
8078
* This class also supports the concept of "naked puts", which are calls to
81-
* {@link #acquirePutFromLoadLock(Object, long)} without a preceding {@link #registerPendingPut(Object, long)}.
82-
* Besides not acquiring lock in {@link #registerPendingPut(Object, long)} this can happen when collection
79+
* {@link #acquirePutFromLoadLock(SessionImplementor, Object, long)} without a preceding {@link #registerPendingPut(SessionImplementor, Object, long)}.
80+
* Besides not acquiring lock in {@link #registerPendingPut(SessionImplementor, Object, long)} this can happen when collection
8381
* elements are loaded after the collection has not been found in the cache, where the elements
8482
* don't have their own table but can be listed as 'select ... from Element where collection_id = ...'.
8583
* Naked puts are handled according to txTimestamp obtained by calling {@link RegionFactory#nextTimestamp()}
@@ -141,8 +139,8 @@ public class PutFromLoadValidator {
141139

142140
/**
143141
* Creates a new put from load validator instance.
144-
*
145-
* @param cache Cache instance on which to store pending put information.
142+
*
143+
* @param cache Cache instance on which to store pending put information.
146144
* @param transactionManager Transaction manager
147145
*/
148146
public PutFromLoadValidator(AdvancedCache cache, TransactionManager transactionManager) {
@@ -247,20 +245,15 @@ public static void removeFromCache(AdvancedCache cache) {
247245
}
248246

249247
public void setCurrentSession(SessionImplementor session) {
250-
// we register synchronizations directly on JTA transactions, let's make this noop with TM
251-
if (transactionManager == null) {
252-
currentSession.set(session);
253-
}
248+
currentSession.set(session);
254249
}
255250

256251
public void resetCurrentSession() {
257-
if (transactionManager == null) {
258-
currentSession.remove();
259-
}
252+
currentSession.remove();
260253
}
261254

262255
/**
263-
* Marker for lock acquired in {@link #acquirePutFromLoadLock(Object, long)}
256+
* Marker for lock acquired in {@link #acquirePutFromLoadLock(SessionImplementor, Object, long)}
264257
*/
265258
public static abstract class Lock {
266259
private Lock() {}
@@ -274,13 +267,14 @@ private Lock() {}
274267
* should always be matched with a call to {@link #releasePutFromLoadLock(Object, Lock)}.
275268
* </p>
276269
*
270+
* @param session
277271
* @param key the key
278272
*
279273
* @param txTimestamp
280274
* @return <code>AcquiredLock</code> if the lock is acquired and the cache put
281275
* can proceed; <code>null</code> if the data should not be cached
282276
*/
283-
public Lock acquirePutFromLoadLock(Object key, long txTimestamp) {
277+
public Lock acquirePutFromLoadLock(SessionImplementor session, Object key, long txTimestamp) {
284278
if (trace) {
285279
log.tracef("acquirePutFromLoadLock(%s#%s, %d)", cache.getName(), key, txTimestamp);
286280
}
@@ -305,7 +299,7 @@ public Lock acquirePutFromLoadLock(Object key, long txTimestamp) {
305299
}
306300
continue;
307301
}
308-
final PendingPut toCancel = pending.remove(getLocalLockOwner());
302+
final PendingPut toCancel = pending.remove(session);
309303
if (toCancel != null) {
310304
valid = !toCancel.completed;
311305
toCancel.completed = true;
@@ -359,7 +353,7 @@ else if (pending.lastInvalidationEnd != Long.MIN_VALUE) {
359353
}
360354
}
361355

362-
PendingPut pendingPut = new PendingPut(getLocalLockOwner());
356+
PendingPut pendingPut = new PendingPut(session);
363357
pending = new PendingPutMap(pendingPut);
364358
PendingPutMap existing = pendingPuts.putIfAbsent(key, pending);
365359
if (existing != null) {
@@ -388,7 +382,7 @@ else if (t instanceof Error) {
388382

389383
/**
390384
* Releases the lock previously obtained by a call to
391-
* {@link #acquirePutFromLoadLock(Object, long)}.
385+
* {@link #acquirePutFromLoadLock(SessionImplementor, Object, long)}.
392386
*
393387
* @param key the key
394388
*/
@@ -407,9 +401,9 @@ public void releasePutFromLoadLock(Object key, Lock lock) {
407401
}
408402

409403
/**
410-
* Invalidates all {@link #registerPendingPut(Object, long) previously registered pending puts} ensuring a subsequent call to
411-
* {@link #acquirePutFromLoadLock(Object, long)} will return <code>false</code>. <p> This method will block until any
412-
* concurrent thread that has {@link #acquirePutFromLoadLock(Object, long) acquired the putFromLoad lock} for the any key has
404+
* Invalidates all {@link #registerPendingPut(SessionImplementor, Object, long) previously registered pending puts} ensuring a subsequent call to
405+
* {@link #acquirePutFromLoadLock(SessionImplementor, Object, long)} will return <code>false</code>. <p> This method will block until any
406+
* concurrent thread that has {@link #acquirePutFromLoadLock(SessionImplementor, Object, long) acquired the putFromLoad lock} for the any key has
413407
* released the lock. This allows the caller to be certain the putFromLoad will not execute after this method returns,
414408
* possibly caching stale data. </p>
415409
*
@@ -506,16 +500,17 @@ public void endInvalidatingRegion() {
506500

507501
/**
508502
* Notifies this validator that it is expected that a database read followed by a subsequent {@link
509-
* #acquirePutFromLoadLock(Object, long)} call will occur. The intent is this method would be called following a cache miss
503+
* #acquirePutFromLoadLock(SessionImplementor, Object, long)} call will occur. The intent is this method would be called following a cache miss
510504
* wherein it is expected that a database read plus cache put will occur. Calling this method allows the validator to
511505
* treat the subsequent <code>acquirePutFromLoadLock</code> as if the database read occurred when this method was
512506
* invoked. This allows the validator to compare the timestamp of this call against the timestamp of subsequent removal
513507
* notifications.
514508
*
509+
* @param session
515510
* @param key key that will be used for subsequent cache put
516511
* @param txTimestamp
517512
*/
518-
public void registerPendingPut(Object key, long txTimestamp) {
513+
public void registerPendingPut(SessionImplementor session, Object key, long txTimestamp) {
519514
long invalidationTimestamp = this.regionInvalidationTimestamp;
520515
if (txTimestamp <= invalidationTimestamp) {
521516
boolean skip;
@@ -543,7 +538,7 @@ public void registerPendingPut(Object key, long txTimestamp) {
543538
}
544539
}
545540

546-
final PendingPut pendingPut = new PendingPut( getLocalLockOwner() );
541+
final PendingPut pendingPut = new PendingPut( session );
547542
final PendingPutMap pendingForKey = new PendingPutMap( pendingPut );
548543

549544
for (;;) {
@@ -586,21 +581,23 @@ public void registerPendingPut(Object key, long txTimestamp) {
586581

587582
/**
588583
* Calls {@link #beginInvalidatingKey(Object, Object)} with current transaction or thread.
584+
*
585+
* @param session
589586
* @param key
590587
* @return
591588
*/
592-
public boolean beginInvalidatingKey(Object key) {
593-
return beginInvalidatingKey(key, getLocalLockOwner());
589+
public boolean beginInvalidatingKey(SessionImplementor session, Object key) {
590+
return beginInvalidatingKey(key, session);
594591
}
595592

596593
/**
597-
* Invalidates any {@link #registerPendingPut(Object, long) previously registered pending puts}
598-
* and disables further registrations ensuring a subsequent call to {@link #acquirePutFromLoadLock(Object, long)}
594+
* Invalidates any {@link #registerPendingPut(SessionImplementor, Object, long) previously registered pending puts}
595+
* and disables further registrations ensuring a subsequent call to {@link #acquirePutFromLoadLock(SessionImplementor, Object, long)}
599596
* will return <code>false</code>. <p> This method will block until any concurrent thread that has
600-
* {@link #acquirePutFromLoadLock(Object, long) acquired the putFromLoad lock} for the given key
597+
* {@link #acquirePutFromLoadLock(SessionImplementor, Object, long) acquired the putFromLoad lock} for the given key
601598
* has released the lock. This allows the caller to be certain the putFromLoad will not execute after this method
602599
* returns, possibly caching stale data. </p>
603-
* After this transaction completes, {@link #endInvalidatingKey(Object)} needs to be called }
600+
* After this transaction completes, {@link #endInvalidatingKey(SessionImplementor, Object)} needs to be called }
604601
*
605602
* @param key key identifying data whose pending puts should be invalidated
606603
*
@@ -643,16 +640,18 @@ public boolean beginInvalidatingKey(Object key, Object lockOwner) {
643640

644641
/**
645642
* Calls {@link #endInvalidatingKey(Object, Object)} with current transaction or thread.
643+
*
644+
* @param session
646645
* @param key
647646
* @return
648647
*/
649-
public boolean endInvalidatingKey(Object key) {
650-
return endInvalidatingKey(key, getLocalLockOwner());
648+
public boolean endInvalidatingKey(SessionImplementor session, Object key) {
649+
return endInvalidatingKey(key, session);
651650
}
652651

653652
/**
654653
* Called after the transaction completes, allowing caching of entries. It is possible that this method
655-
* is called without previous invocation of {@link #beginInvalidatingKey(Object)}, then it should be a no-op.
654+
* is called without previous invocation of {@link #beginInvalidatingKey(SessionImplementor, Object)}, then it should be a no-op.
656655
*
657656
* @param key
658657
* @param lockOwner owner of the invalidation - transaction or thread
@@ -690,31 +689,6 @@ public boolean endInvalidatingKey(Object key, Object lockOwner) {
690689
}
691690

692691
public Object registerRemoteInvalidations(Object[] keys) {
693-
Transaction tx = null;
694-
try {
695-
if ( transactionManager != null ) {
696-
tx = transactionManager.getTransaction();
697-
}
698-
}
699-
catch (SystemException se) {
700-
throw new CacheException( "Could not obtain transaction", se );
701-
}
702-
if (tx != null) {
703-
if (trace) {
704-
log.tracef("Registering lock owner %s for %s: %s", tx, cache.getName(), Arrays.toString(keys));
705-
}
706-
try {
707-
Synchronization sync = new Synchronization(nonTxPutFromLoadInterceptor, keys);
708-
tx.registerSynchronization(sync);
709-
return sync.uuid;
710-
}
711-
catch (SystemException se) {
712-
throw new CacheException("Cannot register synchronization", se);
713-
}
714-
catch (RollbackException e) {
715-
return null;
716-
}
717-
}
718692
SessionImplementor session = currentSession.get();
719693
TransactionCoordinator transactionCoordinator = session == null ? null : session.getTransactionCoordinator();
720694
if (transactionCoordinator != null) {
@@ -731,20 +705,6 @@ public Object registerRemoteInvalidations(Object[] keys) {
731705

732706
// ---------------------------------------------------------------- Private
733707

734-
private Object getLocalLockOwner() {
735-
Transaction tx = null;
736-
try {
737-
if ( transactionManager != null ) {
738-
tx = transactionManager.getTransaction();
739-
}
740-
}
741-
catch (SystemException se) {
742-
throw new CacheException( "Could not obtain transaction", se );
743-
}
744-
return tx == null ? Thread.currentThread() : tx;
745-
746-
}
747-
748708
/**
749709
* Lazy-initialization map for PendingPut. Optimized for the expected usual case where only a
750710
* single put is pending for a given key.
@@ -787,7 +747,7 @@ public String toString() {
787747
sb.append("[]");
788748
}
789749
else {
790-
sb.append(invalidators);
750+
sb.append(invalidators.values());
791751
}
792752
}
793753
else {
@@ -968,7 +928,8 @@ private PendingPut(Object owner) {
968928
}
969929

970930
public String toString() {
971-
return (completed ? "C@" : "R@") + owner;
931+
// we can't use SessionImpl.toString() concurrently
932+
return (completed ? "C@" : "R@") + (owner instanceof SessionImplementor ? "Session#" + owner.hashCode() : owner.toString());
972933
}
973934

974935
public boolean invalidate(long now, long expirationPeriod) {
@@ -995,7 +956,8 @@ private Invalidator(Object owner, long registeredTimestamp) {
995956
@Override
996957
public String toString() {
997958
final StringBuilder sb = new StringBuilder("{");
998-
sb.append("Owner=").append(owner);
959+
// we can't use SessionImpl.toString() concurrently
960+
sb.append("Owner=").append(owner instanceof SessionImplementor ? "Session#" + owner.hashCode() : owner.toString());
999961
sb.append(", Timestamp=").append(registeredTimestamp);
1000962
sb.append('}');
1001963
return sb.toString();

0 commit comments

Comments
 (0)