KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > optimistic > ConcurrentTransactionTest


1 /*
2  * JBoss, Home of Professional Open Source
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.cache.optimistic;
8
9 import org.jboss.cache.CacheImpl;
10 import org.jboss.cache.Fqn;
11 import org.jboss.cache.NodeSPI;
12
13 import javax.transaction.Transaction JavaDoc;
14 import javax.transaction.TransactionManager JavaDoc;
15
16 /**
17  * @author <a HREF="mailto:manik@jboss.org">Manik Surtani (manik@jboss.org)</a>
18  */

19 public class ConcurrentTransactionTest extends AbstractOptimisticTestCase
20 {
21    private CacheImpl cache;
22
23    public ConcurrentTransactionTest(String JavaDoc name)
24    {
25       super(name);
26    }
27
28    protected void setUp()
29    {
30       try
31       {
32          cache = createCache();
33       }
34       catch (Exception JavaDoc e)
35       {
36          e.printStackTrace();
37       }
38    }
39
40    protected void tearDown()
41    {
42       if (cache != null)
43       {
44          cache.stop();
45          cache = null;
46       }
47    }
48
49    public void testConcurrentTransactions() throws Exception JavaDoc
50    {
51       TransactionManager JavaDoc tm = cache.getTransactionManager();
52       cache.put("/a/b/c/d", key, value);
53
54       assertEquals(value, cache.get("/a/b/c/d", key));
55
56       tm.begin();
57       Transaction JavaDoc tx = tm.getTransaction();
58
59       cache.put("/a/b/x/y", key, value);
60       tm.suspend();
61
62       // a number of random puts in unrelated sub nodes.
63
cache.put("/a/b/c/d", key, value + value);
64       cache.put("/a/b/c/e", key, value);
65       cache.put("/a/b/c/f", key, value);
66       cache.put("/a/b/c/g", key, value);
67
68       assertEquals(value + value, cache.get("/a/b/c/d", key));
69       assertEquals(value, cache.get("/a/b/c/e", key));
70       assertEquals(value, cache.get("/a/b/c/f", key));
71       assertEquals(value, cache.get("/a/b/c/g", key));
72
73       tm.resume(tx);
74       tx.commit();
75
76       assertEquals(value, cache.get("/a/b/x/y", key));
77
78       NodeSPI n = (NodeSPI) cache.get(Fqn.ROOT);
79       System.out.println(n.getVersion());
80    }
81 }
82
Popular Tags