001: package org.apache.ojb.otm;
002:
003: /* Copyright 2002-2005 The Apache Software Foundation
004: *
005: * Licensed under the Apache License, Version 2.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: import junit.framework.Assert;
019: import junit.framework.TestCase;
020: import org.apache.ojb.broker.Article;
021: import org.apache.ojb.broker.Identity;
022: import org.apache.ojb.broker.PersistenceBrokerFactory;
023: import org.apache.ojb.odmg.oql.EnhancedOQLQuery;
024: import org.apache.ojb.otm.core.Transaction;
025:
026: /**
027: * Ensure a Transaction can attach multiple connections
028: */
029: public class MultipleConnectionsTest extends TestCase {
030: public MultipleConnectionsTest(String name) {
031: super (name);
032: }
033:
034: private TestKit _kit;
035:
036: public void setUp() {
037: _kit = TestKit.getTestInstance();
038: }
039:
040: public void tearDown() {
041:
042: }
043:
044: /**
045: * TODO: I think this only passes because both transactions are in the same thread,
046: * otherwise it would throw an exception every time saying
047: * "Attempt to re-assign a different transaction to a open connection"
048: *
049: * @throws Throwable
050: */
051: public void testJustAttachConnections() throws Throwable {
052: Transaction tx = null;
053: Article example;
054:
055: OTMConnection conn1 = _kit
056: .acquireConnection(PersistenceBrokerFactory
057: .getDefaultKey());
058: OTMConnection conn2 = _kit
059: .acquireConnection(PersistenceBrokerFactory
060: .getDefaultKey());
061: try {
062: tx = _kit.getTransaction(conn1);
063: tx.begin();
064:
065: tx.registerConnection(conn2);
066:
067: example = (Article) conn1.getObjectByIdentity(new Identity(
068: Article.class, Article.class,
069: new Object[] { new Integer(77779) }));
070: if (example == null) {
071: example = Article.createInstance();
072: example.setArticleId(new Integer(77779));
073: }
074: example.setProductGroupId(new Integer(7));
075: example.setStock(333);
076: example.setArticleName("333");
077: conn1.makePersistent(example);
078:
079: EnhancedOQLQuery query = conn2.newOQLQuery();
080: query.create("select obj from " + Article.class.getName()
081: + " where " + "articleId = "
082: + example.getArticleId());
083: Article same = (Article) conn2.getIteratorByOQLQuery(query)
084: .next();
085: Assert.assertNotNull(
086: "Didn't find object in context of transaction",
087: same);
088:
089: tx.commit();
090:
091: } catch (Throwable ex) {
092: try {
093: if (tx != null && tx.isInProgress()) {
094: tx.rollback();
095: }
096: } catch (Exception ex2) {
097: }
098: throw ex;
099: } finally {
100: conn1.close();
101: }
102: }
103: }
|