示例#1
0
  public void testDepends() throws Exception {
    Database db = null;
    MasterObjectNoKeyGen master = new MasterObjectNoKeyGen();
    master.setId(100);
    master.setDescrip("This is the descrip.");
    db = _category.getDatabase();
    db.begin();
    db.create(master);
    db.commit();

    assertTrue(master.getId() != 0);
    assertEquals(100, master.getId());

    // THIS Part Works!
    db.begin();
    DependentObjectNoKeyGen depends = new DependentObjectNoKeyGen();
    depends.setId(101);
    depends.setDescrip("Description");
    master.setDepends(depends);
    db.update(master);
    db.commit();

    assertTrue(master.getId() != 0);
    assertEquals(100, master.getId());
    int masterId = master.getId();

    db.begin();
    master = (MasterObjectNoKeyGen) db.load(MasterObjectNoKeyGen.class, new Integer(masterId));
    assertNotNull(master.getDepends());
    master.setDepends(null);
    db.commit();

    db.begin();
    master = (MasterObjectNoKeyGen) db.load(MasterObjectNoKeyGen.class, new Integer(masterId));
    assertNull(master.getDepends());
    db.commit();

    // THIS part doesn't!
    db.begin();
    master = (MasterObjectNoKeyGen) db.load(MasterObjectNoKeyGen.class, new Integer(masterId));
    depends = new DependentObjectNoKeyGen();
    depends.setId(102);
    depends.setDescrip("Description");
    master.setDepends(depends);
    db.commit();

    db.begin();
    master = (MasterObjectNoKeyGen) db.load(MasterObjectNoKeyGen.class, new Integer(masterId));
    assertNotNull(master.getDepends());
    db.commit();

    db.begin();
    master = (MasterObjectNoKeyGen) db.load(MasterObjectNoKeyGen.class, new Integer(masterId));
    assertNotNull(master);
    db.remove(master);
    db.commit();
  }
示例#2
0
  public void testCreateLoadUpdateDelete() throws Exception {
    Database database;

    // create product
    database = _category.getDatabase();
    database.begin();

    Product pc = new Product(1, "LCD", KindEnum.MONITOR);
    database.create(pc);

    database.commit();
    database.close();

    // load created product
    database = _category.getDatabase();
    database.begin();

    Product pl1 = new Product(1, "LCD", KindEnum.MONITOR);
    Product pl2 = (Product) database.load(Product.class, new Integer(1));
    assertEquals(pl1, pl2);

    database.commit();
    database.close();

    // update product
    database = _category.getDatabase();
    database.begin();

    Product pu = (Product) database.load(Product.class, new Integer(1));
    pu.setName("Laser");
    pu.setKind(KindEnum.PRINTER);

    database.commit();
    database.close();

    // load updated product
    database = _category.getDatabase();
    database.begin();

    Product pl3 = new Product(1, "Laser", KindEnum.PRINTER);
    Product pl4 = (Product) database.load(Product.class, new Integer(1));
    assertEquals(pl3, pl4);

    database.commit();
    database.close();

    // delete product
    database = _category.getDatabase();
    database.begin();

    Product pd = (Product) database.load(Product.class, new Integer(1));
    database.remove(pd);

    database.commit();
    database.close();
  }
示例#3
0
  public void testCreateAndLoadLaptop() throws Exception {
    Database database = _category.getDatabase();

    database.begin();
    ProductDetail detail = new ProductDetail();
    detail.setId(10);
    detail.setCategory("category 10");
    detail.setLocation("location 10");
    database.create(detail);
    database.commit();

    database.begin();
    Laptop laptop = new Laptop();
    laptop.setId(10);
    laptop.setName("laptop 10");
    laptop.setCpu("centrino");
    laptop.setResolution("1600");
    laptop.setWeight(2750);
    laptop.setDetail((ProductDetail) database.load(ProductDetail.class, new Integer(10)));
    database.create(laptop);

    Owner owner = new Owner();
    owner.setId(new Integer(10));
    owner.setName("owner 10");
    owner.setProduct(laptop);
    database.commit();

    database.begin();
    laptop = (Laptop) database.load(Laptop.class, new Integer(10));
    database.commit();

    assertNotNull(laptop);
    assertEquals("ctf.jdo.tc9x.Laptop", laptop.getClass().getName());
    assertEquals(10, laptop.getId());
    assertEquals("laptop 10", laptop.getName());

    database.begin();
    database.remove(database.load(Laptop.class, new Integer(10)));
    database.remove(database.load(ProductDetail.class, new Integer(10)));
    database.commit();

    database.begin();
    try {
      laptop = (Laptop) database.load(Laptop.class, new Integer(10));
      fail("Laptop with id 10 still exists.");
    } catch (ObjectNotFoundException e) {
      assertEquals(
          "The object of type ctf.jdo.tc9x.Laptop with identity <10(10)> "
              + "was not found in persistent storage",
          e.getMessage());
    }
    database.commit();

    database.close();
  }
示例#4
0
  public void testMasterUpdate() throws PersistenceException {
    Complex fullname = new Complex("First", "Person");

    // 1. load master object, add a new dependent object and commit
    _db.begin();
    TestLazyPerson loadPerson = (TestLazyEmployee) _db.load(TestLazyEmployee.class, fullname);
    TestLazyAddress address = new TestLazyAddress();
    address.setId(201);
    address.setStreet("Halfpipe 1");
    address.setCity("Skater Heaven");
    address.setPerson(loadPerson);
    loadPerson.getAddress().add(address);
    _db.commit();

    // 2. reuse master and commit. if we get a DuplicateIdentityException,
    //    the dependent object (id=201) was created twice.
    _db.begin();
    _db.update(loadPerson);
    try {
      _db.commit();
    } catch (TransactionAbortedException e) {
      if (e.getException() instanceof DuplicateIdentityException) {
        stream.println("Error: The dependent object Address was just duplicated");
        fail("The dependent object Address was just duplicated");
      } else {
        throw e;
      }
    }
    // No DuplicateIdentityException means, that just one instance of Address (id=201)
    // was created in the database. That's what we want to have.
  }
  /**
   * Removes a new process instance
   *
   * @param instanceId instance id
   */
  public void removeProcessInstance(String instanceId) throws WorkflowException {
    SilverTrace.info(
        "worflowEngine",
        "ProcessInstanceManagerImpl.removeProcessInstance()",
        "root.MSG_GEN_ENTER_METHOD",
        "InstanceId=" + instanceId);
    ProcessInstanceImpl instance;
    Database db = null;
    try {
      // Delete forms data associated with this instance
      removeProcessInstanceData(instanceId);

      // Constructs the query
      db = WorkflowJDOManager.getDatabase();
      db.begin();
      instance = (ProcessInstanceImpl) db.load(ProcessInstanceImpl.class, instanceId);
      db.remove(instance);
      db.commit();
    } catch (PersistenceException pe) {
      throw new WorkflowException(
          "ProcessInstanceManagerImpl.removeProcessInstance", "EX_ERR_CASTOR_REMOVE_INSTANCE", pe);
    } finally {
      WorkflowJDOManager.closeDatabase(db);
    }

    WorkflowHub.getErrorManager().removeErrorsOfInstance(instanceId);
    SilverTrace.info(
        "worflowEngine",
        "ProcessInstanceManagerImpl.removeProcessInstance()",
        "root.MSG_GEN_EXIT_METHOD");
  }
  /**
   * unlocks this instance for the given instance and state
   *
   * @param state state that have to be locked
   * @param user the locking user
   */
  public void unlock(ProcessInstance instance, State state, User user) throws WorkflowException {
    Database db = null;

    try {
      // Get database connection
      db = WorkflowJDOManager.getDatabase();

      // begin transaction
      db.begin();

      // Re-load process instance
      UpdatableProcessInstance copyInstance =
          (UpdatableProcessInstance) db.load(ProcessInstanceImpl.class, instance.getInstanceId());

      // Do workflow stuff
      try {
        // unlock instance for user
        copyInstance.unLock(state, user);
      } catch (WorkflowException we) {
        db.rollback();
        throw new WorkflowException(
            "ProcessInstanceManagerImpl.unlock", "workflowEngine.EX_ERR_UNLOCK", we);
      }

      // commit
      db.commit();
    } catch (PersistenceException pe) {
      throw new WorkflowException(
          "ProcessInstanceManagerImpl.unlock", "workflowEngine.EX_ERR_CASTOR_UNLOCK", pe);
    } finally {
      WorkflowJDOManager.closeDatabase(db);
    }
  }
示例#7
0
  private void iterateStatesOID(final Locked locked, final AccessMode mode) throws Exception {
    _queryStateOID.bind(locked.getId());
    QueryResults results = _queryStateOID.execute(mode);

    while (results.hasMore()) {
      OID oid = (OID) results.next();
      iterateEquipmentsOID((State) _db.load(State.class, oid.getId(), mode), mode);
    }
  }
示例#8
0
  private void iterateEquipmentsOID(final State state, final AccessMode mode) throws Exception {
    _queryEquipmentOID.bind(state.getId());
    QueryResults results = _queryEquipmentOID.execute(Database.ReadOnly);

    while (results.hasMore()) {
      OID oid = (OID) results.next();
      iterateServicesOID((Equipment) _db.load(Equipment.class, oid.getId(), mode), mode);
    }
  }
示例#9
0
  private void iterateServicesOID(final Equipment equipment, final AccessMode mode)
      throws Exception {
    _queryServiceOID.bind(equipment.getId());
    QueryResults results = _queryServiceOID.execute(Database.ReadOnly);

    while (results.hasMore()) {
      OID oid = (OID) results.next();
      _db.load(Service.class, oid.getId(), mode);
    }
  }
示例#10
0
  public void testRollback() throws Exception {
    Database db = getJDOManager(DBNAME, MAPPING).getDatabase();
    db.begin();
    LOG.info("Preparing for rollback bug");

    // Prepare the database: add a NewProduct, which references the same
    // ProductGroup as most Product objects do
    ProductGroup group = (ProductGroup) db.load(ProductGroup.class, new Integer(3));
    NewProduct newProduct = null;
    try {
      newProduct = (NewProduct) db.load(NewProduct.class, new Integer(1));
    } catch (ObjectNotFoundException e) {
      newProduct = new NewProduct();
      newProduct.setId(1);
      newProduct.setName("NewProduct1");
      newProduct.setPrice(1.0f);
      newProduct.setGroup(group);
      db.create(newProduct);
      LOG.info("NewProduct1 created.");
    }

    db.commit();

    // Now the data are prepared:
    // We have Product7 and NewProduct1, both are pointing to Group3
    // Below we will load both objects in on transaction and then call rollback
    db.begin();
    LOG.info("Trying to reproduce rollback bug");

    // loading both product entities
    newProduct = (NewProduct) db.load(NewProduct.class, new Integer(1));
    Product product = (Product) db.load(Product.class, new Integer(7));
    LOG.debug("Product loaded: " + product);
    LOG.debug("NewProduct loaded: " + newProduct);

    // we only loaded both objects in one tx. Now we are doing a rollback.
    LOG.info("Calling Rollback");
    db.rollback();

    LOG.info("End transaction: Trying to reproduce rollback bug");
    db.close();
  }
示例#11
0
  public void testLazyCollectionRollback() throws PersistenceException, SQLException {

    stream.println("Running testLazyCollectionRollback...");

    Complex fullname = new Complex("First", "Person");

    // set up the data object
    _db.begin();
    TestLazyEmployee loadPerson = (TestLazyEmployee) _db.load(TestLazyEmployee.class, fullname);
    Collection projects = loadPerson.getProjects();
    TestLazyProject project = new TestLazyProject();

    project = new TestLazyProject();
    project.setId(1002);
    project.setName("Project Two");
    project.setOwner(loadPerson);
    projects.add(project);
    _db.create(project);

    _db.commit();

    _db.begin();
    loadPerson = (TestLazyEmployee) _db.load(TestLazyEmployee.class, fullname);
    projects = loadPerson.getProjects();

    // is the collection populated?
    assertTrue("The projects collection is not valid!.", projects != null && projects.size() > 0);

    // is the collection instanceof Lazy?
    assertTrue(
        "Collection has to be lazy! It is " + loadPerson.getProjects().getClass(),
        loadPerson.getProjects() instanceof org.exolab.castor.persist.Lazy);

    // OK, the collection of projects is there, let's test a rollback for bug #1046
    stream.println("Rolling back transaction");
    _db.rollback();

    // test it again since the rollback - is the collection instanceof Lazy?
    assertTrue(
        "Collection has to be lazy! It is " + loadPerson.getProjects().getClass(),
        loadPerson.getProjects() instanceof org.exolab.castor.persist.Lazy);
  }
示例#12
0
  /**
   * Test method.
   *
   * @throws Exception For any exception thrown.
   */
  public void testLoadChild() throws Exception {
    Database db = _category.getDatabase();
    db.begin();

    Child child = (Child) db.load(Child.class, new Integer(1));

    assertNotNull(child);
    assertEquals(new Integer(1), child.getId());

    db.commit();
    db.close();
  }
示例#13
0
  /**
   * Test method.
   *
   * @throws Exception For any exception thrown.
   */
  public void testQueryEntityOne() throws Exception {
    Database db = _category.getDatabase();
    db.begin();

    Parent entity = (Parent) db.load(Parent.class, new Integer(1));

    assertNotNull(entity);
    assertEquals(new Integer(1), entity.getId());

    db.commit();
    db.close();
  }
示例#14
0
  public void testLoadTruck() throws Exception {
    Database database = _category.getDatabase();

    database.begin();
    Truck truck = (Truck) database.load(Truck.class, new Integer(5));
    database.commit();

    assertNotNull(truck);
    assertEquals(5, truck.getId());

    database.close();
  }
示例#15
0
  public void testLoadComputer() throws Exception {
    Database database = _category.getDatabase();

    database.begin();
    Computer computer = (Computer) database.load(Computer.class, new Integer(2));
    database.commit();

    assertNotNull(computer);
    assertEquals("ctf.jdo.tc9x.Laptop", computer.getClass().getName());
    assertEquals(2, computer.getId());
    assertEquals("laptop 2", computer.getName());

    database.close();
  }
示例#16
0
  public void testLoadLaptop() throws Exception {
    Database database = _category.getDatabase();

    database.begin();
    Laptop laptop = (Laptop) database.load(Laptop.class, new Integer(1));
    database.commit();

    assertNotNull(laptop);
    assertEquals("ctf.jdo.tc9x.Laptop", laptop.getClass().getName());
    assertEquals(1, laptop.getId());
    assertEquals("laptop 1", laptop.getName());

    database.close();
  }
示例#17
0
  public void testLoadServer() throws Exception {
    Database database = _category.getDatabase();

    database.begin();
    Server server = (Server) database.load(Server.class, new Integer(3));
    database.commit();

    assertNotNull(server);
    assertEquals("ctf.jdo.tc9x.Server", server.getClass().getName());
    assertEquals(3, server.getId());
    assertEquals("server 3", server.getName());

    database.close();
  }
示例#18
0
  public void testManyToMany() throws PersistenceException, SQLException {

    stream.println("Running testManyToMany...");

    _db.begin();

    String key = "a1";
    TestLazyNToNA a1 = (TestLazyNToNA) _db.load(TestLazyNToNA.class, key);

    // The object a1 should have two TestLazyNToNB objects in its
    // "refs" collection
    Collection lazyRefs = a1.getRefs();
    if (lazyRefs.size() != 2) {
      stream.println("Error: incorrect initial collection size in testManyToMany");
      fail("Error: incorrect initial collection size in testManyToMany");
    }

    lazyRefs.clear();
    _db.commit();

    // Now if we re-load the object in a new transaction, there should
    // be no objects in the "refs" collection

    _db.begin();
    a1 = (TestLazyNToNA) _db.load(TestLazyNToNA.class, key);

    // The object a1 should have two TestLazyNToNB objects in its
    // "refs" collection
    lazyRefs = a1.getRefs();
    if (lazyRefs.size() > 2) {
      stream.println("Error: incorrect final collection size in testManyToMany");
      fail("Error: incorrect final collection size in testManyToMany");
    }

    _db.commit();
  }
示例#19
0
  /**
   * Test method.
   *
   * @throws Exception For any exception thrown.
   */
  public void testLoadEntityWithCompoundId() throws Exception {
    Database db = _category.getDatabase();
    db.begin();

    ParentWithCompoundId child =
        (ParentWithCompoundId)
            db.load(ParentWithCompoundId.class, new Complex(new Integer(1), new Integer(1)));

    assertNotNull(child);
    assertEquals(new Integer(1), child.getId1());
    assertEquals(new Integer(1), child.getId2());

    db.commit();
    db.close();
  }
示例#20
0
  public void testLoadCar() throws Exception {
    Database database = _category.getDatabase();

    database.begin();
    Object object = database.load(Car.class, new Integer(5));
    assertNotNull(object);
    assertEquals("ctf.jdo.tc9x.Truck", object.getClass().getName());
    Truck truck = (Truck) object;
    database.commit();

    assertNotNull(truck);
    assertEquals(5, truck.getId());

    database.close();
  }
示例#21
0
  public void testReadOnlyOidEmpty() throws Exception {
    long start = System.currentTimeMillis();

    _db = _jdo.getDatabase();
    _db.getCacheManager().expireCache();
    _db.begin();

    long begin = System.currentTimeMillis();

    OQLQuery query =
        _db.getOQLQuery(
            "CALL SQL select PTF_LOCKED.ID as ID "
                + "from PTF_LOCKED order by PTF_LOCKED.ID "
                + "AS ptf.jdo.rel1toN.OID");
    QueryResults results = query.execute(Database.ReadOnly);

    long result = System.currentTimeMillis();

    initIterateQueriesOID();

    int count = 0;
    while (results.hasMore()) {
      OID oid = (OID) results.next();
      iterateStatesOID(
          (Locked) _db.load(Locked.class, oid.getId(), Database.ReadOnly), Database.ReadOnly);

      count++;
    }

    long iterate = System.currentTimeMillis();

    _db.commit();

    long commit = System.currentTimeMillis();

    _db.close();

    long close = System.currentTimeMillis();

    LOG.info(
        format(
            "ReadOnlyOidEmpty",
            DF.format(begin - start),
            DF.format(result - begin),
            DF.format(iterate - result),
            DF.format(commit - iterate),
            DF.format(close - commit)));
  }
示例#22
0
  public void testLoadComputerMulti() throws Exception {
    Database database = _category.getDatabase();

    database.begin();
    ComputerMulti computer =
        (LaptopMulti)
            database.load(ComputerMulti.class, new Identity(new Integer(1), new Integer(1)));
    database.commit();

    assertNotNull(computer);
    assertEquals(1, computer.getId1());
    assertEquals(1, computer.getId2());
    assertEquals("laptop 1", computer.getName());

    database.close();
  }
示例#23
0
  public void testLoadM() throws Exception {
    Database database = _category.getDatabase();

    database.begin();
    M m = (M) database.load(M.class, new Integer(1));
    database.commit();

    assertNotNull(m);
    assertEquals(1, m.getId());
    assertEquals("m1", m.getName());

    Collection ns = m.getNs();
    assertNotNull(ns);
    assertEquals(2, ns.size());

    database.close();
  }
示例#24
0
  public void testLoadOwner() throws Exception {
    Database database = _category.getDatabase();

    database.begin();
    Owner owner = (Owner) database.load(Owner.class, new Integer(1));
    database.commit();

    assertNotNull(owner);
    assertEquals(1, owner.getId().intValue());
    assertEquals("owner 1", owner.getName());

    Product product = owner.getProduct();
    assertNotNull(product);
    assertEquals(1, product.getId());
    assertEquals("ctf.jdo.tc9x.Laptop", product.getClass().getName());

    database.close();
  }
示例#25
0
 public void run() {
     boolean succeed;
     int trials = 0;
     TestRace tr;
     try {
         for ( int i=0; i < trial && !other.isDone(); i++ ) {
             try {
                 // load it and modify it
                 db.begin();
                 succeed = false;
                 trials = 0;
                 while ( !succeed && trials < NUM_OF_RETRIAL && !other.isDone() ) {
                     trials++;
                     try {
                         tr = (TestRace) db.load( _classType, id, Database.Shared );
                                             // may throw ObjectNotFoundException
                                             // LockNotGrantedException
                         db.commit();
                         succeed = true;
                     } catch ( LockNotGrantedException e ) {
                         succeed = false;
                         // ethernet way of retry
                         Thread.currentThread().sleep( (long) ((SLEEP_BASE_TIME^trials) * ran.nextDouble()) );
                     } catch ( ObjectNotFoundException e ) {
                         succeed = false;
                         // ethernet way of retry
                         Thread.currentThread().sleep( (long) ((SLEEP_BASE_TIME^trials) * ran.nextDouble()) );
                     } catch ( TransactionAbortedException e ) {
                         succeed = false;
                         // ethernet way of retry
                         Thread.currentThread().sleep( (long) ((SLEEP_BASE_TIME^trials) * ran.nextDouble()) );
                     }
                     Thread.currentThread().sleep( 0 );
                 }
                 if ( db.isActive() ) 
                     db.rollback();
             
             } catch ( Exception e ) {
             }
         }
     } finally {
         isDone = true;
     }
 }
  /**
   * Get the process instances for a given instance id
   *
   * @param instanceId id of searched instance
   * @return the searched process instance
   */
  public ProcessInstance getProcessInstance(String instanceId) throws WorkflowException {
    ProcessInstanceImpl instance;

    Database db = null;
    try {
      // Constructs the query
      db = WorkflowJDOManager.getDatabase();
      db.begin();
      instance = (ProcessInstanceImpl) db.load(ProcessInstanceImpl.class, instanceId);
      db.commit();

      return instance;
    } catch (PersistenceException pe) {
      throw new WorkflowException(
          "ProcessInstanceManagerImpl.getProcessInstance", "EX_ERR_CASTOR_GET_INSTANCE", pe);
    } finally {
      WorkflowJDOManager.closeDatabase(db);
    }
  }
示例#27
0
  public void testCreateAndLoadComputer() throws Exception {
    Database database = _category.getDatabase();

    database.begin();
    Order order = new Order();
    order.setId(12);
    order.setName("order 12");
    database.create(order);
    database.commit();

    database.begin();
    ProductDetail detail = new ProductDetail();
    detail.setId(12);
    detail.setCategory("category 12");
    detail.setLocation("location 12");
    database.create(detail);
    database.commit();

    database.begin();
    Laptop laptop = new Laptop();
    laptop.setId(12);
    laptop.setName("laptop 12");
    laptop.setCpu("centrino");
    laptop.setResolution("1600");
    laptop.setWeight(2450);
    laptop.setDetail((ProductDetail) database.load(ProductDetail.class, new Integer(12)));
    database.create(laptop);
    Collection orders = new LinkedList();
    orders.add(database.load(Order.class, new Integer(12)));
    laptop.setOrders(orders);
    database.commit();

    database.begin();
    Computer computer = (Computer) database.load(Computer.class, new Integer(12));
    database.commit();

    assertNotNull(computer);
    assertEquals("ctf.jdo.tc9x.Laptop", computer.getClass().getName());
    assertEquals(12, computer.getId());
    assertEquals("laptop 12", computer.getName());

    database.begin();
    computer = (Computer) database.load(Computer.class, new Integer(12));
    database.remove(computer);
    database.remove(database.load(ProductDetail.class, new Integer(12)));
    database.remove(database.load(Order.class, new Integer(12)));
    database.commit();

    database.close();
  }
示例#28
0
  public void testCreateAndLoadCar() throws Exception {
    Database database = _category.getDatabase();

    database.begin();
    Order order = new Order();
    order.setId(11);
    order.setName("order 11");
    database.create(order);
    database.commit();

    database.begin();
    ProductDetail detail = new ProductDetail();
    detail.setId(11);
    detail.setCategory("category 11");
    detail.setLocation("location 11");
    database.create(detail);
    database.commit();

    database.begin();
    Truck truck = new Truck();
    truck.setId(11);
    truck.setName("truck 11");
    truck.setKw(112);
    truck.setMake("Fiat");
    truck.setMaxWeight(3750);
    truck.setDetail((ProductDetail) database.load(ProductDetail.class, new Integer(11)));
    database.create(truck);
    Collection orders = new LinkedList();
    orders.add(database.load(Order.class, new Integer(11)));
    truck.setOrders(orders);
    database.commit();

    database.begin();
    Object object = database.load(Car.class, new Integer(11));
    assertNotNull(object);
    assertEquals("ctf.jdo.tc9x.Truck", object.getClass().getName());
    Truck loadedTruck = (Truck) object;
    database.commit();

    assertNotNull(loadedTruck);
    assertEquals(11, loadedTruck.getId());

    database.begin();
    database.remove(database.load(Car.class, new Integer(11)));
    database.remove(database.load(ProductDetail.class, new Integer(11)));
    database.remove(database.load(Order.class, new Integer(11)));
    database.commit();

    database.close();
  }
示例#29
0
  public void runTest() throws PersistenceException {
    TimeStampableObject object;
    CallbacksInvoked cbi = new CallbacksInvoked();

    cbi.allow(CallbacksInvoked.CREATING);
    cbi.allow(CallbacksInvoked.USING);
    cbi.allow(CallbacksInvoked.CREATED);
    cbi.allow(CallbacksInvoked.STORING);
    cbi.allow(CallbacksInvoked.RELEASING);

    _i._callbacksInvoked.init();
    _db.begin();

    object = new TimeStampableObject();
    LOG.debug("Creating new object: " + object);
    _db.create(object);

    _db.commit();

    if (!cbi.equals(_i._callbacksInvoked)) {
      LOG.error("Callbacks were not properly invoked: " + cbi + " != " + _i._callbacksInvoked);
      fail("Callbacks were not properly invoked: " + cbi + " != " + _i._callbacksInvoked);
    }

    cbi.init();
    cbi.allow(CallbacksInvoked.USING);
    cbi.allow(CallbacksInvoked.LOADED);
    cbi.allow(CallbacksInvoked.STORING);
    cbi.allow(CallbacksInvoked.RELEASING);
    cbi.allow(CallbacksInvoked.INSTANTIATE);

    _i._callbacksInvoked.init();
    _db.begin();

    object =
        (TimeStampableObject)
            _db.load(TimeStampableObject.class, new Integer(TimeStampableObject.DEFAULT_ID));
    object.setValue1("Alan");

    _db.commit();

    if (!cbi.equals(_i._callbacksInvoked)) {
      LOG.error("Callbacks were not properly invoked: " + cbi + " != " + _i._callbacksInvoked);
      fail("Callbacks were not properly invoked: " + cbi + " != " + _i._callbacksInvoked);
    }

    cbi.init();
    cbi.allow(CallbacksInvoked.USING);
    cbi.allow(CallbacksInvoked.UPDATED);
    cbi.allow(CallbacksInvoked.STORING);
    cbi.allow(CallbacksInvoked.RELEASING);

    _i._callbacksInvoked.init();

    object.setValue2("long transaction new value");
    _db.begin();
    _db.update(object);
    _db.commit();

    if (!cbi.equals(_i._callbacksInvoked)) {
      LOG.error("Callbacks were not properly invoked: " + cbi + " != " + _i._callbacksInvoked);
      fail("Callbacks were not properly invoked: " + cbi + " != " + _i._callbacksInvoked);
    }

    cbi.init();
    cbi.allow(CallbacksInvoked.USING);
    cbi.allow(CallbacksInvoked.LOADED);
    cbi.allow(CallbacksInvoked.REMOVING);
    cbi.allow(CallbacksInvoked.REMOVED);
    cbi.allow(CallbacksInvoked.RELEASING);
    cbi.allow(CallbacksInvoked.INSTANTIATE);

    _i._callbacksInvoked.init();
    _db.begin();

    object =
        (TimeStampableObject)
            _db.load(TimeStampableObject.class, new Integer(TimeStampableObject.DEFAULT_ID));
    _db.remove(object);

    _db.commit();

    if (!cbi.equals(_i._callbacksInvoked)) {
      LOG.error("Callbacks were not properly invoked: " + cbi + " != " + _i._callbacksInvoked);
      fail("Callbacks were not properly invoked: " + cbi + " != " + _i._callbacksInvoked);
    }
  }
示例#30
0
        public void run() {
            try {
                int num = 0;
                stream.writeVerbose("start testing");
                TestRace tr;
                TestRace testrace;
                OQLQuery oql;
                QueryResults qr;
                boolean succeed;
                int trials;
                Integer id = new Integer(5);

                out:
                for ( int i=0; i<trial; i++ ) {
                    // create, modified, delete object
                    try {
                        switch ( cachetype ) {
                        case 0:
                            testrace = new TestRaceCount();
                            testrace.setId(5);
                            break;
                        case 1:
                            testrace = new TestRaceTime();
                            testrace.setId(5);
                            break;
                        case 2:
                            testrace = new TestRaceNone();
                            testrace.setId(5);
                            break;
                        case 3:
                            testrace = new TestRaceUnlimited();
                            testrace.setId(5);
                            break;
                        default:
                            testrace = null;
                        }
     
                        // create object
                        //try {
                            db.begin();
                            db.create( testrace );  // may throw duplicateIdentityException
                            db.commit();
                        //} catch ( Exception e ) {
                        //    e.printStackTrace();
                        //}

                        // load it and modify it
                        succeed = false;
                        trials = 0;
                        while ( !succeed && trials < NUM_OF_RETRIAL ) {
                            Thread.currentThread().sleep( 0 );
                            trials++;
                            try {
                                db.begin();
                                tr = (TestRace) db.load( _classType, id );
                                                    // may throw ObjectNotFoundException
                                                    // LockNotGrantedException
                                tr.incValue1();
                                db.commit();
                                succeed = true;
                            } catch ( LockNotGrantedException e ) {
                                succeed = false;
                                // ethernet way of retry
                                if ( db.isActive() ) db.rollback();
                                Thread.currentThread().sleep( (long) ((SLEEP_BASE_TIME^trials) * ran.nextDouble()) );
                            } catch ( TransactionAbortedException e ) {
                                succeed = false;
                                // ethernet way of retry
                                if ( db.isActive() ) db.rollback();
                                Thread.currentThread().sleep( (long) ((SLEEP_BASE_TIME^trials) * ran.nextDouble()) );
                            } 
                        }
                        if ( db.isActive() ) 
                            db.rollback();

                        // load it and release it
                        succeed = false;
                        trials = 0;
                        while ( !succeed && trials < NUM_OF_RETRIAL ) {
                            Thread.currentThread().sleep( 0 );
                            trials++;
                            try {
                                db.begin();
                                tr = (TestRace) db.load( _classType, id );
                                                    // may throw ObjectNotFoundException
                                                    // LockNotGrantedException
                                db.commit();
                                succeed = true;
                            } catch ( LockNotGrantedException e ) {
                                succeed = false;
                                // ethernet way of retry
                                if ( db.isActive() ) db.rollback();
                                Thread.currentThread().sleep( (long) ((SLEEP_BASE_TIME^trials) * ran.nextDouble()) );
                            } 
                        }
                        if ( db.isActive() ) 
                            db.rollback();

                        // load it and delete it
                        succeed = false;
                        trials = 0;
                        while ( !succeed && trials < NUM_OF_RETRIAL ) {
                            Thread.currentThread().sleep( 0 );
                            trials++;    
                            try {
                                db.begin();
                                tr = (TestRace) db.load( _classType, id );
                                                // may throw ObjectNotFoundException
                                                // LockNotGrantedException
                                db.remove( tr );
                                db.commit();
                                succeed = true;
                            } catch ( LockNotGrantedException e ) {
                                succeed = false;
                                if ( db.isActive() ) db.rollback();
                                Thread.currentThread().sleep( (long) ((SLEEP_BASE_TIME^trials) * ran.nextDouble()) );
                            } catch ( TransactionAbortedException e ) {
                                succeed = false;
                                // ethernet way of retry
                                if ( db.isActive() ) db.rollback();
                                Thread.currentThread().sleep( (long) ((SLEEP_BASE_TIME^trials) * ran.nextDouble()) );
                            }
                        }
                        if ( db.isActive() ) 
                            db.rollback();
                        if ( !succeed )
                            throw new Exception("Transaction can't not lock the object within "+trials+" trials");

                    } catch ( TransactionNotInProgressException e ) {
                        stream.writeVerbose( "Thread <CreateDelete> will be killed. Unexcepted exception: "+e.getException() );
                        e.printStackTrace();
                        if ( db.isActive() ) try { db.rollback(); } catch ( TransactionNotInProgressException ee ) {}
                        _errLeak = true;
                        break out;
                    } catch ( PersistenceException e ) {
                        stream.writeVerbose( "Thread <CreateDelete> will be killed. Unexcepted exception: " );
                        e.printStackTrace();
                        if ( db.isActive() ) try { db.rollback(); } catch ( TransactionNotInProgressException ee ) {}
                        _errLeak = true;
                        break out;
                    } catch ( Exception e ) {
                        stream.writeVerbose( "Thread <CreateDelete> will be killed. Element not found: other exception: "+e );
                        e.printStackTrace();
                        if ( db.isActive() ) try { db.rollback(); } catch ( TransactionNotInProgressException ee ) {}
                        _errLeak = true;
                        break out;
                    }
                }

            } finally {
                isDone = true;
            }
        }