001: /*-
002: * See the file LICENSE for redistribution information.
003: *
004: * Copyright (c) 2002,2008 Oracle. All rights reserved.
005: *
006: * $Id: XACollectionTest.java,v 1.5.2.4 2008/01/07 15:14:24 cwl Exp $
007: */
008:
009: package com.sleepycat.collections.test;
010:
011: import java.io.File;
012:
013: import javax.transaction.xa.XAResource;
014:
015: import junit.framework.Test;
016: import junit.framework.TestSuite;
017:
018: import com.sleepycat.collections.TransactionRunner;
019: import com.sleepycat.collections.TransactionWorker;
020: import com.sleepycat.je.DatabaseException;
021: import com.sleepycat.je.DeadlockException;
022: import com.sleepycat.je.Environment;
023: import com.sleepycat.je.EnvironmentConfig;
024: import com.sleepycat.je.XAEnvironment;
025: import com.sleepycat.je.log.LogUtils.XidImpl;
026: import com.sleepycat.util.ExceptionUnwrapper;
027:
028: /**
029: * Runs CollectionTest with special TestEnv and TransactionRunner objects to
030: * simulate XA transactions.
031: *
032: * <p>This test is currently JE-only and will not compile on DB core.</p>
033: */
034: public class XACollectionTest extends CollectionTest {
035:
036: public static Test suite() throws Exception {
037:
038: TestSuite suite = new TestSuite();
039:
040: EnvironmentConfig config = new EnvironmentConfig();
041: config.setTransactional(true);
042: TestEnv xaTestEnv = new XATestEnv(config);
043:
044: for (int j = 0; j < TestStore.ALL.length; j += 1) {
045: for (int k = 0; k < 2; k += 1) {
046: boolean entityBinding = (k != 0);
047:
048: suite.addTest(new XACollectionTest(xaTestEnv,
049: TestStore.ALL[j], entityBinding));
050: }
051: }
052:
053: return suite;
054: }
055:
056: public XACollectionTest(TestEnv testEnv, TestStore testStore,
057: boolean isEntityBinding) {
058:
059: super (testEnv, testStore, isEntityBinding, false /*isAutoCommit*/);
060: }
061:
062: protected TransactionRunner newTransactionRunner(Environment env)
063: throws DatabaseException {
064:
065: return new XARunner((XAEnvironment) env);
066: }
067:
068: private static class XATestEnv extends TestEnv {
069:
070: private XATestEnv(EnvironmentConfig config) {
071: super ("XA", config);
072: }
073:
074: protected Environment newEnvironment(File dir,
075: EnvironmentConfig config) throws DatabaseException {
076:
077: return new XAEnvironment(dir, config);
078: }
079: }
080:
081: private static class XARunner extends TransactionRunner {
082:
083: private XAEnvironment xaEnv;
084: private static int sequence;
085:
086: private XARunner(XAEnvironment env) {
087: super (env);
088: xaEnv = env;
089: }
090:
091: public void run(TransactionWorker worker) throws Exception {
092:
093: if (xaEnv.getThreadTransaction() == null) {
094: for (int i = 0;; i += 1) {
095: sequence += 1;
096: XidImpl xid = new XidImpl(1, String.valueOf(
097: sequence).getBytes(), null);
098: try {
099: xaEnv.start(xid, 0);
100: worker.doWork();
101: int ret = xaEnv.prepare(xid);
102: xaEnv.end(xid, 0);
103: if (ret != XAResource.XA_RDONLY) {
104: xaEnv.commit(xid, false);
105: }
106: return;
107: } catch (Exception e) {
108: e = ExceptionUnwrapper.unwrap(e);
109: try {
110: xaEnv.end(xid, 0);
111: xaEnv.rollback(xid);
112: } catch (Exception 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 e) {
126: throw ExceptionUnwrapper.unwrap(e);
127: }
128: }
129: }
130: }
131: }
|