示例#1
0
  /**
   * This method is called by the tests and preform the actual concurrent modification test.
   *
   * @param accessMode the access mode that is used in the concurrent modification tests
   */
  private void testDirtyIgnored(final AccessMode accessMode)
      throws PersistenceException, SQLException {
    OQLQuery oql;
    Sample object;
    QueryResults enumeration;

    // Open transaction in order to perform JDO operations
    _db.begin();

    // Determine if test object exists, if not create it.
    // If it exists, set the name to some predefined value
    // that this test will later override.
    oql = _db.getOQLQuery("SELECT object FROM " + Sample.class.getName() + " object WHERE id = $1");
    oql.bind(Sample.DEFAULT_ID);

    enumeration = oql.execute();
    if (enumeration.hasMore()) {
      object = (Sample) enumeration.next();
      LOG.debug("Retrieved object: " + object);
      object.setValue1(Sample.DEFAULT_VALUE_1);
      object.setValue2(Sample.DEFAULT_VALUE_2);
    } else {
      object = new Sample();
      LOG.debug("Creating new object: " + object);
      _db.create(object);
    }

    _db.commit();

    // Open a new transaction in order to conduct test
    _db.begin();

    oql.bind(new Integer(Sample.DEFAULT_ID));
    object = (Sample) oql.execute(accessMode).nextElement();
    object.setValue2(JDO_VALUE);

    // Perform direct JDBC access and override the value of that table
    if (accessMode != Database.DbLocked) {
      _conn
          .createStatement()
          .execute(
              "UPDATE tc0x_sample SET value2='" + JDBC_VALUE + "' WHERE id=" + Sample.DEFAULT_ID);
      LOG.debug("Updated test object from JDBC");
    }

    // Commit JDO transaction, this should report object modified exception
    LOG.debug("Commit update: no dirty checking field not modified");
    try {
      _db.commit();
      LOG.debug("OK: ObjectModifiedException not thrown");
    } catch (ObjectModifiedException ex) {
      if (_db.isActive()) {
        _db.rollback();
      }
      LOG.error("Error: ObjectModifiedException thrown", ex);
      fail("ObjectModifiedException thrown");
    }
  }
示例#2
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;
     }
 }
示例#3
0
  public void tearDown() throws PersistenceException {
    if (_db.isActive()) {
      _db.rollback();
    }

    OQLQuery oql;
    QueryResults qres;

    LOG.debug("Delete everything");
    _db.begin();
    oql =
        _db.getOQLQuery("SELECT p FROM " + TimeStampableObject.class.getName() + " p WHERE id=$1");
    oql.bind(TimeStampableObject.DEFAULT_ID);
    qres = oql.execute();
    while (qres.hasMore()) {
      _db.remove(qres.next());
    }
    oql.close();
    _db.commit();

    _db.close();

    _jdo.setCallbackInterceptor(null);
  }
示例#4
0
 public void tearDown() throws Exception {
   if (_db.isActive()) {
     _db.rollback();
   }
   _db.close();
 }
示例#5
0
 /** Close the database and JDBC connection */
 public void tearDown() throws PersistenceException, SQLException {
   if (_db.isActive()) {
     _db.rollback();
   }
   _db.close();
 }
示例#6
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;
            }
        }
示例#7
0
  public boolean run(CWVerboseStream stream) {
    try {

      _db = _category.getDatabase(stream.verbose());
      _conn = _category.getJDBCConnection();

      stream.writeVerbose("Running...");
      stream.writeVerbose("");

      // delete everything
      _conn.createStatement().executeUpdate("DELETE test_serial");
      _conn.commit();

      // create new object with an serializable dependent object
      _db.begin();
      TestSerial master = new TestSerial();
      master.setId(1);
      master.setSerializableObject(new TestSerializableObject());
      master.getSerializableObject().aCoolString = "Very cool!";
      master.getSerializableObject().ints = new int[] {1, 3, 5, 7, 9};
      _db.create(master);
      _db.commit();

      // test if object created properly
      _db.begin();
      TestSerial testSerial = (TestSerial) _db.load(TestSerial.class, new Integer(1));
      if (testSerial == null) throw new Exception("Object creation failed!");

      if (testSerial.getSerializableObject() == null
          || !testSerial.getSerializableObject().aCoolString.equals("Very cool!")
          || testSerial.getSerializableObject().ints == null
          || testSerial.getSerializableObject().ints.length != 5
          || testSerial.getSerializableObject().ints[0] != 1
          || testSerial.getSerializableObject().ints[1] != 3
          || testSerial.getSerializableObject().ints[2] != 5
          || testSerial.getSerializableObject().ints[3] != 7
          || testSerial.getSerializableObject().ints[4] != 9)
        throw new Exception("dependent objects creation failed!" + testSerial);

      // modify the object
      testSerial.getSerializableObject().ints[1] = 103;
      testSerial.getSerializableObject().ints[3] = 107;
      testSerial.getSerializableObject().aCoolString = "Very very cool!";
      _db.commit();

      _db.begin();
      testSerial = (TestSerial) _db.load(TestSerial.class, new Integer(1));
      if (testSerial == null) throw new Exception("dependent modfiication failed!" + testSerial);

      if (testSerial.getSerializableObject() == null
          || !testSerial.getSerializableObject().aCoolString.equals("Very very cool!")
          || testSerial.getSerializableObject().ints == null
          || testSerial.getSerializableObject().ints.length != 5
          || testSerial.getSerializableObject().ints[0] != 1
          || testSerial.getSerializableObject().ints[1] != 103
          || testSerial.getSerializableObject().ints[2] != 5
          || testSerial.getSerializableObject().ints[3] != 107
          || testSerial.getSerializableObject().ints[4] != 9)
        throw new Exception("dependent modification failed!" + testSerial);

      // set the field to null;
      testSerial.getSerializableObject().ints = null;
      testSerial.getSerializableObject().aCoolString = null;
      _db.commit();

      _db.begin();
      testSerial = (TestSerial) _db.load(TestSerial.class, new Integer(1));
      if (testSerial == null) throw new Exception("dependent modfiication failed!" + testSerial);

      if (testSerial.getSerializableObject() == null
          || testSerial.getSerializableObject().aCoolString != null
          || testSerial.getSerializableObject().ints != null)
        throw new Exception("dependent modification failed!" + testSerial);

      // setSerializableObject( null );
      testSerial.setSerializableObject(null);
      _db.commit();

      _db.begin();
      testSerial = (TestSerial) _db.load(TestSerial.class, new Integer(1));
      if (testSerial == null) throw new Exception("dependent modfiication failed!" + testSerial);

      if (testSerial.getSerializableObject() != null)
        throw new Exception("dependent modification failed!" + testSerial);
      _db.commit();

    } catch (Exception e) {
      e.printStackTrace();

      stream.writeVerbose("Exception: " + e);
      try {
        if (_db.isActive()) _db.close();
        return false;
      } catch (Exception ex) {
        return false;
      }
    }
    return true;
  }