1 package org.apache.ojb.otm; 2 3 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 31 public class MultipleConnectionsTest extends TestCase 32 { 33 public MultipleConnectionsTest(String 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 57 public void testJustAttachConnections() throws Throwable 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 []{new Integer (77779)})); 74 if (example == null) 75 { 76 example = Article.createInstance(); 77 example.setArticleId(new Integer (77779)); 78 } 79 example.setProductGroupId(new Integer (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 ex) 94 { 95 try 96 { 97 if (tx != null && tx.isInProgress()) 98 { 99 tx.rollback(); 100 } 101 } 102 catch (Exception ex2) 103 { 104 } 105 throw ex; 106 } 107 finally 108 { 109 conn1.close(); 110 } 111 } 112 } 113 | Popular Tags |