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