KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > otm > MultipleConnectionsTest


1 package org.apache.ojb.otm;
2
3 /* Copyright 2002-2005 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 import junit.framework.Assert;
19 import junit.framework.TestCase;
20 import org.apache.ojb.broker.Article;
21 import org.apache.ojb.broker.Identity;
22 import org.apache.ojb.broker.PersistenceBrokerFactory;
23 import org.apache.ojb.odmg.oql.EnhancedOQLQuery;
24 import org.apache.ojb.otm.core.Transaction;
25
26
27
28 /**
29  * Ensure a Transaction can attach multiple connections
30  */

31 public class MultipleConnectionsTest extends TestCase
32 {
33     public MultipleConnectionsTest(String JavaDoc name)
34     {
35         super(name);
36     }
37
38     private TestKit _kit;
39
40     public void setUp()
41     {
42         _kit = TestKit.getTestInstance();
43     }
44
45     public void tearDown()
46     {
47
48     }
49
50     /**
51      * TODO: I think this only passes because both transactions are in the same thread,
52      * otherwise it would throw an exception every time saying
53      * "Attempt to re-assign a different transaction to a open connection"
54      *
55      * @throws Throwable
56      */

57     public void testJustAttachConnections() throws Throwable JavaDoc
58     {
59         Transaction tx = null;
60         Article example;
61
62         OTMConnection conn1 = _kit.acquireConnection(PersistenceBrokerFactory.getDefaultKey());
63         OTMConnection conn2 = _kit.acquireConnection(PersistenceBrokerFactory.getDefaultKey());
64         try
65         {
66             tx = _kit.getTransaction(conn1);
67             tx.begin();
68
69             tx.registerConnection(conn2);
70
71             example = (Article) conn1.getObjectByIdentity(
72                     new Identity(Article.class, Article.class,
73                                  new Object JavaDoc[]{new Integer JavaDoc(77779)}));
74             if (example == null)
75             {
76                 example = Article.createInstance();
77                 example.setArticleId(new Integer JavaDoc(77779));
78             }
79             example.setProductGroupId(new Integer JavaDoc(7));
80             example.setStock(333);
81             example.setArticleName("333");
82             conn1.makePersistent(example);
83
84             EnhancedOQLQuery query = conn2.newOQLQuery();
85             query.create("select obj from " + Article.class.getName()
86                          + " where " + "articleId = " + example.getArticleId());
87             Article same = (Article) conn2.getIteratorByOQLQuery(query).next();
88             Assert.assertNotNull("Didn't find object in context of transaction", same);
89
90             tx.commit();
91
92         }
93         catch (Throwable JavaDoc ex)
94         {
95             try
96             {
97                 if (tx != null && tx.isInProgress())
98                 {
99                     tx.rollback();
100                 }
101             }
102             catch (Exception JavaDoc ex2)
103             {
104             }
105             throw ex;
106         }
107         finally
108         {
109             conn1.close();
110         }
111     }
112 }
113
Popular Tags