Source Code Cross Referenced for OtmExamples.java in  » Database-ORM » db-ojb » org » apache » ojb » otm » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Database ORM » db ojb » org.apache.ojb.otm 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package org.apache.ojb.otm;
002:
003:        import java.util.Collection;
004:        import java.util.Iterator;
005:
006:        import org.apache.ojb.broker.Article;
007:        import org.apache.ojb.broker.Identity;
008:        import org.apache.ojb.broker.InterfaceArticle;
009:        import org.apache.ojb.broker.PersistenceBrokerFactory;
010:        import org.apache.ojb.broker.ProductGroup;
011:        import org.apache.ojb.broker.ProductGroupWithCollectionProxy;
012:        import org.apache.ojb.broker.core.proxy.CollectionProxyDefaultImpl;
013:        import org.apache.ojb.broker.query.Criteria;
014:        import org.apache.ojb.broker.query.Query;
015:        import org.apache.ojb.broker.query.QueryFactory;
016:        import org.apache.ojb.junit.OJBTestCase;
017:        import org.apache.ojb.odmg.oql.EnhancedOQLQuery;
018:        import org.apache.ojb.otm.core.Transaction;
019:        import org.apache.ojb.otm.lock.LockType;
020:        import org.apache.ojb.otm.lock.LockingException;
021:        import org.apache.ojb.otm.lock.wait.DeadlockException;
022:        import org.apache.ojb.otm.lock.wait.NoWaitStrategy;
023:        import org.apache.ojb.otm.lock.wait.TimeoutStrategy;
024:
025:        /**
026:         * Demo Application that shows basic concepts for Applications
027:         * using the OJB OTM layer directly.
028:         */
029:        public class OtmExamples extends OJBTestCase {
030:            private static Class CLASS = OtmExamples.class;
031:            private TestKit _kit;
032:            private OTMConnection _conn;
033:
034:            public OtmExamples(String name) {
035:                super (name);
036:            }
037:
038:            public void setUp() throws Exception {
039:                super .setUp();
040:                ojbChangeReferenceSetting(ProductGroup.class,
041:                        "allArticlesInGroup", true, true, true, false);
042:                ojbChangeReferenceSetting(Article.class, "productGroup", true,
043:                        true, true, false);
044:                _kit = TestKit.getTestInstance();
045:                _conn = _kit.acquireConnection(PersistenceBrokerFactory
046:                        .getDefaultKey());
047:            }
048:
049:            public void tearDown() throws Exception {
050:                _conn.close();
051:                _conn = null;
052:                super .tearDown();
053:            }
054:
055:            public void testOtmSession() throws Throwable {
056:                Transaction tx = null;
057:                Criteria crit;
058:                Query q;
059:                EnhancedOQLQuery oql;
060:                Iterator it;
061:                Article example;
062:
063:                //perform transaction
064:                try {
065:                    tx = _kit.getTransaction(_conn);
066:                    tx.begin();
067:
068:                    example = (Article) _conn.getObjectByIdentity(new Identity(
069:                            Article.class, Article.class,
070:                            new Object[] { new Integer(77777) }));
071:                    if (example == null) {
072:                        example = Article.createInstance();
073:                        example.setArticleId(new Integer(77777));
074:                    }
075:                    example.setProductGroupId(new Integer(7));
076:                    example.setStock(333);
077:                    example.setArticleName("333");
078:                    _conn.makePersistent(example);
079:
080:                    tx.commit();
081:
082:                    Identity oid = _conn.getIdentity(example);
083:
084:                    // get from the cache
085:                    tx = _kit.getTransaction(_conn);
086:                    tx.begin();
087:                    example = (Article) _conn.getObjectByIdentity(oid);
088:                    assertEquals("should be equal", 7, example
089:                            .getProductGroupId().intValue());
090:                    assertEquals("should be equal", 333, example.getStock());
091:                    assertEquals("should be equal", "333", example
092:                            .getArticleName());
093:                    tx.commit();
094:
095:                    // get from the database
096:                    tx = _kit.getTransaction(_conn);
097:                    tx.begin();
098:                    _conn.invalidate(oid);
099:                    example = (Article) _conn.getObjectByIdentity(oid);
100:                    assertEquals("should be equal", 7, example
101:                            .getProductGroupId().intValue());
102:                    assertEquals("should be equal", "333", example
103:                            .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(77777));
113:                    crit.addEqualTo("articleName", "334");
114:                    q = QueryFactory.newQuery(Article.class, crit);
115:                    it = _conn.getIteratorByQuery(q);
116:                    if (it.hasNext()) {
117:                        InterfaceArticle article = (InterfaceArticle) it.next();
118:                        assertEquals("should be equal", 77777, article
119:                                .getArticleId().intValue());
120:                        assertEquals("should be equal", "334", article
121:                                .getArticleName());
122:                        article.setArticleName("335"); // test update
123:                        if (it.hasNext()) {
124:                            fail("Query returned more than 1 object");
125:                        }
126:                    } else {
127:                        fail("Query returned empty result set");
128:                    }
129:                    tx.commit();
130:
131:                    // get from the database via OQLQuery Iterator
132:                    tx = _kit.getTransaction(_conn);
133:                    _conn.invalidate(oid);
134:                    tx.begin();
135:                    oql = _conn.newOQLQuery();
136:                    oql.create("select a from " + Article.class.getName()
137:                            + " where articleId=$1 and articleName=$2");
138:                    oql.bind(new Integer(77777));
139:                    oql.bind("335");
140:                    it = _conn.getIteratorByOQLQuery(oql);
141:                    if (it.hasNext()) {
142:                        InterfaceArticle article = (InterfaceArticle) it.next();
143:                        assertEquals("should be equal", 77777, article
144:                                .getArticleId().intValue());
145:                        assertEquals("should be equal", "335", article
146:                                .getArticleName());
147:                        article.setArticleName("336"); // test update
148:                        if (it.hasNext()) {
149:                            fail("Query returned more than 1 object");
150:                        }
151:                    } else {
152:                        fail("Query returned empty result set");
153:                    }
154:                    tx.commit();
155:
156:                    // get from the database via OQLQuery Collection
157:                    tx = _kit.getTransaction(_conn);
158:                    _conn.invalidate(oid);
159:                    tx.begin();
160:                    oql.bind(new Integer(77777));
161:                    oql.bind("336");
162:                    it = ((Collection) oql.execute()).iterator();
163:                    if (it.hasNext()) {
164:                        InterfaceArticle article = (InterfaceArticle) it.next();
165:                        assertEquals("should be equal", 77777, article
166:                                .getArticleId().intValue());
167:                        assertEquals("should be equal", "336", article
168:                                .getArticleName());
169:                        article.setArticleName("337"); // test update
170:                        if (it.hasNext()) {
171:                            fail("Query returned more than 1 object");
172:                        }
173:                    } else {
174:                        fail("Query returned empty result set");
175:                    }
176:                    tx.commit();
177:
178:                    // get from the database
179:                    tx = _kit.getTransaction(_conn);
180:                    tx.begin();
181:                    _conn.invalidate(oid);
182:                    example = (Article) _conn.getObjectByIdentity(oid);
183:                    assertEquals("should be equal", "337", example
184:                            .getArticleName());
185:                    tx.commit();
186:
187:                    try {
188:                        tx = _kit.getTransaction(_conn);
189:                        tx.begin();
190:                        example = (Article) _conn.getObjectByIdentity(oid);
191:                        _conn.deletePersistent(example);
192:                        tx.commit();
193:                    } catch (Throwable ex) {
194:                        ex.printStackTrace();
195:                        tx.rollback();
196:                    }
197:                } catch (Throwable ex) {
198:                    try {
199:                        if (tx != null && tx.isInProgress()) {
200:                            tx.rollback();
201:                        }
202:                    } catch (Exception ex2) {
203:                    }
204:                    throw ex;
205:                }
206:            }
207:
208:            public void testCollectionProxy() throws Throwable {
209:                Transaction tx = null;
210:
211:                //perform transaction
212:                try {
213:                    tx = _kit.getTransaction(_conn);
214:                    tx.begin();
215:
216:                    ProductGroupWithCollectionProxy pg = new ProductGroupWithCollectionProxy();
217:                    pg.setId(new Integer(77777));
218:                    pg.setName("1");
219:                    _conn.makePersistent(pg);
220:
221:                    tx.commit();
222:
223:                    Identity oid = _conn.getIdentity(pg);
224:
225:                    // get from the database
226:                    tx = _kit.getTransaction(_conn);
227:                    tx.begin();
228:                    _conn.invalidate(oid);
229:                    pg = (ProductGroupWithCollectionProxy) _conn
230:                            .getObjectByIdentity(oid);
231:                    assertTrue("CollectionProxy isn't loaded",
232:                            !((CollectionProxyDefaultImpl) pg
233:                                    .getAllArticlesInGroup()).isLoaded());
234:                    Article article = Article.createInstance();
235:                    article.setArticleId(new Integer(77777));
236:                    article.setProductGroup(pg);
237:                    article.setStock(333);
238:                    article.setArticleName("333");
239:                    pg.getAllArticlesInGroup().add(article);
240:                    _conn.makePersistent(article);
241:                    tx.commit();
242:
243:                    // get from the database
244:                    tx = _kit.getTransaction(_conn);
245:                    tx.begin();
246:                    _conn.invalidate(oid);
247:                    pg = (ProductGroupWithCollectionProxy) _conn
248:                            .getObjectByIdentity(oid);
249:                    assertEquals("CollectionProxy size", 1, pg
250:                            .getAllArticlesInGroup().size());
251:                    ((InterfaceArticle) pg.getAllArticlesInGroup().get(0))
252:                            .setArticleName("444");
253:                    tx.commit();
254:
255:                    // test isolation of the cache
256:                    ((InterfaceArticle) pg.getAllArticlesInGroup().get(0))
257:                            .setArticleName("555");
258:
259:                    tx = _kit.getTransaction(_conn);
260:                    tx.begin();
261:                    pg = (ProductGroupWithCollectionProxy) _conn
262:                            .getObjectByIdentity(oid);
263:                    assertEquals("Article name", "444", ((InterfaceArticle) pg
264:                            .getAllArticlesInGroup().get(0)).getArticleName());
265:                    tx.commit();
266:
267:                    try {
268:                        tx = _kit.getTransaction(_conn);
269:                        tx.begin();
270:                        pg = (ProductGroupWithCollectionProxy) _conn
271:                                .getObjectByIdentity(oid);
272:                        _conn.deletePersistent(pg.getAllArticlesInGroup()
273:                                .get(0));
274:                        _conn.deletePersistent(pg);
275:                        tx.commit();
276:                    } catch (Throwable ex) {
277:                        ex.printStackTrace();
278:                        tx.rollback();
279:                    }
280:                } catch (Throwable ex) {
281:                    try {
282:                        if (tx != null && tx.isInProgress()) {
283:                            tx.rollback();
284:                        }
285:                    } catch (Exception ex2) {
286:                    }
287:                    throw ex;
288:                }
289:            }
290:
291:            public void testOtmCache() throws Throwable {
292:                Transaction tx = null;
293:
294:                //perform transaction
295:                try {
296:                    tx = _kit.getTransaction(_conn);
297:                    tx.begin();
298:
299:                    ProductGroup pg = new ProductGroup();
300:                    pg.setId(new Integer(77777));
301:                    pg.setName("1");
302:                    _conn.makePersistent(pg);
303:                    Article article = Article.createInstance();
304:                    article.setArticleId(new Integer(77777));
305:                    article.setStock(373);
306:                    pg.add(article);
307:                    article.setProductGroup(pg);
308:                    _conn.makePersistent(article);
309:                    tx.commit();
310:
311:                    Identity aOid = _conn.getIdentity(article);
312:                    Identity pgOid = _conn.getIdentity(pg);
313:
314:                    tx = _kit.getTransaction(_conn);
315:                    tx.begin();
316:                    pg = (ProductGroup) _conn.getObjectByIdentity(pgOid);
317:                    pg.setName("2");
318:                    _conn.makePersistent(pg);
319:                    tx.rollback();
320:
321:                    tx = _kit.getTransaction(_conn);
322:                    tx.begin();
323:                    article = (Article) _conn.getObjectByIdentity(aOid);
324:                    assertEquals("should be equal", "1", article
325:                            .getProductGroup().getName());
326:                    tx.commit();
327:
328:                    // test checkpoint
329:                    tx = _kit.getTransaction(_conn);
330:                    tx.begin();
331:                    pg = (ProductGroup) _conn.getObjectByIdentity(pgOid);
332:                    pg.setName("2");
333:                    _conn.makePersistent(pg);
334:                    tx.checkpoint();
335:                    tx.rollback();
336:
337:                    tx = _kit.getTransaction(_conn);
338:                    tx.begin();
339:                    article = (Article) _conn.getObjectByIdentity(aOid);
340:                    assertEquals("should be equal", "1", article
341:                            .getProductGroup().getName());
342:                    tx.commit();
343:
344:                    try {
345:                        tx = _kit.getTransaction(_conn);
346:                        tx.begin();
347:                        article = (Article) _conn.getObjectByIdentity(aOid);
348:                        _conn.deletePersistent(article);
349:                        _conn.deletePersistent(article.getProductGroup());
350:                        tx.commit();
351:                    } catch (Throwable ex) {
352:                        ex.printStackTrace();
353:                        tx.rollback();
354:                    }
355:                } catch (Throwable ex) {
356:                    try {
357:                        if (tx != null && tx.isInProgress()) {
358:                            tx.rollback();
359:                        }
360:                    } catch (Exception ex2) {
361:                    }
362:                    throw ex;
363:                }
364:            }
365:
366:            public void testOtmIsolation() throws Throwable {
367:                Transaction tx = null;
368:                Transaction tx2 = null;
369:                OTMConnection conn2;
370:
371:                conn2 = _kit.acquireConnection(PersistenceBrokerFactory
372:                        .getDefaultKey());
373:
374:                try {
375:                    tx = _kit.getTransaction(_conn);
376:                    tx.begin();
377:
378:                    ProductGroup pg = new ProductGroup();
379:                    pg.setId(new Integer(77777));
380:                    pg.setName("1");
381:                    _conn.makePersistent(pg);
382:                    tx.commit();
383:
384:                    Identity pgOid = _conn.getIdentity(pg);
385:
386:                    tx = _kit.getTransaction(_conn);
387:                    tx.begin();
388:                    pg = (ProductGroup) _conn.getObjectByIdentity(pgOid);
389:                    pg.setName("2");
390:
391:                    tx2 = _kit.getTransaction(conn2);
392:                    tx2.begin();
393:                    pg = (ProductGroup) conn2.getObjectByIdentity(pgOid);
394:                    assertEquals("should be equal", "1", pg.getName());
395:                    tx2.commit();
396:                    tx.commit();
397:
398:                    try {
399:                        tx = _kit.getTransaction(_conn);
400:                        tx.begin();
401:                        pg = (ProductGroup) _conn.getObjectByIdentity(pgOid);
402:                        _conn.deletePersistent(pg);
403:                        tx.commit();
404:                    } catch (Throwable ex) {
405:                        ex.printStackTrace();
406:                        tx.rollback();
407:                    }
408:                } catch (Throwable ex) {
409:                    try {
410:                        if (tx != null && tx.isInProgress()) {
411:                            tx.rollback();
412:                        }
413:                    } catch (Exception ex2) {
414:                    }
415:                    throw ex;
416:                }
417:            }
418:
419:            public void testOtmLocks() throws Throwable {
420:                Transaction tx = null;
421:                Transaction tx2 = null;
422:                OTMConnection conn2;
423:                ProductGroup pg = null;
424:                ProductGroup pg2 = null;
425:                Identity pOid = null;
426:                Identity pOid2 = null;
427:
428:                conn2 = _kit.acquireConnection(PersistenceBrokerFactory
429:                        .getDefaultKey());
430:
431:                try {
432:                    tx = _kit.getTransaction(_conn);
433:                    tx.begin();
434:                    pg = new ProductGroup();
435:                    pg.setId(new Integer(77777));
436:                    pg.setName("1");
437:                    pOid = _conn.getIdentity(pg);
438:                    if (_conn.getObjectByIdentity(pOid) == null) {
439:                        _conn.makePersistent(pg);
440:                    }
441:                    pg2 = new ProductGroup();
442:                    pg2.setId(new Integer(77778));
443:                    pg2.setName("1");
444:                    pOid2 = _conn.getIdentity(pg2);
445:                    if (_conn.getObjectByIdentity(pOid2) == null) {
446:                        _conn.makePersistent(pg2);
447:                    }
448:                    tx.commit();
449:
450:                    final Identity pgOid = _conn.getIdentity(pg);
451:                    final Identity pgOid2 = _conn.getIdentity(pg2);
452:
453:                    final Transaction tx3 = _kit.getTransaction(_conn);
454:                    tx3.begin();
455:                    _conn.getObjectByIdentity(pgOid, LockType.WRITE_LOCK);
456:                    // we can write lock twice from the same tx
457:                    _conn.getObjectByIdentity(pgOid, LockType.WRITE_LOCK);
458:
459:                    // test different LockWaitStrategies
460:                    _kit.setLockWaitStrategy(new NoWaitStrategy());
461:                    failIfLockForWrite(conn2, pgOid);
462:                    _kit.setLockWaitStrategy(new TimeoutStrategy(1));
463:                    failIfLockForWrite(conn2, pgOid);
464:
465:                    // Second test for the TimeoutStrategy:
466:                    // let the second tx to lock
467:                    _kit.setLockWaitStrategy(new TimeoutStrategy(2000));
468:                    tx2 = _kit.getTransaction(conn2);
469:                    tx2.begin();
470:                    (new Thread() {
471:                        public void run() {
472:                            try {
473:                                Thread.sleep(1000);
474:                                tx3.commit();
475:                            } catch (InterruptedException ex) {
476:                            }
477:                        }
478:                    }).start();
479:                    conn2.getObjectByIdentity(pgOid, LockType.WRITE_LOCK);
480:                    tx2.commit();
481:
482:                    // Third test for the TimeoutStrategy:
483:                    // test deadlock detection
484:                    _kit.setLockWaitStrategy(new TimeoutStrategy(4000));
485:                    tx = _kit.getTransaction(_conn);
486:                    tx.begin();
487:                    _conn.getObjectByIdentity(pgOid, LockType.WRITE_LOCK);
488:                    tx2 = _kit.getTransaction(conn2);
489:                    tx2.begin();
490:                    conn2.getObjectByIdentity(pgOid2, LockType.WRITE_LOCK);
491:                    (new Thread() {
492:                        public void run() {
493:                            try {
494:                                _conn.getObjectByIdentity(pgOid2,
495:                                        LockType.WRITE_LOCK);
496:                            } catch (LockingException ex) {
497:                                ex.printStackTrace();
498:                            }
499:                        }
500:                    }).start();
501:
502:                    try {
503:                        Thread.sleep(2000);
504:                    } catch (InterruptedException ex) {
505:                    }
506:
507:                    try {
508:                        conn2.getObjectByIdentity(pgOid, LockType.WRITE_LOCK);
509:                        fail("DeadlockException was not thrown");
510:                    } catch (DeadlockException ex) {
511:                        // ok, deadlock was detected
512:                    }
513:
514:                    tx2.rollback();
515:                    try {
516:                        Thread.sleep(2000);
517:                    } catch (InterruptedException ex) {
518:                    }
519:
520:                    tx.commit();
521:                } catch (Throwable ex) {
522:                    try {
523:                        if (tx != null && tx.isInProgress()) {
524:                            tx.rollback();
525:                        }
526:                    } catch (Exception ex2) {
527:                    }
528:                    throw ex;
529:                } finally {
530:                    try {
531:                        tx = _kit.getTransaction(_conn);
532:                        tx.begin();
533:                        if (pOid != null) {
534:                            pg = (ProductGroup) _conn.getObjectByIdentity(pOid);
535:                            if (pg != null) {
536:                                _conn.deletePersistent(pg);
537:                            }
538:                        }
539:                        if (pOid2 != null) {
540:                            pg2 = (ProductGroup) _conn
541:                                    .getObjectByIdentity(pOid2);
542:                            if (pg2 != null) {
543:                                _conn.deletePersistent(pg2);
544:                            }
545:                        }
546:                        tx.commit();
547:                    } catch (Throwable ex) {
548:                        ex.printStackTrace();
549:                        tx.rollback();
550:                    }
551:                }
552:            }
553:
554:            public void testUpdateByReachability() throws Throwable {
555:                if (ojbSkipKnownIssueProblem("Update by reachabilitiy doesn't work proper")) {
556:                    return;
557:                }
558:                Transaction tx = null;
559:                ProductGroup pg;
560:                Article article;
561:                Article article2;
562:                org.apache.ojb.broker.Person person;
563:                org.apache.ojb.broker.Project project;
564:                Identity aOid = null;
565:                Identity aOid2 = null;
566:                Identity pgOid = null;
567:                Identity prsOid = null;
568:                Identity prjOid = null;
569:
570:                //perform transaction
571:                try {
572:                    tx = _kit.getTransaction(_conn);
573:                    tx.begin();
574:
575:                    pg = new ProductGroup();
576:                    pg.setId(new Integer(77777));
577:                    pgOid = _conn.getIdentity(pg);
578:                    pg.setName("1");
579:                    _conn.makePersistent(pg);
580:                    article = Article.createInstance();
581:                    article.setArticleId(new Integer(77777));
582:                    aOid = _conn.getIdentity(article);
583:                    article.setStock(333);
584:                    pg.add(article);
585:                    article.setProductGroup(pg);
586:                    article2 = Article.createInstance();
587:                    article2.setArticleId(new Integer(77778));
588:                    aOid2 = _conn.getIdentity(article2);
589:                    article2.setStock(334);
590:                    pg.add(article2);
591:                    article2.setProductGroup(pg);
592:                    _conn.makePersistent(article);
593:                    _conn.makePersistent(article2);
594:                    person = new org.apache.ojb.broker.Person(77777, "first",
595:                            "last");
596:                    prsOid = _conn.getIdentity(person);
597:                    project = new org.apache.ojb.broker.Project(77777, "title",
598:                            "desc");
599:                    prjOid = _conn.getIdentity(project);
600:                    _conn.makePersistent(person);
601:                    _conn.makePersistent(project);
602:                    tx.commit();
603:
604:                    tx = _kit.getTransaction(_conn);
605:                    tx.begin();
606:                    pg = (ProductGroup) _conn.getObjectByIdentity(pgOid);
607:                    InterfaceArticle articleNew1 = (InterfaceArticle) pg
608:                            .getAllArticles().get(0);
609:                    InterfaceArticle articleNew2 = (InterfaceArticle) pg
610:                            .getAllArticles().get(1);
611:                    if (!_conn.getIdentity(articleNew2).equals(aOid2)) {
612:                        articleNew2 = (InterfaceArticle) pg.getAllArticles()
613:                                .get(0);
614:                        articleNew1 = (InterfaceArticle) pg.getAllArticles()
615:                                .get(1);
616:                        if (!_conn.getIdentity(article2).equals(aOid2)) {
617:                            fail("Missing the second article");
618:                        }
619:                    }
620:                    articleNew1.setStock(433);
621:                    articleNew2.setStock(434);
622:                    pg.setName("2");
623:                    tx.commit();
624:
625:                    tx = _kit.getTransaction(_conn);
626:                    tx.begin();
627:                    _conn.invalidateAll();
628:                    articleNew1 = (InterfaceArticle) _conn
629:                            .getObjectByIdentity(aOid);
630:                    articleNew2 = (InterfaceArticle) _conn
631:                            .getObjectByIdentity(aOid2);
632:                    assertEquals("should be equal", "2", article
633:                            .getProductGroup().getName());
634:                    assertEquals("should be equal", 433, article.getStock());
635:                    assertEquals("should be equal", 434, article2.getStock());
636:                    tx.commit();
637:
638:                    // Test M:N relations
639:                    tx = _kit.getTransaction(_conn);
640:                    tx.begin();
641:                    person = (org.apache.ojb.broker.Person) _conn
642:                            .getObjectByIdentity(prsOid);
643:                    project = (org.apache.ojb.broker.Project) _conn
644:                            .getObjectByIdentity(prjOid);
645:                    person.getProjects().add(project);
646:                    tx.commit();
647:
648:                    tx = _kit.getTransaction(_conn);
649:                    tx.begin();
650:                    _conn.invalidateAll();
651:                    person = (org.apache.ojb.broker.Person) _conn
652:                            .getObjectByIdentity(prsOid);
653:                    project = (org.apache.ojb.broker.Project) _conn
654:                            .getObjectByIdentity(prjOid);
655:                    assertEquals("should be equal", 1, person.getProjects()
656:                            .size());
657:                    tx.commit();
658:                } catch (Throwable ex) {
659:                    try {
660:                        if (tx != null && tx.isInProgress()) {
661:                            tx.rollback();
662:                        }
663:                    } catch (Exception ex2) {
664:                    }
665:                    throw ex;
666:                } finally {
667:                    try {
668:                        if (tx == null || !tx.isInProgress()) {
669:                            tx = _kit.getTransaction(_conn);
670:                            tx.begin();
671:                        }
672:
673:                        if (aOid != null) {
674:                            article = (Article) _conn.getObjectByIdentity(aOid);
675:                            if (article != null) {
676:                                _conn.deletePersistent(article);
677:                            }
678:                        }
679:                        if (aOid2 != null) {
680:                            article2 = (Article) _conn
681:                                    .getObjectByIdentity(aOid2);
682:                            if (article2 != null) {
683:                                _conn.deletePersistent(article2);
684:                            }
685:                        }
686:                        if (pgOid != null) {
687:                            pg = (ProductGroup) _conn
688:                                    .getObjectByIdentity(pgOid);
689:                            if (pg != null) {
690:                                _conn.deletePersistent(pg);
691:                            }
692:                        }
693:                        if (prsOid != null) {
694:                            person = (org.apache.ojb.broker.Person) _conn
695:                                    .getObjectByIdentity(prsOid);
696:                            if (person != null) {
697:                                _conn.deletePersistent(person);
698:                            }
699:                        }
700:                        if (prjOid != null) {
701:                            project = (org.apache.ojb.broker.Project) _conn
702:                                    .getObjectByIdentity(prjOid);
703:                            if (project != null) {
704:                                _conn.deletePersistent(project);
705:                            }
706:                        }
707:                        tx.commit();
708:                    } catch (Throwable ex) {
709:                        ex.printStackTrace();
710:                        tx.rollback();
711:                    }
712:                }
713:            }
714:
715:            public void testSwizzling() throws Throwable {
716:                Transaction tx = null;
717:                ProductGroup pg;
718:                Article article;
719:                Article article2;
720:
721:                try {
722:                    tx = _kit.getTransaction(_conn);
723:                    tx.begin();
724:
725:                    pg = new ProductGroup();
726:                    pg.setId(new Integer(77777));
727:                    _conn.makePersistent(pg);
728:                    article = Article.createInstance();
729:                    article.setArticleId(new Integer(77777));
730:                    article.setStock(333);
731:                    pg.add(article);
732:                    article.setProductGroup(pg);
733:                    _conn.makePersistent(article);
734:                    article2 = Article.createInstance();
735:                    article2.setArticleId(article.getArticleId());
736:                    article2.setStock(334);
737:                    article2.setProductGroup(pg);
738:                    _conn.makePersistent(article2);
739:                    article = (Article) pg.getAllArticles().get(0);
740:                    assertEquals("should be equal", 334, article.getStock());
741:                } finally {
742:                    if (tx != null) {
743:                        try {
744:                            tx.rollback();
745:                        } catch (Throwable ex) {
746:                            ex.printStackTrace();
747:                        }
748:                    }
749:                }
750:            }
751:
752:            private void failIfLockForWrite(OTMConnection conn2, Identity oid)
753:                    throws Exception {
754:                Transaction tx2 = null;
755:
756:                tx2 = _kit.getTransaction(conn2);
757:                tx2.begin();
758:                try {
759:                    conn2.getObjectByIdentity(oid, LockType.WRITE_LOCK);
760:                    fail("LockingException was not thrown");
761:                } catch (LockingException ex) {
762:                    // ok: we cannot write lock from another tx
763:                    tx2.rollback();
764:                }
765:            }
766:
767:            public static void main(String[] args) {
768:                String[] arr = { CLASS.getName() };
769:                junit.textui.TestRunner.main(arr);
770:            }
771:
772:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.