1 package org.apache.ojb.odmg; 2 3 import java.util.ArrayList ; 4 import java.util.Collection ; 5 import java.util.Iterator ; 6 import java.util.List ; 7 8 import org.apache.ojb.broker.PersistenceBroker; 9 import org.apache.ojb.broker.PersistenceBrokerFactory; 10 import org.apache.ojb.broker.query.Criteria; 11 import org.apache.ojb.broker.query.Query; 12 import org.apache.ojb.broker.query.QueryByCriteria; 13 import org.apache.ojb.broker.query.QueryByIdentity; 14 import org.apache.ojb.broker.query.QueryFactory; 15 import org.apache.ojb.junit.ODMGTestCase; 16 import org.apache.ojb.odmg.shared.ODMGGourmet; 17 import org.apache.ojb.odmg.shared.ODMGZoo; 18 import org.odmg.Database; 19 import org.odmg.Implementation; 20 import org.odmg.LockNotGrantedException; 21 import org.odmg.OQLQuery; 22 import org.odmg.Transaction; 23 24 28 public class ODMGRollbackTest extends ODMGTestCase 29 { 30 public static void main(String [] args) 31 { 32 String [] arr = {ODMGRollbackTest.class.getName()}; 33 junit.textui.TestRunner.main(arr); 34 } 35 36 public ODMGRollbackTest(String s) 37 { 38 super(s); 39 } 40 41 public void testDatabaseClose() throws Exception 42 { 43 TransactionExt tx = (TransactionExt) odmg.newTransaction(); 44 try 45 { 46 tx.begin(); 47 database.close(); 48 fail("We should not able to close database instance while running tx"); 49 } 50 catch (Exception e) 51 { 52 } 53 finally 54 { 55 tx.abort(); 56 } 57 } 58 59 public void testDeleteAll() throws Exception 60 { 61 deleteAll(odmg, ODMGZoo.class); 62 deleteAll(odmg, ODMGGourmet.class); 63 } 64 65 public void testTransactionFlush() throws Exception 66 { 67 String name = "testTransactionFlush_" + System.currentTimeMillis(); 68 TransactionExt tx = (TransactionExt) odmg.newTransaction(); 69 70 tx.begin(); 71 PersistenceBroker broker = tx.getBroker(); 72 ODMGZoo obj = new ODMGZoo(); 73 tx.lock(obj, Transaction.WRITE); 74 obj.setName(name); 75 76 tx.flush(); 77 78 Criteria crit = new Criteria(); 79 crit.addEqualTo("name", obj.getName()); 80 QueryByCriteria query = QueryFactory.newQuery(ODMGZoo.class, crit); 81 Iterator it = broker.getIteratorByQuery(query); 83 assertTrue(it.hasNext()); 84 ODMGZoo other = (ODMGZoo) it.next(); 85 assertNotNull(other); 86 assertEquals(obj.getZooId(), other.getZooId()); 87 assertEquals(obj.getName(), other.getName()); 88 assertFalse(it.hasNext()); 89 tx.abort(); 92 93 tx = (TransactionExt) odmg.newTransaction(); 94 tx.begin(); 95 broker = tx.getBroker(); 96 QueryByIdentity query2 = new QueryByIdentity(obj); 97 Object result = broker.getObjectByQuery(query2); 98 tx.commit(); 99 100 assertNull("We should not find objects from aborted tx", result); 101 } 102 103 public void testTransactionFlush_2() throws Exception 104 { 105 String name = "testTransactionFlush_2_" + System.currentTimeMillis(); 106 TransactionExt tx = (TransactionExt) odmg.newTransaction(); 107 108 tx.begin(); 109 PersistenceBroker broker = tx.getBroker(); 110 111 ODMGZoo obj = new ODMGZoo(); 112 obj.setName(name); 113 tx.lock(obj, Transaction.WRITE); 114 tx.flush(); 115 117 Criteria crit = new Criteria(); 119 crit.addEqualTo("name", obj.getName()); 120 QueryByCriteria query = QueryFactory.newQuery(ODMGZoo.class, crit); 121 Iterator it = broker.getIteratorByQuery(query); 123 assertTrue(it.hasNext()); 124 ODMGZoo other = (ODMGZoo) it.next(); 125 assertNotNull(other); 126 assertEquals(obj.getZooId(), other.getZooId()); 127 assertEquals(obj.getName(), other.getName()); 128 assertFalse(it.hasNext()); 129 130 131 tx.flush(); 133 obj.setName("updated_" + name); 135 tx.flush(); 136 OQLQuery q = odmg.newOQLQuery(); 138 q.create("select zoos from " + ODMGZoo.class.getName() + " where name like $1"); 139 q.bind("updated_" + name); 140 141 it = ((Collection ) q.execute()).iterator(); 143 assertTrue(it.hasNext()); 144 other = (ODMGZoo) it.next(); 145 assertNotNull(other); 146 assertEquals(obj.getZooId(), other.getZooId()); 147 assertEquals(obj.getName(), other.getName()); 148 assertFalse(it.hasNext()); 149 150 151 tx.abort(); 154 155 tx = (TransactionExt) odmg.newTransaction(); 156 tx.begin(); 157 broker = tx.getBroker(); 158 QueryByIdentity query2 = new QueryByIdentity(obj); 159 Object result = broker.getObjectByQuery(query2); 160 tx.commit(); 161 162 assertNull("We should not find objects from aborted tx", result); 163 } 164 165 174 public void testResultsWhileTransactionWithCheckpoint() throws Exception 175 { 176 178 int odmgZoosBefore = getDBObjectCountWithNewPB(ODMGZoo.class); 179 int projectsBefore = getDBObjectCountWithNewPB(ODMGGourmet.class); 180 181 TransactionExt tx = (TransactionExt) odmg.newTransaction(); 182 tx.begin(); 183 List zoos = new ArrayList (getNewODMGZoos("testResultsWhileTransactionWithCheckpoint", 5)); 184 List projects = new ArrayList (getNewProjects("testResultsWhileTransactionWithCheckpoint", 3)); 185 storeObjects(tx, zoos); 187 storeObjects(tx, projects); 188 tx.checkpoint(); 190 191 int odmgZoosWhile = getDBObjectCountViaOqlQuery(odmg, ODMGZoo.class); 193 int projectsWhile = getDBObjectCountViaOqlQuery(odmg, ODMGGourmet.class); 194 int odmgZoosWhilePB = 0; 195 int projectsWhilePB = 0; 196 197 odmgZoosWhilePB = getDBObjectCount(tx.getBroker(), ODMGZoo.class); 198 projectsWhilePB = getDBObjectCount(tx.getBroker(), ODMGGourmet.class); 199 200 List zoos2 = new ArrayList (getNewODMGZoos("testResultsWhileTransactionWithCheckpoint", 5)); 202 List projects2 = new ArrayList (getNewProjects("testResultsWhileTransactionWithCheckpoint", 2)); 203 storeObjects(tx, zoos2); 204 storeObjects(tx, projects2); 205 206 zoos.addAll(zoos2); 207 projects.addAll(projects2); 208 tx.checkpoint(); 210 211 TransactionImpl tx2 = (TransactionImpl) odmg.newTransaction(); 213 tx2.begin(); 214 try 215 { 216 Iterator it = zoos.iterator(); 217 while (it.hasNext()) 218 { 219 Object o = it.next(); 220 tx2.lock(o, Transaction.WRITE); 221 } 222 223 it = projects.iterator(); 224 while (it.hasNext()) 225 { 226 Object o = it.next(); 227 tx2.lock(o, Transaction.WRITE); 228 } 229 fail("After checkpoint all locks should be still exist for objects"); 230 } 231 catch(LockNotGrantedException e) 232 { 233 assertTrue(true); 235 } 236 finally 237 { 238 tx2.abort(); 239 } 240 tx.join(); 242 243 int odmgZoosWhile2 = getDBObjectCountViaOqlQuery(odmg, ODMGZoo.class); 245 int projectsWhile2 = getDBObjectCountViaOqlQuery(odmg, ODMGGourmet.class); 246 int odmgZoosWhilePB2 = 0; 247 int projectsWhilePB2 = 0; 248 249 odmgZoosWhilePB2 = getDBObjectCount(tx.getBroker(), ODMGZoo.class); 250 projectsWhilePB2 = getDBObjectCount(tx.getBroker(), ODMGGourmet.class); 251 252 tx.commit(); 253 int odmgZoosAfter = getDBObjectCountWithNewPB(ODMGZoo.class); 254 int projectsAfter = getDBObjectCountWithNewPB(ODMGGourmet.class); 255 int odmgZoosAfterOQL = getDBObjectCountViaOqlQueryUseNewTransaction(odmg, ODMGZoo.class); 256 int projectsAfterOQL = getDBObjectCountViaOqlQueryUseNewTransaction(odmg, ODMGGourmet.class); 257 258 assertEquals("Wrong number of odmgZoos found after commit", (odmgZoosBefore + 10), odmgZoosAfter); 259 assertEquals("Wrong number of projects found after commit", (projectsBefore + 5), projectsAfter); 260 assertEquals("Wrong number of odmgZoos found after commit", (odmgZoosBefore + 10), odmgZoosAfterOQL); 261 assertEquals("Wrong number of projects found after commit", (projectsBefore + 5), projectsAfterOQL); 262 263 267 268 assertEquals("Wrong number of odmgZoos found while transaction", (odmgZoosBefore + 5), odmgZoosWhilePB); 269 assertEquals("Wrong number of projects found while transaction", (projectsBefore + 3), projectsWhilePB); 270 assertEquals("Wrong number of odmgZoos found while transaction", (odmgZoosBefore + 10), odmgZoosWhilePB2); 271 assertEquals("Wrong number of projects found while transaction", (projectsBefore + 5), projectsWhilePB2); 272 assertEquals("Wrong number of odmgZoos found while transaction", (odmgZoosBefore + 5), odmgZoosWhile); 273 assertEquals("Wrong number of projects found while transaction", (projectsBefore + 3), projectsWhile); 274 assertEquals("Wrong number of odmgZoos found while transaction", (odmgZoosBefore + 10), odmgZoosWhile2); 275 assertEquals("Wrong number of projects found while transaction", (projectsBefore + 5), projectsWhile2); 276 277 tx2 = (TransactionImpl) odmg.newTransaction(); 279 tx2.begin(); 280 try 281 { 282 Iterator it = zoos.iterator(); 283 while (it.hasNext()) 284 { 285 Object o = it.next(); 286 tx2.lock(o, Transaction.WRITE); 287 } 288 289 it = projects.iterator(); 290 while (it.hasNext()) 291 { 292 Object o = it.next(); 293 tx2.lock(o, Transaction.WRITE); 294 } 295 } 296 finally 297 { 298 tx2.abort(); 299 } 300 } 301 302 309 public void testResultsAfterTransaction() throws Exception 310 { 311 313 int odmgZoosBefore = getDBObjectCountWithNewPB(ODMGZoo.class); 314 int projectsBefore = getDBObjectCountWithNewPB(ODMGGourmet.class); 315 316 Transaction tx = odmg.newTransaction(); 317 tx.begin(); 318 persistentStoreObjects(database, getNewODMGZoos("testResultsAfterTransaction", 5)); 320 persistentStoreObjects(database, getNewProjects("testResultsAfterTransaction", 3)); 321 storeObjects(tx, getNewODMGZoos("testResultsAfterTransaction", 5)); 323 storeObjects(tx, getNewProjects("testResultsAfterTransaction", 2)); 324 tx.commit(); 325 326 int odmgZoosAfter = getDBObjectCountWithNewPB(ODMGZoo.class); 327 int projectsAfter = getDBObjectCountWithNewPB(ODMGGourmet.class); 328 int odmgZoosAfterOQL = getDBObjectCountViaOqlQueryUseNewTransaction(odmg, ODMGZoo.class); 329 int projectsAfterOQL = getDBObjectCountViaOqlQueryUseNewTransaction(odmg, ODMGGourmet.class); 330 331 assertEquals("Wrong number of odmgZoos found", (odmgZoosBefore + 10), odmgZoosAfter); 332 assertEquals("Wrong number of projects found", (projectsBefore + 5), projectsAfter); 333 assertEquals("Wrong number of odmgZoos found", (odmgZoosBefore + 10), odmgZoosAfterOQL); 334 assertEquals("Wrong number of projects found", (projectsBefore + 5), projectsAfterOQL); 335 336 337 odmgZoosBefore = getDBObjectCountWithNewPB(ODMGZoo.class); 339 projectsBefore = getDBObjectCountWithNewPB(ODMGGourmet.class); 340 341 tx.begin(); 343 persistentStoreObjects(database, getNewODMGZoos("testResultsAfterTransaction", 5)); 345 persistentStoreObjects(database, getNewProjects("testResultsAfterTransaction", 3)); 346 storeObjects(tx, getNewODMGZoos("testResultsAfterTransaction", 5)); 348 storeObjects(tx, getNewProjects("testResultsAfterTransaction", 2)); 349 tx.commit(); 350 351 odmgZoosAfter = getDBObjectCountWithNewPB(ODMGZoo.class); 352 projectsAfter = getDBObjectCountWithNewPB(ODMGGourmet.class); 353 odmgZoosAfterOQL = getDBObjectCountViaOqlQueryUseNewTransaction(odmg, ODMGZoo.class); 354 projectsAfterOQL = getDBObjectCountViaOqlQueryUseNewTransaction(odmg, ODMGGourmet.class); 355 356 assertEquals("Wrong number of odmgZoos found", (odmgZoosBefore + 10), odmgZoosAfter); 357 assertEquals("Wrong number of projects found", (projectsBefore + 5), projectsAfter); 358 assertEquals("Wrong number of odmgZoos found", (odmgZoosBefore + 10), odmgZoosAfterOQL); 359 assertEquals("Wrong number of projects found", (projectsBefore + 5), projectsAfterOQL); 360 361 } 362 363 366 public void testResultsAfterTransactionWithClearedCache() throws Exception 367 { 368 int odmgZoosBefore = getDBObjectCountWithNewPB(ODMGZoo.class); 369 int projectsBefore = getDBObjectCountWithNewPB(ODMGGourmet.class); 370 371 Transaction tx = odmg.newTransaction(); 372 tx.begin(); 373 persistentStoreObjects(database, getNewODMGZoos("testResultsAfterTransactionWithClearedCache", 5)); 375 persistentStoreObjects(database, getNewProjects("testResultsAfterTransactionWithClearedCache", 3)); 376 storeObjects(tx, getNewODMGZoos("testResultsAfterTransactionWithClearedCache", 5)); 378 storeObjects(tx, getNewProjects("testResultsAfterTransactionWithClearedCache", 2)); 379 tx.commit(); 380 381 PersistenceBroker tmp = PersistenceBrokerFactory.defaultPersistenceBroker(); 383 tmp.clearCache(); 384 tmp.close(); 385 386 int odmgZoosAfter = getDBObjectCountWithNewPB(ODMGZoo.class); 387 int projectsAfter = getDBObjectCountWithNewPB(ODMGGourmet.class); 388 int odmgZoosAfterOQL = getDBObjectCountViaOqlQueryUseNewTransaction(odmg, ODMGZoo.class); 389 int projectsAfterOQL = getDBObjectCountViaOqlQueryUseNewTransaction(odmg, ODMGGourmet.class); 390 391 assertEquals("Wrong number of odmgZoos found", (odmgZoosBefore + 10), odmgZoosAfter); 392 assertEquals("Wrong number of projects found", (projectsBefore + 5), projectsAfter); 393 assertEquals("Wrong number of odmgZoos found", (odmgZoosBefore + 10), odmgZoosAfterOQL); 394 assertEquals("Wrong number of projects found", (projectsBefore + 5), projectsAfterOQL); 395 396 397 odmgZoosBefore = getDBObjectCountWithNewPB(ODMGZoo.class); 399 projectsBefore = getDBObjectCountWithNewPB(ODMGGourmet.class); 400 401 tx.begin(); 403 persistentStoreObjects(database, getNewODMGZoos("testResultsAfterTransactionWithClearedCache", 5)); 405 persistentStoreObjects(database, getNewProjects("testResultsAfterTransactionWithClearedCache", 3)); 406 storeObjects(tx, getNewODMGZoos("testResultsAfterTransactionWithClearedCache", 5)); 408 storeObjects(tx, getNewProjects("testResultsAfterTransactionWithClearedCache", 2)); 409 tx.commit(); 410 411 tmp = PersistenceBrokerFactory.defaultPersistenceBroker(); 413 tmp.clearCache(); 414 tmp.close(); 415 416 odmgZoosAfter = getDBObjectCountWithNewPB(ODMGZoo.class); 417 projectsAfter = getDBObjectCountWithNewPB(ODMGGourmet.class); 418 odmgZoosAfterOQL = getDBObjectCountViaOqlQueryUseNewTransaction(odmg, ODMGZoo.class); 419 projectsAfterOQL = getDBObjectCountViaOqlQueryUseNewTransaction(odmg, ODMGGourmet.class); 420 421 assertEquals("Wrong number of odmgZoos found", (odmgZoosBefore + 10), odmgZoosAfter); 422 assertEquals("Wrong number of projects found", (projectsBefore + 5), projectsAfter); 423 assertEquals("Wrong number of odmgZoos found", (odmgZoosBefore + 10), odmgZoosAfterOQL); 424 assertEquals("Wrong number of projects found", (projectsBefore + 5), projectsAfterOQL); 425 } 426 427 431 public void testUserRollback() throws Exception 432 { 433 int odmgZoosBefore = getDBObjectCountWithNewPB(ODMGZoo.class); 434 int projectsBefore = getDBObjectCountWithNewPB(ODMGGourmet.class); 435 436 Transaction tx = odmg.newTransaction(); 437 tx.begin(); 438 storeObjects(tx, getNewODMGZoos("testUserRollback", 10)); 439 storeObjects(tx, getNewProjects("testUserRollback", 10)); 440 tx.abort(); 442 443 int odmgZoosAfter = getDBObjectCountWithNewPB(ODMGZoo.class); 444 int projectsAfter = getDBObjectCountWithNewPB(ODMGGourmet.class); 445 int odmgZoosAfterOQL = getDBObjectCountViaOqlQueryUseNewTransaction(odmg, ODMGZoo.class); 446 int projectsAfterOQL = getDBObjectCountViaOqlQueryUseNewTransaction(odmg, ODMGGourmet.class); 447 448 assertEquals("Wrong number of odmgZoos found", odmgZoosBefore, odmgZoosAfter); 449 assertEquals("Wrong number of projects found", projectsBefore, projectsAfter); 450 assertEquals("Wrong number of odmgZoos found", (odmgZoosBefore), odmgZoosAfterOQL); 451 assertEquals("Wrong number of projects found", (projectsBefore), projectsAfterOQL); 452 453 odmgZoosBefore = getDBObjectCountWithNewPB(ODMGZoo.class); 455 projectsBefore = getDBObjectCountWithNewPB(ODMGGourmet.class); 456 457 tx.begin(); 458 storeObjects(tx, getNewODMGZoos("testUserRollback", 10)); 459 storeObjects(tx, getNewProjects("testUserRollback", 10)); 460 tx.abort(); 462 463 odmgZoosAfter = getDBObjectCountWithNewPB(ODMGZoo.class); 464 projectsAfter = getDBObjectCountWithNewPB(ODMGGourmet.class); 465 odmgZoosAfterOQL = getDBObjectCountViaOqlQueryUseNewTransaction(odmg, ODMGZoo.class); 466 projectsAfterOQL = getDBObjectCountViaOqlQueryUseNewTransaction(odmg, ODMGGourmet.class); 467 468 assertEquals("Wrong number of odmgZoos found", odmgZoosBefore, odmgZoosAfter); 469 assertEquals("Wrong number of projects found", projectsBefore, projectsAfter); 470 assertEquals("Wrong number of odmgZoos found", (odmgZoosBefore), odmgZoosAfterOQL); 471 assertEquals("Wrong number of projects found", (projectsBefore), projectsAfterOQL); 472 } 473 474 478 public void testUserRollbackWithCheckpoint() throws Exception 479 { 480 int odmgZoosBefore = getDBObjectCountWithNewPB(ODMGZoo.class); 481 int projectsBefore = getDBObjectCountWithNewPB(ODMGGourmet.class); 482 483 Transaction tx = odmg.newTransaction(); 484 tx.begin(); 485 storeObjects(tx, getNewODMGZoos("testUserRollbackWithCheckpoint", 10)); 486 tx.checkpoint(); 488 storeObjects(tx, getNewProjects("testUserRollbackWithCheckpoint", 10)); 489 tx.abort(); 491 492 int odmgZoosAfter = getDBObjectCountWithNewPB(ODMGZoo.class); 493 int projectsAfter = getDBObjectCountWithNewPB(ODMGGourmet.class); 494 int odmgZoosAfterOQL = getDBObjectCountViaOqlQueryUseNewTransaction(odmg, ODMGZoo.class); 495 int projectsAfterOQL = getDBObjectCountViaOqlQueryUseNewTransaction(odmg, ODMGGourmet.class); 496 497 assertEquals("Wrong number of odmgZoos found", odmgZoosBefore + 10, odmgZoosAfter); 498 assertEquals("Wrong number of projects found", projectsBefore, projectsAfter); 499 assertEquals("Wrong number of odmgZoos found", odmgZoosBefore + 10, odmgZoosAfterOQL); 500 assertEquals("Wrong number of projects found", projectsBefore, projectsAfterOQL); 501 502 odmgZoosBefore = getDBObjectCountWithNewPB(ODMGZoo.class); 505 projectsBefore = getDBObjectCountWithNewPB(ODMGGourmet.class); 506 tx.begin(); 508 storeObjects(tx, getNewODMGZoos("testUserRollbackWithCheckpoint", 10)); 509 tx.checkpoint(); 511 storeObjects(tx, getNewProjects("testUserRollbackWithCheckpoint", 10)); 512 tx.abort(); 514 515 odmgZoosAfter = getDBObjectCountWithNewPB(ODMGZoo.class); 516 projectsAfter = getDBObjectCountWithNewPB(ODMGGourmet.class); 517 odmgZoosAfterOQL = getDBObjectCountViaOqlQueryUseNewTransaction(odmg, ODMGZoo.class); 518 projectsAfterOQL = getDBObjectCountViaOqlQueryUseNewTransaction(odmg, ODMGGourmet.class); 519 520 assertEquals("Wrong number of odmgZoos found", odmgZoosBefore + 10, odmgZoosAfter); 521 assertEquals("Wrong number of projects found", projectsBefore, projectsAfter); 522 assertEquals("Wrong number of odmgZoos found", odmgZoosBefore + 10, odmgZoosAfterOQL); 523 assertEquals("Wrong number of projects found", projectsBefore, projectsAfterOQL); 524 } 525 526 527 528 529 private void storeObjects(Transaction tx, Collection objects) 530 { 531 for (Iterator iterator = objects.iterator(); iterator.hasNext();) 532 { 533 tx.lock(iterator.next(), Transaction.WRITE); 534 } 535 } 536 537 private void persistentStoreObjects(Database database, Collection objects) 538 { 539 for (Iterator iterator = objects.iterator(); iterator.hasNext();) 540 { 541 database.makePersistent(iterator.next()); 542 } 543 } 544 545 private static int counter; 546 547 protected Collection getNewProjects(String name, int count) 548 { 549 ArrayList list = new ArrayList (); 550 for (int i = 0; i < count; i++) 551 { 552 list.add(newProject(name)); 553 } 554 return list; 555 } 556 557 protected ODMGGourmet newProject(String name) 558 { 559 ODMGGourmet p = new ODMGGourmet(); 560 ++counter; 561 if(name == null) 562 { 563 p.setName("Test " + counter); 564 } 565 else 566 { 567 p.setName(name + "_" + counter); 568 } 569 return p; 570 } 571 572 protected Collection getNewODMGZoos(String name, int count) 573 { 574 ArrayList list = new ArrayList (); 575 for (int i = 0; i < count; i++) 576 { 577 list.add(newODMGZoo(name)); 578 } 579 return list; 580 } 581 582 protected ODMGZoo newODMGZoo(String name) 583 { 584 ODMGZoo odmgZoo = new ODMGZoo(); 585 ++counter; 586 if(name == null) 587 { 588 odmgZoo.setName("animal_" + counter); 589 } 590 else 591 { 592 odmgZoo.setName(name + "_" + counter); 593 } 594 return odmgZoo; 595 } 596 597 protected int getDBObjectCountWithNewPB(Class target) throws Exception 598 { 599 PersistenceBroker broker = PersistenceBrokerFactory.defaultPersistenceBroker(); 600 Criteria c = new Criteria(); 601 Query q = new QueryByCriteria(target, c); 602 int count = broker.getCount(q); 603 broker.close(); 604 return count; 605 } 606 607 protected int getDBObjectCount(PersistenceBroker broker, Class target) throws Exception 608 { 609 Criteria c = new Criteria(); 610 Query q = new QueryByCriteria(target, c); 611 int count = broker.getCount(q); 612 return count; 613 } 614 615 protected int getDBObjectCountViaOqlQueryUseNewTransaction(Implementation odmg, Class target) throws Exception 616 { 617 Transaction tx = odmg.newTransaction(); 618 tx.begin(); 619 OQLQuery query = odmg.newOQLQuery(); 620 query.create("select allODMGGourmets from " + target.getName()); 621 List list = (List ) query.execute(); 622 tx.commit(); 623 return list.size(); 624 } 625 626 protected int getDBObjectCountViaOqlQuery(Implementation odmg, Class target) throws Exception 627 { 628 OQLQuery query = odmg.newOQLQuery(); 629 query.create("select allObjects from " + target.getName()); 630 List list = (List ) query.execute(); 631 return list.size(); 632 } 633 634 protected void deleteAll(Implementation odmg, Class target) throws Exception 635 { 636 TransactionExt tx = (TransactionExt) odmg.newTransaction(); 637 tx.begin(); 638 tx.setCascadingDelete(target, true); 639 OQLQuery query = odmg.newOQLQuery(); 640 query.create("select allObjects from " + target.getName()); 641 List list = (List ) query.execute(); 642 for (int i = 0; i < list.size(); i++) 643 { 644 Object obj = list.get(i); 645 database.deletePersistent(obj); 646 } 647 tx.commit(); 648 } 649 650 public void testDuplicateLocking() throws Exception 652 { 653 RollbackObjectOne ro = null; 654 655 Transaction tx = odmg.newTransaction(); 656 tx.begin(); 657 ro = new RollbackObjectOne(); 658 ro.setName("test_step_1"); 659 tx.lock(ro, Transaction.WRITE); 660 tx.lock(ro, Transaction.WRITE); 661 tx.commit(); 662 663 tx.begin(); 664 tx.lock(ro, Transaction.WRITE); 665 tx.lock(ro, Transaction.WRITE); 666 ro.setName("test_step_2"); 667 tx.commit(); 668 669 tx.begin(); 670 tx.lock(ro, Transaction.WRITE); 671 ro.setName("test_step_3"); 672 tx.lock(ro, Transaction.WRITE); 673 tx.commit(); 674 } 675 676 public void testCheckCacheAfterRollback() throws Exception 677 { 678 RollbackObjectOne ro = null; 679 RollbackObjectOne ro_2 = null; 680 String name = "testCheckCacheAfterRollback_"+System.currentTimeMillis(); 681 682 Transaction tx = odmg.newTransaction(); 683 tx.begin(); 684 ro = new RollbackObjectOne(); 685 ro.setName(name); 686 tx.lock(ro, Transaction.WRITE); 687 688 ro_2 = new RollbackObjectOne(); 689 ro_2.setName(name); 690 tx.lock(ro_2, Transaction.WRITE); 691 tx.commit(); 692 693 tx = odmg.newTransaction(); 694 tx.begin(); 695 OQLQuery query = odmg.newOQLQuery(); 696 query.create("select all from " + RollbackObjectOne.class.getName() + " where name like $1"); 697 query.bind(name); 698 List list = (List ) query.execute(); 699 tx.commit(); 700 assertEquals(2,list.size()); 701 702 tx = odmg.newTransaction(); 703 tx.begin(); 704 tx.lock(ro, Transaction.WRITE); 705 ro = new RollbackObjectOne(); 706 ro.setDescription(name); 707 708 tx.lock(ro_2, Transaction.WRITE); 709 ro_2 = new RollbackObjectOne(); 710 ro_2.setDescription(name); 711 712 tx.abort(); 713 714 tx = odmg.newTransaction(); 715 tx.begin(); 716 query = odmg.newOQLQuery(); 717 query.create("select all from " + RollbackObjectOne.class.getName() + " where name like $1"); 718 query.bind(name); 719 list = (List ) query.execute(); 720 tx.commit(); 721 assertEquals(2,list.size()); 722 assertNull(((RollbackObjectOne)list.get(0)).getDescription()); 723 assertNull(((RollbackObjectOne)list.get(1)).getDescription()); 724 725 TransactionImpl tx2 = (TransactionImpl) odmg.newTransaction(); 727 tx2.begin(); 728 try 729 { 730 Iterator it = list.iterator(); 731 while (it.hasNext()) 732 { 733 Object o = it.next(); 734 tx2.lock(o, Transaction.WRITE); 735 } 736 } 737 finally 738 { 739 tx2.abort(); 740 } 741 } 742 743 746 public void testEmpty() throws Exception 747 { 748 Transaction tx = odmg.newTransaction(); 750 tx.begin(); 751 tx.abort(); 752 753 tx = odmg.newTransaction(); 754 tx.begin(); 755 tx.checkpoint(); 756 tx.checkpoint(); 757 tx.abort(); 758 759 tx = odmg.newTransaction(); 760 tx.begin(); 761 tx.checkpoint(); 762 tx.checkpoint(); 763 tx.commit(); 764 765 tx = odmg.newTransaction(); 766 tx.begin(); 767 tx.commit(); 768 769 tx = odmg.newTransaction(); 771 tx.begin(); 772 tx.abort(); 773 774 tx.begin(); 775 tx.checkpoint(); 776 tx.checkpoint(); 777 tx.abort(); 778 779 tx.begin(); 780 tx.checkpoint(); 781 tx.checkpoint(); 782 tx.commit(); 783 784 tx.begin(); 785 tx.commit(); 786 } 787 788 public void testDoubleAbortTxCall() throws Exception 789 { 790 try 791 { 792 Transaction tx = odmg.newTransaction(); 793 tx.begin(); 794 tx.abort(); 795 tx.abort(); 796 } 797 catch(Exception e) 798 { 799 e.printStackTrace(); 800 fail("We allow to do multiple tx.abort calls, but this test fails with: " + e.getMessage()); 801 } 802 } 803 804 public void testInternalCausedRollback() throws Exception 805 { 806 Transaction tx = odmg.newTransaction(); 807 String name = "testCheckCacheAfterRollback_"+System.currentTimeMillis(); 808 try 809 { 810 tx.begin(); 811 812 RollbackObjectOne ro = new RollbackObjectOne(); 813 ro.setName(name); 814 tx.lock(ro, Transaction.WRITE); 815 tx.lock(new Exception (), Transaction.WRITE); 817 818 tx.commit(); 819 fail("A exception was expected"); 820 } 821 catch(Exception e) 822 { 823 if(tx != null && tx.isOpen()) tx.abort(); 824 } 825 } 826 827 828 public static class RollbackObjectOne 832 { 833 Integer objId; 834 String name; 835 String description; 836 837 public Integer getObjId() 838 { 839 return objId; 840 } 841 842 public void setObjId(Integer objId) 843 { 844 this.objId = objId; 845 } 846 847 public String getName() 848 { 849 return name; 850 } 851 852 public void setName(String name) 853 { 854 this.name = name; 855 } 856 857 public String getDescription() 858 { 859 return description; 860 } 861 862 public void setDescription(String description) 863 { 864 this.description = description; 865 } 866 } 867 868 public static class RollbackObjectTwo 869 { 870 Integer objId; 871 String name; 872 String description; 873 874 public Integer getObjId() 875 { 876 return objId; 877 } 878 879 public void setObjId(Integer objId) 880 { 881 this.objId = objId; 882 } 883 884 public String getName() 885 { 886 return name; 887 } 888 889 public void setName(String name) 890 { 891 this.name = name; 892 } 893 894 public String getDescription() 895 { 896 return description; 897 } 898 899 public void setDescription(String description) 900 { 901 this.description = description; 902 } 903 } 904 } 905 | Popular Tags |