001: /*
002: * Copyright 2004-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.compass.core.test.transaction;
018:
019: import org.compass.core.CompassSession;
020: import org.compass.core.CompassTransaction;
021: import org.compass.core.config.CompassEnvironment;
022: import org.compass.core.config.CompassSettings;
023: import org.compass.core.test.AbstractTestCase;
024:
025: public class LocalTransactionNoThreadBoundTests extends
026: AbstractTestCase {
027:
028: protected String[] getMappings() {
029: return new String[] { "transaction/A.cpm.xml" };
030: }
031:
032: protected void addSettings(CompassSettings settings) {
033: settings
034: .setBooleanSetting(
035: CompassEnvironment.Transaction.DISABLE_THREAD_BOUND_LOCAL_TRANSATION,
036: true);
037: }
038:
039: public void testNestedLocalTransacitonWithCommitsAndNestedBegin()
040: throws Exception {
041: CompassSession session = openSession();
042: CompassTransaction tr = session.beginTransaction();
043:
044: Long id = new Long(1);
045: A a = new A();
046: a.setId(id);
047: session.save(a);
048:
049: CompassSession nestedSession = openSession();
050: assertTrue(session != nestedSession);
051: CompassTransaction nestedTr = nestedSession.beginTransaction();
052: a = (A) nestedSession.get(A.class, id);
053: assertNull(a);
054: nestedTr.commit();
055: nestedSession.close();
056:
057: a = (A) session.get(A.class, id);
058: assertNotNull(a);
059:
060: tr.commit();
061:
062: // verify that the instance was saved
063: tr = session.beginTransaction();
064: a = (A) session.get(A.class, id);
065: assertNotNull(a);
066:
067: tr.commit();
068: session.close();
069: }
070:
071: public void testNestedLocalTransactionsWithCommits()
072: throws Exception {
073: CompassSession session = openSession();
074: CompassTransaction tr = session.beginTransaction();
075:
076: Long id = new Long(1);
077: A a = new A();
078: a.setId(id);
079: session.save(a);
080:
081: CompassSession nestedSession = openSession();
082: CompassTransaction nestedTr = nestedSession.beginTransaction();
083: assertTrue(session != nestedSession);
084: a = (A) nestedSession.get(A.class, id);
085: assertNull(a);
086: nestedTr.commit();
087: nestedSession.close();
088:
089: a = (A) session.get(A.class, id);
090: assertNotNull(a);
091:
092: tr.commit();
093:
094: // verify that the instance was saved
095: tr = session.beginTransaction();
096: a = (A) session.get(A.class, id);
097: assertNotNull(a);
098:
099: tr.commit();
100: session.close();
101: }
102:
103: public void testNestedLocalTransactionsWithNestedRollback()
104: throws Exception {
105: CompassSession session = openSession();
106: CompassTransaction tr = session.beginTransaction();
107:
108: Long id = new Long(1);
109: A a = new A();
110: a.setId(id);
111: session.save(a);
112:
113: CompassSession nestedSession = openSession();
114: CompassTransaction nestedTr = nestedSession.beginTransaction();
115: assertTrue(session != nestedSession);
116: a = (A) nestedSession.get(A.class, id);
117: assertNull(a);
118: nestedTr.rollback();
119: nestedSession.close();
120:
121: a = (A) session.get(A.class, id);
122: tr.commit();
123: session.close();
124:
125: }
126: }
|