1 7 package org.jboss.cache.transaction; 8 9 import junit.framework.TestCase; 10 import org.jboss.cache.CacheImpl; 11 import org.jboss.cache.interceptors.OrderedSynchronizationHandler; 12 import org.jboss.cache.misc.TestingUtil; 13 import org.jgroups.JChannel; 14 import org.jgroups.blocks.RpcDispatcher; 15 16 import javax.transaction.RollbackException ; 17 import javax.transaction.Synchronization ; 18 import javax.transaction.SystemException ; 19 import javax.transaction.Transaction ; 20 import javax.transaction.TransactionManager ; 21 22 25 public class AbortionTest extends TestCase 26 { 27 private MyTC cache1, cache2, cache3; 28 29 protected void setUp() throws Exception 30 { 31 super.setUp(); 32 System.out.println("********* START: SET UP *************"); 33 cache1 = initCache(false); 34 TestingUtil.sleepThread(1500); cache2 = initCache(false); 36 cache3 = initCache(true); 37 System.out.println("********* END: SET UP *************"); 38 } 39 40 41 protected void tearDown() throws Exception 42 { 43 System.out.println("********* START: TEAR DOWN *************"); 44 destroyCache(cache3); 45 destroyCache(cache2); 46 destroyCache(cache1); 47 cache1 = null; 48 cache2 = null; 49 cache3 = null; 50 super.tearDown(); 51 System.out.println("********* END: TEAR DOWN *************"); 52 } 53 54 private MyTC initCache(boolean notifying) throws Exception 55 { 56 MyTC c = new MyTC(); 57 c.getConfiguration().setCacheMode("REPL_SYNC"); 58 c.getConfiguration().setClusterConfig(getJGroupsStack()); 59 c.getConfiguration().setFetchInMemoryState(false); 60 if (!notifying) 61 { 62 c.getConfiguration().setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup"); 63 } 64 else 65 { 66 c.getConfiguration().setTransactionManagerLookupClass("org.jboss.cache.transaction.NotifyingTransactionManager"); 67 } 68 c.start(); 69 return c; 70 } 71 72 private String getJGroupsStack() 74 { 75 return JChannel.DEFAULT_PROTOCOL_STACK; 85 } 86 87 private void destroyCache(MyTC c) 88 { 89 if (c != null) 90 { 91 c.stop(); 92 c.destroy(); 93 } 94 } 95 96 public void testSyncCaches() throws Exception 97 { 98 performTest(false, false); 99 } 100 101 public void testSyncCachesSyncCommitRollback() throws Exception 102 { 103 performTest(true, false); 104 } 105 106 111 public void testAbortBeforeCompletion() throws Exception 112 { 113 performTest(true, true); 114 } 115 116 private void performTest(boolean syncCommitRollback, boolean abortBeforeCompletion) throws Exception 117 { 118 cache1.getConfiguration().setSyncCommitPhase(syncCommitRollback); 119 cache1.getConfiguration().setSyncRollbackPhase(syncCommitRollback); 120 cache2.getConfiguration().setSyncCommitPhase(syncCommitRollback); 121 cache2.getConfiguration().setSyncRollbackPhase(syncCommitRollback); 122 cache3.getConfiguration().setSyncCommitPhase(syncCommitRollback); 123 cache3.getConfiguration().setSyncRollbackPhase(syncCommitRollback); 124 125 TransactionManager mgr1 = cache1.getTransactionManager(); 126 TransactionManager mgr2 = cache2.getTransactionManager(); 127 NotifyingTransactionManager mgr3 = (NotifyingTransactionManager) cache3.getTransactionManager(); 128 129 assertSame(mgr1, mgr2); 130 assertNotSame(mgr1, mgr3); 131 assertNotSame(mgr2, mgr3); 132 133 assertTrue(mgr1 instanceof DummyTransactionManager); 134 assertTrue(mgr2 instanceof DummyTransactionManager); 135 assertTrue(mgr3 instanceof NotifyingTransactionManager); 136 137 138 cache1.put("/test", "key", "value"); 139 140 assertEquals("value", cache1.get("/test", "key")); 141 assertEquals("value", cache2.get("/test", "key")); 142 assertEquals("value", cache3.get("/test", "key")); 143 144 final boolean fAbortBeforeCompletion = abortBeforeCompletion; 146 mgr3.notification = new NotifyingTransactionManager.Notification() 147 { 148 public void notify(Transaction tx) throws SystemException , RollbackException 149 { 150 final Transaction finalTx = tx; 151 System.out.println("Notify called."); 152 Synchronization abort = new Synchronization () 154 { 155 156 Transaction t = finalTx; 157 158 public void beforeCompletion() 159 { 160 if (fAbortBeforeCompletion) 161 { 162 cache3.myChannel.close(); 163 System.out.println("Returning from abort.beforeCompletion"); 164 try 165 { 166 finalTx.setRollbackOnly(); 167 } 168 catch (SystemException e) 169 { 170 throw new RuntimeException ("Unable to set rollback", e); 171 } 172 throw new RuntimeException ("Dummy exception"); 173 } 174 } 175 176 public void afterCompletion(int i) 177 { 178 if (!fAbortBeforeCompletion) 179 { 180 cache3.myChannel.close(); 181 System.out.println("Returning from abort.afterCompletion"); 182 throw new RuntimeException ("Dummy exception"); 183 } 184 } 185 }; 186 187 OrderedSynchronizationHandler osh = OrderedSynchronizationHandler.getInstance(tx); 188 osh.registerAtHead(abort); 189 System.out.println("Added sync handler."); 190 } 191 }; 192 193 mgr1.begin(); 194 Transaction tx = mgr1.getTransaction(); 195 cache1.put("/test", "key", "value2"); 196 tx.commit(); 197 198 TestingUtil.sleepThread(5000); 199 200 assertEquals(0, cache1.getNumberOfLocksHeld()); 202 assertEquals(0, cache2.getNumberOfLocksHeld()); 203 assertEquals("put in transaction should NOT have been rolled back", "value2", cache1.get("/test", "key")); 204 assertEquals("put in transaction should NOT have been rolled back", "value2", cache2.get("/test", "key")); 205 206 } 207 208 209 public static class MyTC extends CacheImpl 210 { 211 JChannel myChannel; 212 RpcDispatcher myDispatcher; 213 214 public MyTC() throws Exception 215 { 216 super(); 217 } 218 219 public void start() throws Exception 220 { 221 super.start(); 222 myChannel = channel; 223 myDispatcher = disp; 224 } 225 } 226 227 } 228 | Popular Tags |