KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sleepycat > collections > test > XACollectionTest


1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2002,2006 Oracle. All rights reserved.
5  *
6  * $Id: XACollectionTest.java,v 1.5 2006/10/30 21:14:39 bostic Exp $
7  */

8
9 package com.sleepycat.collections.test;
10
11 import java.io.File JavaDoc;
12
13 import junit.framework.Test;
14 import junit.framework.TestSuite;
15
16 import com.sleepycat.collections.TransactionRunner;
17 import com.sleepycat.collections.TransactionWorker;
18 import com.sleepycat.je.DatabaseException;
19 import com.sleepycat.je.DeadlockException;
20 import com.sleepycat.je.Environment;
21 import com.sleepycat.je.EnvironmentConfig;
22 import com.sleepycat.je.XAEnvironment;
23 import com.sleepycat.je.log.LogUtils.XidImpl;
24 import com.sleepycat.util.ExceptionUnwrapper;
25
26 /**
27  * Runs CollectionTest with special TestEnv and TransactionRunner objects to
28  * simulate XA transactions.
29  *
30  * <p>This test is currently JE-only and will not compile on DB core.</p>
31  */

32 public class XACollectionTest extends CollectionTest {
33
34     public static Test suite()
35         throws Exception JavaDoc {
36
37         TestSuite suite = new TestSuite();
38
39         EnvironmentConfig config = new EnvironmentConfig();
40         config.setTransactional(true);
41         TestEnv xaTestEnv = new XATestEnv(config);
42
43         for (int j = 0; j < TestStore.ALL.length; j += 1) {
44             for (int k = 0; k < 2; k += 1) {
45                 boolean entityBinding = (k != 0);
46
47                 suite.addTest(new XACollectionTest
48                     (xaTestEnv, TestStore.ALL[j], entityBinding));
49             }
50         }
51
52         return suite;
53     }
54
55     public XACollectionTest(TestEnv testEnv,
56                             TestStore testStore,
57                             boolean isEntityBinding) {
58
59         super(testEnv, testStore, isEntityBinding, false /*isAutoCommit*/);
60     }
61
62     protected TransactionRunner newTransactionRunner(Environment env)
63         throws DatabaseException {
64
65         return new XARunner((XAEnvironment) env);
66     }
67
68     private static class XATestEnv extends TestEnv {
69
70         private XATestEnv(EnvironmentConfig config) {
71             super("XA", config);
72         }
73
74         protected Environment newEnvironment(File JavaDoc dir,
75                                              EnvironmentConfig config)
76             throws DatabaseException {
77
78             return new XAEnvironment(dir, config);
79         }
80     }
81
82     private static class XARunner extends TransactionRunner {
83
84         private XAEnvironment xaEnv;
85         private static int sequence;
86
87         private XARunner(XAEnvironment env) {
88             super(env);
89             xaEnv = env;
90         }
91
92         public void run(TransactionWorker worker)
93             throws Exception JavaDoc {
94
95             if (xaEnv.getThreadTransaction() == null) {
96                 for (int i = 0;; i += 1) {
97                     sequence += 1;
98                     XidImpl xid = new XidImpl
99                         (1, String.valueOf(sequence).getBytes(), null);
100                     try {
101                         xaEnv.start(xid, 0);
102                         worker.doWork();
103                         xaEnv.prepare(xid);
104                         xaEnv.end(xid, 0);
105                         xaEnv.commit(xid, false);
106                         return;
107                     } catch (Exception JavaDoc e) {
108                         e = ExceptionUnwrapper.unwrap(e);
109                         try {
110                             xaEnv.end(xid, 0);
111                             xaEnv.rollback(xid);
112                         } catch (Exception JavaDoc e2) {
113                             e2.printStackTrace();
114                             throw e;
115                         }
116                         if (i >= getMaxRetries() ||
117                             !(e instanceof DeadlockException)) {
118                             throw e;
119                         }
120                     }
121                 }
122             } else { /* Nested */
123                 try {
124                     worker.doWork();
125                 } catch (Exception JavaDoc e) {
126                     throw ExceptionUnwrapper.unwrap(e);
127                 }
128             }
129         }
130     }
131 }
132
Popular Tags