KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sleepycat > je > txn > TwoPCTest


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

8
9 package com.sleepycat.je.txn;
10
11 import java.io.File JavaDoc;
12 import java.io.IOException JavaDoc;
13
14 import junit.framework.TestCase;
15
16 import com.sleepycat.je.Database;
17 import com.sleepycat.je.DatabaseConfig;
18 import com.sleepycat.je.DatabaseException;
19 import com.sleepycat.je.EnvironmentConfig;
20 import com.sleepycat.je.Transaction;
21 import com.sleepycat.je.XAEnvironment;
22 import com.sleepycat.je.log.FileManager;
23 import com.sleepycat.je.log.LogUtils.XidImpl;
24 import com.sleepycat.je.util.StringDbt;
25 import com.sleepycat.je.util.TestUtils;
26
27 /*
28  * Simple 2PC transaction testing.
29  */

30 public class TwoPCTest extends TestCase {
31     private File JavaDoc envHome;
32     private XAEnvironment env;
33     private Database db;
34
35     public TwoPCTest()
36         throws DatabaseException {
37
38         envHome = new File JavaDoc(System.getProperty(TestUtils.DEST_DIR));
39     }
40
41     public void setUp()
42         throws IOException JavaDoc, DatabaseException {
43
44         TestUtils.removeFiles("Setup", envHome, FileManager.JE_SUFFIX);
45
46         EnvironmentConfig envConfig = TestUtils.initEnvConfig();
47         envConfig.setTransactional(true);
48         envConfig.setAllowCreate(true);
49         env = new XAEnvironment(envHome, envConfig);
50
51         DatabaseConfig dbConfig = new DatabaseConfig();
52         dbConfig.setTransactional(true);
53         dbConfig.setAllowCreate(true);
54         db = env.openDatabase(null, "foo", dbConfig);
55     }
56
57     public void tearDown()
58         throws IOException JavaDoc, DatabaseException {
59
60         db.close();
61         env.close();
62         TestUtils.removeFiles("TearDown", envHome, FileManager.JE_SUFFIX);
63     }
64
65     /**
66      * Basic Two Phase Commit calls.
67      */

68     public void testBasic2PC()
69         throws Throwable JavaDoc {
70
71     try {
72         Transaction txn = env.beginTransaction(null, null);
73         XidImpl xid = new XidImpl(1, "TwoPCTest1".getBytes(), null);
74         env.setXATransaction(xid, txn);
75
76         StringDbt key = new StringDbt("key");
77         StringDbt data = new StringDbt("data");
78         db.put(txn, key, data);
79
80         env.prepare(xid);
81         env.commit(xid, false);
82     } catch (Exception JavaDoc E) {
83         System.out.println("caught " + E);
84     }
85     }
86
87     /**
88      * Test calling prepare twice (should throw exception).
89      */

90     public void testTwicePreparedTransaction()
91         throws Throwable JavaDoc {
92
93     Transaction txn = env.beginTransaction(null, null);
94     XidImpl xid = new XidImpl(1, "TwoPCTest2".getBytes(), null);
95     env.setXATransaction(xid, txn);
96     StringDbt key = new StringDbt("key");
97     StringDbt data = new StringDbt("data");
98     db.put(txn, key, data);
99
100     try {
101         env.prepare(xid);
102         env.prepare(xid);
103         fail("should not be able to prepare twice");
104     } catch (Exception JavaDoc E) {
105         env.commit(xid, false);
106     }
107     }
108
109     /**
110      * Test calling rollback(xid) on an unregistered xa txn.
111      */

112     public void testRollbackNonExistent()
113         throws Throwable JavaDoc {
114
115     Transaction txn = env.beginTransaction(null, null);
116     StringDbt key = new StringDbt("key");
117     StringDbt data = new StringDbt("data");
118     db.put(txn, key, data);
119     XidImpl xid = new XidImpl(1, "TwoPCTest2".getBytes(), null);
120
121     try {
122         env.rollback(xid);
123         fail("should not be able to call rollback on an unknown xid");
124     } catch (Exception JavaDoc E) {
125     }
126     txn.abort();
127     }
128
129     /**
130      * Test calling commit(xid) on an unregistered xa txn.
131      */

132     public void testCommitNonExistent()
133         throws Throwable JavaDoc {
134
135     Transaction txn = env.beginTransaction(null, null);
136     StringDbt key = new StringDbt("key");
137     StringDbt data = new StringDbt("data");
138     db.put(txn, key, data);
139     XidImpl xid = new XidImpl(1, "TwoPCTest2".getBytes(), null);
140
141     try {
142         env.commit(xid, false);
143         fail("should not be able to call commit on an unknown xid");
144     } catch (Exception JavaDoc E) {
145     }
146     txn.abort();
147     }
148 }
149
Popular Tags