001: /*-
002: * See the file LICENSE for redistribution information.
003: *
004: * Copyright (c) 2002,2008 Oracle. All rights reserved.
005: *
006: * $Id: TwoPCTest.java,v 1.5.2.4 2008/01/07 15:14:34 cwl Exp $
007: */
008:
009: package com.sleepycat.je.txn;
010:
011: import java.io.File;
012: import java.io.IOException;
013:
014: import javax.transaction.xa.XAResource;
015:
016: import junit.framework.TestCase;
017:
018: import com.sleepycat.je.Database;
019: import com.sleepycat.je.DatabaseConfig;
020: import com.sleepycat.je.DatabaseException;
021: import com.sleepycat.je.EnvironmentConfig;
022: import com.sleepycat.je.Transaction;
023: import com.sleepycat.je.XAEnvironment;
024: import com.sleepycat.je.log.FileManager;
025: import com.sleepycat.je.log.LogUtils.XidImpl;
026: import com.sleepycat.je.util.StringDbt;
027: import com.sleepycat.je.util.TestUtils;
028:
029: /*
030: * Simple 2PC transaction testing.
031: */
032: public class TwoPCTest extends TestCase {
033: private File envHome;
034: private XAEnvironment env;
035: private Database db;
036:
037: public TwoPCTest() throws DatabaseException {
038:
039: envHome = new File(System.getProperty(TestUtils.DEST_DIR));
040: }
041:
042: public void setUp() throws IOException, DatabaseException {
043:
044: TestUtils.removeFiles("Setup", envHome, FileManager.JE_SUFFIX);
045:
046: EnvironmentConfig envConfig = TestUtils.initEnvConfig();
047: envConfig.setTransactional(true);
048: envConfig.setAllowCreate(true);
049: env = new XAEnvironment(envHome, envConfig);
050:
051: DatabaseConfig dbConfig = new DatabaseConfig();
052: dbConfig.setTransactional(true);
053: dbConfig.setAllowCreate(true);
054: db = env.openDatabase(null, "foo", dbConfig);
055: }
056:
057: public void tearDown() throws IOException, DatabaseException {
058:
059: db.close();
060: env.close();
061: TestUtils.removeFiles("TearDown", envHome,
062: FileManager.JE_SUFFIX);
063: }
064:
065: /**
066: * Basic Two Phase Commit calls.
067: */
068: public void testBasic2PC() throws Throwable {
069:
070: try {
071: Transaction txn = env.beginTransaction(null, null);
072: XidImpl xid = new XidImpl(1, "TwoPCTest1".getBytes(), null);
073: env.setXATransaction(xid, txn);
074:
075: StringDbt key = new StringDbt("key");
076: StringDbt data = new StringDbt("data");
077: db.put(txn, key, data);
078:
079: env.prepare(xid);
080: env.commit(xid, false);
081: } catch (Exception E) {
082: System.out.println("caught " + E);
083: }
084: }
085:
086: /**
087: * Basic readonly-prepare.
088: */
089: public void testROPrepare() throws Throwable {
090:
091: try {
092: Transaction txn = env.beginTransaction(null, null);
093: XidImpl xid = new XidImpl(1, "TwoPCTest1".getBytes(), null);
094: env.setXATransaction(xid, txn);
095:
096: assertEquals(XAResource.XA_RDONLY, env.prepare(xid));
097: } catch (Exception E) {
098: System.out.println("caught " + E);
099: }
100: }
101:
102: /**
103: * Test calling prepare twice (should throw exception).
104: */
105: public void testTwicePreparedTransaction() throws Throwable {
106:
107: Transaction txn = env.beginTransaction(null, null);
108: XidImpl xid = new XidImpl(1, "TwoPCTest2".getBytes(), null);
109: env.setXATransaction(xid, txn);
110: StringDbt key = new StringDbt("key");
111: StringDbt data = new StringDbt("data");
112: db.put(txn, key, data);
113:
114: try {
115: env.prepare(xid);
116: env.prepare(xid);
117: fail("should not be able to prepare twice");
118: } catch (Exception E) {
119: env.commit(xid, false);
120: }
121: }
122:
123: /**
124: * Test calling rollback(xid) on an unregistered xa txn.
125: */
126: public void testRollbackNonExistent() throws Throwable {
127:
128: Transaction txn = env.beginTransaction(null, null);
129: StringDbt key = new StringDbt("key");
130: StringDbt data = new StringDbt("data");
131: db.put(txn, key, data);
132: XidImpl xid = new XidImpl(1, "TwoPCTest2".getBytes(), null);
133:
134: try {
135: env.rollback(xid);
136: fail("should not be able to call rollback on an unknown xid");
137: } catch (Exception E) {
138: }
139: txn.abort();
140: }
141:
142: /**
143: * Test calling commit(xid) on an unregistered xa txn.
144: */
145: public void testCommitNonExistent() throws Throwable {
146:
147: Transaction txn = env.beginTransaction(null, null);
148: StringDbt key = new StringDbt("key");
149: StringDbt data = new StringDbt("data");
150: db.put(txn, key, data);
151: XidImpl xid = new XidImpl(1, "TwoPCTest2".getBytes(), null);
152:
153: try {
154: env.commit(xid, false);
155: fail("should not be able to call commit on an unknown xid");
156: } catch (Exception E) {
157: }
158: txn.abort();
159: }
160: }
|