001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019: package org.apache.openjpa.persistence.kernel;
020:
021: import java.util.ArrayList;
022: import javax.persistence.EntityManager;
023: import javax.persistence.EntityTransaction;
024:
025: import org.apache.openjpa.persistence.common.utils.AbstractTestCase;
026:
027: public class TestEJBTransactions extends AbstractTestCase {
028:
029: public TestEJBTransactions(String name) {
030: super (name, "kernelcactusapp");
031: }
032:
033: public void setUp() {
034: }
035:
036: public void testTxCannotBeCommittedTwiceInDiffThreads()
037: throws Throwable {
038: final ArrayList list = new ArrayList();
039:
040: final EntityManager em = currentEntityManager();
041: final EntityTransaction tx;
042:
043: tx = em.getTransaction();
044: if (tx.isActive())
045: tx.rollback();
046: tx.begin();
047: endTx(em);
048:
049: Thread thread = new Thread() {
050: @SuppressWarnings("unchecked")
051: public void run() {
052: try {
053: endTx(em);
054: list.add(new Integer(0));
055: } catch (Exception e) {
056: list.add(e);
057: } catch (Throwable t) {
058: list.add(t);
059: }
060: }
061: };
062:
063: thread.start();
064:
065: while (list.size() == 0)
066: ;
067:
068: Object result = list.get(0);
069:
070: if (!(result instanceof Exception)) {
071: if (result instanceof Throwable)
072: throw (Throwable) result;
073: }
074: thread.join();
075: }
076:
077: public void testTransactionsCannotBeCommittedTwice() {
078: EntityManager pm = currentEntityManager();
079: rollbackTx(pm);
080:
081: startTx(pm);
082: endTx(pm);
083:
084: try {
085: // second commit
086: endTx(pm);
087: } catch (Exception e) {
088: // good: we should be throwing an exception here
089: System.out.println("Exception should be thrown here..."
090: + "Transactions cannot be committed twice...");
091: }
092: }
093:
094: public void testTransactionsCannotBeRolledBackTwice() {
095: EntityManager pm = currentEntityManager();
096: startTx(pm);
097:
098: EntityTransaction t;
099: t = pm.getTransaction();
100:
101: if (t.isActive()) {
102: t.rollback();
103: } else {
104: t.begin();
105: t.rollback();
106: }
107:
108: try {
109: // second rollback
110: t.rollback();
111: } catch (Exception e) {
112: // good: we should be throwing an exception here
113: System.out.println("Exception should be thrown here..."
114: + "Transactions cannot be rolled back twice...");
115: }
116: }
117: }
|