1 7 package org.jboss.cache.transaction; 8 9 import junit.framework.TestCase; 10 import org.jboss.cache.CacheImpl; 11 import org.jboss.cache.config.Configuration; 12 import org.jboss.cache.interceptors.OrderedSynchronizationHandler; 13 import org.jboss.cache.misc.TestingUtil; 14 15 import javax.transaction.Synchronization ; 16 import javax.transaction.SystemException ; 17 import javax.transaction.Transaction ; 18 import javax.transaction.TransactionManager ; 19 20 25 public class InvocationContextCleanupTest extends TestCase 26 { 27 private CacheImpl[] caches; 28 29 private CacheImpl createCache(boolean optimistic) throws Exception 30 { 31 CacheImpl cache = new CacheImpl(); 32 cache.getConfiguration().setCacheMode(Configuration.CacheMode.REPL_SYNC); 33 if (optimistic) cache.getConfiguration().setNodeLockingScheme("OPTIMISTIC"); 34 cache.getConfiguration().setClusterName("InvocationContextCleanupTest"); 35 cache.getConfiguration().setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup"); 36 cache.getConfiguration().setLockAcquisitionTimeout(2000); 37 cache.start(); 38 return cache; 39 } 40 41 protected void tearDown() 42 { 43 if (caches != null) 44 { 45 for (int i = 0; i < caches.length; i++) 46 { 47 if (caches[i] != null) 48 { 49 caches[i].stop(); 50 caches[i] = null; 51 } 52 } 53 caches = null; 54 } 55 } 56 57 public void testInvocationContextCleanupPessimistic() throws Exception 58 { 59 test2CachesSync(false); 60 } 61 62 private void test2CachesSync(boolean optimistic) throws Exception 63 { 64 caches = new CacheImpl[2]; 65 caches[0] = createCache(optimistic); 66 caches[1] = createCache(optimistic); 67 68 TestingUtil.blockUntilViewsReceived(caches, 2000); 69 70 TransactionManager mgr = caches[0].getTransactionManager(); 71 72 mgr.begin(); 73 74 OrderedSynchronizationHandler orderedHandler = OrderedSynchronizationHandler.getInstance(mgr.getTransaction()); 75 orderedHandler.registerAtTail(new DummySynchronization(caches[0], mgr)); 76 77 caches[0].put("/test", "x", "y"); 78 try 79 { 80 mgr.commit(); 81 } 82 finally 83 { 84 } 85 86 System.out.println(caches[0].printLockInfo()); 87 System.out.println(caches[1].printLockInfo()); 88 assertEquals("y", caches[0].get("/test", "x")); 89 assertEquals("y", caches[1].get("/test", "x")); 90 } 91 92 public static class DummySynchronization implements Synchronization 93 { 94 private CacheImpl cache; 95 private TransactionManager mgr; 96 97 public DummySynchronization(CacheImpl cache, TransactionManager mgr) 98 { 99 this.cache = cache; 100 this.mgr = mgr; 101 } 102 103 public void beforeCompletion() 104 { 105 Transaction tx = null; 107 try 108 { 109 tx = mgr.suspend(); 110 } 111 catch (SystemException e) 112 { 113 throw new RuntimeException ("Unable to sustend transaction! " + e.getMessage()); 114 } 115 116 try 117 { 118 cache.put("/test", "blah", "blahblah"); 119 assertTrue("Should fail with a lock exception!", false); 120 } 121 catch (Exception e) 122 { 123 assertTrue("Should fail!", true); 124 } 125 finally 126 { 127 if (tx != null) try 128 { 129 mgr.resume(tx); 130 } 131 catch (Exception e) 132 { 133 } 134 } 135 } 136 137 public void afterCompletion(int i) 138 { 139 } 141 } 142 } 143 | Popular Tags |