KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.apache.ojb.otm;
2
3 import java.util.Collection JavaDoc;
4 import java.util.Iterator JavaDoc;
5
6 import org.apache.ojb.broker.Article;
7 import org.apache.ojb.broker.Identity;
8 import org.apache.ojb.broker.InterfaceArticle;
9 import org.apache.ojb.broker.PersistenceBrokerFactory;
10 import org.apache.ojb.broker.ProductGroup;
11 import org.apache.ojb.broker.ProductGroupWithCollectionProxy;
12 import org.apache.ojb.broker.core.proxy.CollectionProxyDefaultImpl;
13 import org.apache.ojb.broker.query.Criteria;
14 import org.apache.ojb.broker.query.Query;
15 import org.apache.ojb.broker.query.QueryFactory;
16 import org.apache.ojb.junit.OJBTestCase;
17 import org.apache.ojb.odmg.oql.EnhancedOQLQuery;
18 import org.apache.ojb.otm.core.Transaction;
19 import org.apache.ojb.otm.lock.LockType;
20 import org.apache.ojb.otm.lock.LockingException;
21 import org.apache.ojb.otm.lock.wait.DeadlockException;
22 import org.apache.ojb.otm.lock.wait.NoWaitStrategy;
23 import org.apache.ojb.otm.lock.wait.TimeoutStrategy;
24
25 /**
26  * Demo Application that shows basic concepts for Applications
27  * using the OJB OTM layer directly.
28  */

29 public class OtmExamples extends OJBTestCase
30 {
31     private static Class JavaDoc CLASS = OtmExamples.class;
32     private TestKit _kit;
33     private OTMConnection _conn;
34
35     public OtmExamples(String JavaDoc name)
36     {
37         super(name);
38     }
39
40     public void setUp() throws Exception JavaDoc
41     {
42         super.setUp();
43         ojbChangeReferenceSetting(ProductGroup.class, "allArticlesInGroup", true, true, true, false);
44         ojbChangeReferenceSetting(Article.class, "productGroup", true, true, true, false);
45         _kit = TestKit.getTestInstance();
46         _conn = _kit.acquireConnection(PersistenceBrokerFactory.getDefaultKey());
47     }
48
49     public void tearDown() throws Exception JavaDoc
50     {
51         _conn.close();
52         _conn = null;
53         super.tearDown();
54     }
55
56     public void testOtmSession() throws Throwable JavaDoc
57     {
58         Transaction tx = null;
59         Criteria crit;
60         Query q;
61         EnhancedOQLQuery oql;
62         Iterator JavaDoc it;
63         Article example;
64
65         //perform transaction
66
try
67         {
68             tx = _kit.getTransaction(_conn);
69             tx.begin();
70
71             example = (Article) _conn.getObjectByIdentity(
72                     new Identity(Article.class, Article.class,
73                                  new Object JavaDoc[] {new Integer JavaDoc(77777)}));
74             if (example == null)
75             {
76                 example = Article.createInstance();
77                 example.setArticleId(new Integer JavaDoc(77777));
78             }
79             example.setProductGroupId(new Integer JavaDoc(7));
80             example.setStock(333);
81             example.setArticleName("333");
82             _conn.makePersistent(example);
83
84             tx.commit();
85
86             Identity oid = _conn.getIdentity(example);
87
88             // get from the cache
89
tx = _kit.getTransaction(_conn);
90             tx.begin();
91             example = (Article) _conn.getObjectByIdentity(oid);
92             assertEquals("should be equal", 7, example.getProductGroupId().intValue());
93             assertEquals("should be equal", 333, example.getStock());
94             assertEquals("should be equal", "333", example.getArticleName());
95             tx.commit();
96
97             // get from the database
98
tx = _kit.getTransaction(_conn);
99             tx.begin();
100             _conn.invalidate(oid);
101             example = (Article) _conn.getObjectByIdentity(oid);
102             assertEquals("should be equal", 7, example.getProductGroupId().intValue());
103             assertEquals("should be equal", "333", example.getArticleName());
104             example.setArticleName("334"); // test update
105
tx.commit();
106
107             // get from the database via Query
108
tx = _kit.getTransaction(_conn);
109             _conn.invalidate(oid);
110             tx.begin();
111             crit = new Criteria();
112             crit.addEqualTo("articleId", new Integer JavaDoc(77777));
113             crit.addEqualTo("articleName", "334");
114             q = QueryFactory.newQuery(Article.class, crit);
115             it = _conn.getIteratorByQuery(q);
116             if (it.hasNext())
117             {
118                 InterfaceArticle article = (InterfaceArticle) it.next();
119                 assertEquals("should be equal", 77777, article.getArticleId().intValue());
120                 assertEquals("should be equal", "334", article.getArticleName());
121                 article.setArticleName("335"); // test update
122
if (it.hasNext())
123                 {
124                     fail("Query returned more than 1 object");
125                 }
126             }
127             else
128             {
129                 fail("Query returned empty result set");
130             }
131             tx.commit();
132
133             // get from the database via OQLQuery Iterator
134
tx = _kit.getTransaction(_conn);
135             _conn.invalidate(oid);
136             tx.begin();
137             oql = _conn.newOQLQuery();
138             oql.create("select a from " + Article.class.getName()
139                 + " where articleId=$1 and articleName=$2");
140             oql.bind(new Integer JavaDoc(77777));
141             oql.bind("335");
142             it = _conn.getIteratorByOQLQuery(oql);
143             if (it.hasNext())
144             {
145                 InterfaceArticle article = (InterfaceArticle) it.next();
146                 assertEquals("should be equal", 77777, article.getArticleId().intValue());
147                 assertEquals("should be equal", "335", article.getArticleName());
148                 article.setArticleName("336"); // test update
149
if (it.hasNext())
150                 {
151                     fail("Query returned more than 1 object");
152                 }
153             }
154             else
155             {
156                 fail("Query returned empty result set");
157             }
158             tx.commit();
159
160             // get from the database via OQLQuery Collection
161
tx = _kit.getTransaction(_conn);
162             _conn.invalidate(oid);
163             tx.begin();
164             oql.bind(new Integer JavaDoc(77777));
165             oql.bind("336");
166             it = ((Collection JavaDoc) oql.execute()).iterator();
167             if (it.hasNext())
168             {
169                 InterfaceArticle article = (InterfaceArticle) it.next();
170                 assertEquals("should be equal", 77777, article.getArticleId().intValue());
171                 assertEquals("should be equal", "336", article.getArticleName());
172                 article.setArticleName("337"); // test update
173
if (it.hasNext())
174                 {
175                     fail("Query returned more than 1 object");
176                 }
177             }
178             else
179             {
180                 fail("Query returned empty result set");
181             }
182             tx.commit();
183
184             // get from the database
185
tx = _kit.getTransaction(_conn);
186             tx.begin();
187             _conn.invalidate(oid);
188             example = (Article) _conn.getObjectByIdentity(oid);
189             assertEquals("should be equal", "337", example.getArticleName());
190             tx.commit();
191
192             try
193             {
194                 tx = _kit.getTransaction(_conn);
195                 tx.begin();
196                 example = (Article) _conn.getObjectByIdentity(oid);
197                 _conn.deletePersistent(example);
198                 tx.commit();
199             }
200             catch (Throwable JavaDoc ex)
201             {
202                 ex.printStackTrace();
203                 tx.rollback();
204             }
205         }
206         catch (Throwable JavaDoc ex)
207         {
208             try
209             {
210                 if (tx != null && tx.isInProgress())
211                 {
212                     tx.rollback();
213                 }
214             }
215             catch (Exception JavaDoc ex2)
216             {
217             }
218             throw ex;
219         }
220     }
221
222     public void testCollectionProxy() throws Throwable JavaDoc
223     {
224         Transaction tx = null;
225
226         //perform transaction
227
try
228         {
229             tx = _kit.getTransaction(_conn);
230             tx.begin();
231
232             ProductGroupWithCollectionProxy pg = new ProductGroupWithCollectionProxy();
233             pg.setId(new Integer JavaDoc(77777));
234             pg.setName("1");
235             _conn.makePersistent(pg);
236
237             tx.commit();
238
239             Identity oid = _conn.getIdentity(pg);
240
241             // get from the database
242
tx = _kit.getTransaction(_conn);
243             tx.begin();
244             _conn.invalidate(oid);
245             pg = (ProductGroupWithCollectionProxy) _conn.getObjectByIdentity(oid);
246             assertTrue("CollectionProxy isn't loaded",
247                     !((CollectionProxyDefaultImpl) pg.getAllArticlesInGroup()).isLoaded());
248             Article article = Article.createInstance();
249             article.setArticleId(new Integer JavaDoc(77777));
250             article.setProductGroup(pg);
251             article.setStock(333);
252             article.setArticleName("333");
253             pg.getAllArticlesInGroup().add(article);
254             _conn.makePersistent(article);
255             tx.commit();
256
257             // get from the database
258
tx = _kit.getTransaction(_conn);
259             tx.begin();
260             _conn.invalidate(oid);
261             pg = (ProductGroupWithCollectionProxy) _conn.getObjectByIdentity(oid);
262             assertEquals("CollectionProxy size", 1, pg.getAllArticlesInGroup().size());
263             ((InterfaceArticle) pg.getAllArticlesInGroup().get(0)).setArticleName("444");
264             tx.commit();
265
266             // test isolation of the cache
267
((InterfaceArticle) pg.getAllArticlesInGroup().get(0)).setArticleName("555");
268
269             tx = _kit.getTransaction(_conn);
270             tx.begin();
271             pg = (ProductGroupWithCollectionProxy) _conn.getObjectByIdentity(oid);
272             assertEquals("Article name", "444",
273                     ((InterfaceArticle) pg.getAllArticlesInGroup().get(0)).getArticleName());
274             tx.commit();
275
276             try
277             {
278                 tx = _kit.getTransaction(_conn);
279                 tx.begin();
280                 pg = (ProductGroupWithCollectionProxy) _conn.getObjectByIdentity(oid);
281                 _conn.deletePersistent(pg.getAllArticlesInGroup().get(0));
282                 _conn.deletePersistent(pg);
283                 tx.commit();
284             }
285             catch (Throwable JavaDoc ex)
286             {
287                 ex.printStackTrace();
288                 tx.rollback();
289             }
290         }
291         catch (Throwable JavaDoc ex)
292         {
293             try
294             {
295                 if (tx != null && tx.isInProgress())
296                 {
297                     tx.rollback();
298                 }
299             }
300             catch (Exception JavaDoc ex2)
301             {
302             }
303             throw ex;
304         }
305     }
306
307     public void testOtmCache() throws Throwable JavaDoc
308     {
309         Transaction tx = null;
310
311         //perform transaction
312
try
313         {
314             tx = _kit.getTransaction(_conn);
315             tx.begin();
316
317             ProductGroup pg = new ProductGroup();
318             pg.setId(new Integer JavaDoc(77777));
319             pg.setName("1");
320             _conn.makePersistent(pg);
321             Article article = Article.createInstance();
322             article.setArticleId(new Integer JavaDoc(77777));
323             article.setStock(373);
324             pg.add(article);
325             article.setProductGroup(pg);
326             _conn.makePersistent(article);
327             tx.commit();
328
329             Identity aOid = _conn.getIdentity(article);
330             Identity pgOid = _conn.getIdentity(pg);
331
332             tx = _kit.getTransaction(_conn);
333             tx.begin();
334             pg = (ProductGroup) _conn.getObjectByIdentity(pgOid);
335             pg.setName("2");
336             _conn.makePersistent(pg);
337             tx.rollback();
338
339             tx = _kit.getTransaction(_conn);
340             tx.begin();
341             article = (Article) _conn.getObjectByIdentity(aOid);
342             assertEquals("should be equal", "1", article.getProductGroup().getName());
343             tx.commit();
344
345             // test checkpoint
346
tx = _kit.getTransaction(_conn);
347             tx.begin();
348             pg = (ProductGroup) _conn.getObjectByIdentity(pgOid);
349             pg.setName("2");
350             _conn.makePersistent(pg);
351             tx.checkpoint();
352             tx.rollback();
353
354             tx = _kit.getTransaction(_conn);
355             tx.begin();
356             article = (Article) _conn.getObjectByIdentity(aOid);
357             assertEquals("should be equal", "1", article.getProductGroup().getName());
358             tx.commit();
359
360             try
361             {
362                 tx = _kit.getTransaction(_conn);
363                 tx.begin();
364                 article = (Article) _conn.getObjectByIdentity(aOid);
365                 _conn.deletePersistent(article);
366                 _conn.deletePersistent(article.getProductGroup());
367                 tx.commit();
368             }
369             catch (Throwable JavaDoc ex)
370             {
371                 ex.printStackTrace();
372                 tx.rollback();
373             }
374         }
375         catch (Throwable JavaDoc ex)
376         {
377             try
378             {
379                 if (tx != null && tx.isInProgress())
380                 {
381                     tx.rollback();
382                 }
383             }
384             catch (Exception JavaDoc ex2)
385             {
386             }
387             throw ex;
388         }
389     }
390
391     public void testOtmIsolation() throws Throwable JavaDoc
392     {
393         Transaction tx = null;
394         Transaction tx2 = null;
395         OTMConnection conn2;
396
397         conn2 = _kit.acquireConnection(PersistenceBrokerFactory.getDefaultKey());
398
399         try
400         {
401             tx = _kit.getTransaction(_conn);
402             tx.begin();
403
404             ProductGroup pg = new ProductGroup();
405             pg.setId(new Integer JavaDoc(77777));
406             pg.setName("1");
407             _conn.makePersistent(pg);
408             tx.commit();
409
410             Identity pgOid = _conn.getIdentity(pg);
411
412             tx = _kit.getTransaction(_conn);
413             tx.begin();
414             pg = (ProductGroup) _conn.getObjectByIdentity(pgOid);
415             pg.setName("2");
416
417             tx2 = _kit.getTransaction(conn2);
418             tx2.begin();
419             pg = (ProductGroup) conn2.getObjectByIdentity(pgOid);
420             assertEquals("should be equal", "1", pg.getName());
421             tx2.commit();
422             tx.commit();
423
424             try
425             {
426                 tx = _kit.getTransaction(_conn);
427                 tx.begin();
428                 pg = (ProductGroup) _conn.getObjectByIdentity(pgOid);
429                 _conn.deletePersistent(pg);
430                 tx.commit();
431             }
432             catch (Throwable JavaDoc ex)
433             {
434                 ex.printStackTrace();
435                 tx.rollback();
436             }
437         }
438         catch (Throwable JavaDoc ex)
439         {
440             try
441             {
442                 if (tx != null && tx.isInProgress())
443                 {
444                     tx.rollback();
445                 }
446             }
447             catch (Exception JavaDoc ex2)
448             {
449             }
450             throw ex;
451         }
452     }
453
454     public void testOtmLocks() throws Throwable JavaDoc
455     {
456         Transaction tx = null;
457         Transaction tx2 = null;
458         OTMConnection conn2;
459         ProductGroup pg = null;
460         ProductGroup pg2 = null;
461         Identity pOid = null;
462         Identity pOid2 = null;
463
464         conn2 = _kit.acquireConnection(PersistenceBrokerFactory.getDefaultKey());
465
466         try
467         {
468             tx = _kit.getTransaction(_conn);
469             tx.begin();
470             pg = new ProductGroup();
471             pg.setId(new Integer JavaDoc(77777));
472             pg.setName("1");
473             pOid = _conn.getIdentity(pg);
474             if (_conn.getObjectByIdentity(pOid) == null)
475             {
476                 _conn.makePersistent(pg);
477             }
478             pg2 = new ProductGroup();
479             pg2.setId(new Integer JavaDoc(77778));
480             pg2.setName("1");
481             pOid2 = _conn.getIdentity(pg2);
482             if (_conn.getObjectByIdentity(pOid2) == null)
483             {
484                 _conn.makePersistent(pg2);
485             }
486             tx.commit();
487
488             final Identity pgOid = _conn.getIdentity(pg);
489             final Identity pgOid2 = _conn.getIdentity(pg2);
490
491
492             final Transaction tx3 = _kit.getTransaction(_conn);
493             tx3.begin();
494             _conn.getObjectByIdentity(pgOid, LockType.WRITE_LOCK);
495             // we can write lock twice from the same tx
496
_conn.getObjectByIdentity(pgOid, LockType.WRITE_LOCK);
497
498             // test different LockWaitStrategies
499
_kit.setLockWaitStrategy(new NoWaitStrategy());
500             failIfLockForWrite(conn2, pgOid);
501             _kit.setLockWaitStrategy(new TimeoutStrategy(1));
502             failIfLockForWrite(conn2, pgOid);
503
504             // Second test for the TimeoutStrategy:
505
// let the second tx to lock
506
_kit.setLockWaitStrategy(new TimeoutStrategy(2000));
507             tx2 = _kit.getTransaction(conn2);
508             tx2.begin();
509             (new Thread JavaDoc()
510             {
511                 public void run()
512                 {
513                     try
514                     {
515                         Thread.sleep(1000);
516                         tx3.commit();
517                     }
518                     catch (InterruptedException JavaDoc ex)
519                     {
520                     }
521                 }
522             }).start();
523             conn2.getObjectByIdentity(pgOid, LockType.WRITE_LOCK);
524             tx2.commit();
525
526             // Third test for the TimeoutStrategy:
527
// test deadlock detection
528
_kit.setLockWaitStrategy(new TimeoutStrategy(4000));
529             tx = _kit.getTransaction(_conn);
530             tx.begin();
531             _conn.getObjectByIdentity(pgOid, LockType.WRITE_LOCK);
532             tx2 = _kit.getTransaction(conn2);
533             tx2.begin();
534             conn2.getObjectByIdentity(pgOid2, LockType.WRITE_LOCK);
535             (new Thread JavaDoc()
536             {
537                 public void run()
538                 {
539                     try
540                     {
541                         _conn.getObjectByIdentity(pgOid2, LockType.WRITE_LOCK);
542                     }
543                     catch (LockingException ex)
544                     {
545                         ex.printStackTrace();
546                     }
547                 }
548             }).start();
549
550             try
551             {
552                 Thread.sleep(2000);
553             }
554             catch (InterruptedException JavaDoc ex)
555             {
556             }
557
558             try
559             {
560                 conn2.getObjectByIdentity(pgOid, LockType.WRITE_LOCK);
561                 fail("DeadlockException was not thrown");
562             }
563             catch (DeadlockException ex)
564             {
565                 // ok, deadlock was detected
566
}
567
568             tx2.rollback();
569             try
570             {
571                 Thread.sleep(2000);
572             }
573             catch (InterruptedException JavaDoc ex)
574             {
575             }
576
577             tx.commit();
578         }
579         catch (Throwable JavaDoc ex)
580         {
581             try
582             {
583                 if (tx != null && tx.isInProgress())
584                 {
585                     tx.rollback();
586                 }
587             }
588             catch (Exception JavaDoc ex2)
589             {
590             }
591             throw ex;
592         }
593         finally
594         {
595             try
596             {
597                 tx = _kit.getTransaction(_conn);
598                 tx.begin();
599                 if (pOid != null)
600                 {
601                     pg = (ProductGroup) _conn.getObjectByIdentity(pOid);
602                     if (pg != null)
603                     {
604                         _conn.deletePersistent(pg);
605                     }
606                 }
607                 if (pOid2 != null)
608                 {
609                     pg2 = (ProductGroup) _conn.getObjectByIdentity(pOid2);
610                     if (pg2 != null)
611                     {
612                         _conn.deletePersistent(pg2);
613                     }
614                 }
615                 tx.commit();
616             }
617             catch (Throwable JavaDoc ex)
618             {
619                 ex.printStackTrace();
620                 tx.rollback();
621             }
622         }
623     }
624
625     public void testUpdateByReachability() throws Throwable JavaDoc
626     {
627         if(ojbSkipKnownIssueProblem("Update by reachabilitiy doesn't work proper"))
628         {
629             return;
630         }
631         Transaction tx = null;
632         ProductGroup pg;
633         Article article;
634         Article article2;
635         org.apache.ojb.broker.Person person;
636         org.apache.ojb.broker.Project project;
637         Identity aOid = null;
638         Identity aOid2 = null;
639         Identity pgOid = null;
640         Identity prsOid = null;
641         Identity prjOid = null;
642
643         //perform transaction
644
try
645         {
646             tx = _kit.getTransaction(_conn);
647             tx.begin();
648
649             pg = new ProductGroup();
650             pg.setId(new Integer JavaDoc(77777));
651             pgOid = _conn.getIdentity(pg);
652             pg.setName("1");
653             _conn.makePersistent(pg);
654             article = Article.createInstance();
655             article.setArticleId(new Integer JavaDoc(77777));
656             aOid = _conn.getIdentity(article);
657             article.setStock(333);
658             pg.add(article);
659             article.setProductGroup(pg);
660             article2 = Article.createInstance();
661             article2.setArticleId(new Integer JavaDoc(77778));
662             aOid2 = _conn.getIdentity(article2);
663             article2.setStock(334);
664             pg.add(article2);
665             article2.setProductGroup(pg);
666             _conn.makePersistent(article);
667             _conn.makePersistent(article2);
668             person = new org.apache.ojb.broker.Person(77777, "first", "last");
669             prsOid = _conn.getIdentity(person);
670             project = new org.apache.ojb.broker.Project(77777, "title", "desc");
671             prjOid = _conn.getIdentity(project);
672             _conn.makePersistent(person);
673             _conn.makePersistent(project);
674             tx.commit();
675
676
677             tx = _kit.getTransaction(_conn);
678             tx.begin();
679             pg = (ProductGroup) _conn.getObjectByIdentity(pgOid);
680             InterfaceArticle articleNew1 = (InterfaceArticle) pg.getAllArticles().get(0);
681             InterfaceArticle articleNew2 = (InterfaceArticle) pg.getAllArticles().get(1);
682             if (!_conn.getIdentity(articleNew2).equals(aOid2))
683             {
684                 articleNew2 = (InterfaceArticle) pg.getAllArticles().get(0);
685                 articleNew1 = (InterfaceArticle) pg.getAllArticles().get(1);
686                 if (!_conn.getIdentity(article2).equals(aOid2))
687                 {
688                     fail("Missing the second article");
689                 }
690             }
691             articleNew1.setStock(433);
692             articleNew2.setStock(434);
693             pg.setName("2");
694             tx.commit();
695
696             tx = _kit.getTransaction(_conn);
697             tx.begin();
698             _conn.invalidateAll();
699             articleNew1 = (InterfaceArticle) _conn.getObjectByIdentity(aOid);
700             articleNew2 = (InterfaceArticle) _conn.getObjectByIdentity(aOid2);
701             assertEquals("should be equal", "2", article.getProductGroup().getName());
702             assertEquals("should be equal", 433, article.getStock());
703             assertEquals("should be equal", 434, article2.getStock());
704             tx.commit();
705
706             // Test M:N relations
707
tx = _kit.getTransaction(_conn);
708             tx.begin();
709             person = (org.apache.ojb.broker.Person) _conn.getObjectByIdentity(prsOid);
710             project = (org.apache.ojb.broker.Project) _conn.getObjectByIdentity(prjOid);
711             person.getProjects().add(project);
712             tx.commit();
713
714             tx = _kit.getTransaction(_conn);
715             tx.begin();
716             _conn.invalidateAll();
717             person = (org.apache.ojb.broker.Person) _conn.getObjectByIdentity(prsOid);
718             project = (org.apache.ojb.broker.Project) _conn.getObjectByIdentity(prjOid);
719             assertEquals("should be equal", 1, person.getProjects().size());
720             tx.commit();
721         }
722         catch (Throwable JavaDoc ex)
723         {
724             try
725             {
726                 if (tx != null && tx.isInProgress())
727                 {
728                     tx.rollback();
729                 }
730             }
731             catch (Exception JavaDoc ex2)
732             {
733             }
734             throw ex;
735         }
736         finally
737         {
738             try
739             {
740                 if (tx == null || !tx.isInProgress())
741                 {
742                     tx = _kit.getTransaction(_conn);
743                     tx.begin();
744                 }
745
746                 if (aOid != null)
747                 {
748                     article = (Article) _conn.getObjectByIdentity(aOid);
749                     if (article != null)
750                     {
751                         _conn.deletePersistent(article);
752                     }
753                 }
754                 if (aOid2 != null)
755                 {
756                     article2 = (Article) _conn.getObjectByIdentity(aOid2);
757                     if (article2 != null)
758                     {
759                         _conn.deletePersistent(article2);
760                     }
761                 }
762                 if (pgOid != null)
763                 {
764                     pg = (ProductGroup) _conn.getObjectByIdentity(pgOid);
765                     if (pg != null)
766                     {
767                         _conn.deletePersistent(pg);
768                     }
769                 }
770                 if (prsOid != null)
771                 {
772                     person = (org.apache.ojb.broker.Person) _conn.getObjectByIdentity(prsOid);
773                     if (person != null)
774                     {
775                         _conn.deletePersistent(person);
776                     }
777                 }
778                 if (prjOid != null)
779                 {
780                     project = (org.apache.ojb.broker.Project) _conn.getObjectByIdentity(prjOid);
781                     if (project != null)
782                     {
783                         _conn.deletePersistent(project);
784                     }
785                 }
786                 tx.commit();
787             }
788             catch (Throwable JavaDoc ex)
789             {
790                 ex.printStackTrace();
791                 tx.rollback();
792             }
793         }
794     }
795
796     public void testSwizzling() throws Throwable JavaDoc
797     {
798         Transaction tx = null;
799         ProductGroup pg;
800         Article article;
801         Article article2;
802
803         try
804         {
805             tx = _kit.getTransaction(_conn);
806             tx.begin();
807
808             pg = new ProductGroup();
809             pg.setId(new Integer JavaDoc(77777));
810             _conn.makePersistent(pg);
811             article = Article.createInstance();
812             article.setArticleId(new Integer JavaDoc(77777));
813             article.setStock(333);
814             pg.add(article);
815             article.setProductGroup(pg);
816             _conn.makePersistent(article);
817             article2 = Article.createInstance();
818             article2.setArticleId(article.getArticleId());
819             article2.setStock(334);
820             article2.setProductGroup(pg);
821             _conn.makePersistent(article2);
822             article = (Article) pg.getAllArticles().get(0);
823             assertEquals("should be equal", 334, article.getStock());
824         }
825         finally
826         {
827             if (tx != null)
828             {
829                 try
830                 {
831                     tx.rollback();
832                 }
833                 catch (Throwable JavaDoc ex)
834                 {
835                     ex.printStackTrace();
836                 }
837             }
838         }
839     }
840
841     private void failIfLockForWrite(OTMConnection conn2, Identity oid)
842             throws Exception JavaDoc
843     {
844         Transaction tx2 = null;
845
846         tx2 = _kit.getTransaction(conn2);
847         tx2.begin();
848         try {
849             conn2.getObjectByIdentity(oid, LockType.WRITE_LOCK);
850             fail("LockingException was not thrown");
851         } catch (LockingException ex) {
852             // ok: we cannot write lock from another tx
853
tx2.rollback();
854         }
855     }
856
857     public static void main(String JavaDoc[] args)
858     {
859         String JavaDoc[] arr = {CLASS.getName()};
860         junit.textui.TestRunner.main(arr);
861     }
862
863 }
864
Popular Tags