Package com.google.appengine.api.datastore

Examples of com.google.appengine.api.datastore.Transaction


   
    _logger.info("Key to add names for: " + key);
   
    DatastoreService datastoreService = DatastoreServiceFactory.getDatastoreService();

    Transaction txn = datastoreService.beginTransaction();
   
    try {
      Entity entity = datastoreService.get(txn, key);
     
      @SuppressWarnings("unchecked")
      List<String> localNames = (List<String>) entity.getProperty("names");
     
      if (null == localNames) {
        _logger.info("names property is null. Creating new names property list");
        localNames = new ArrayList<String>();
      }
     
      Set<String> localNamesSet = new HashSet<String>(localNames);
      for (String name : names) {
        if (!localNamesSet.contains(name)) {
          localNamesSet.add(name);
        }
      }
     
      localNames = new ArrayList<String>(localNamesSet);
     
      try {
        entity.setProperty("names", localNames);
       
        datastoreService.put(txn, entity);
       
        txn.commit();

        return ResultType.ADD_SUCCESS;
      } catch (Exception e) {
        _logger.warning("Failure to save entity with names: " + e);
        if (localNames.size() >= 5000) { //python has an error about size, but we'll just artificially limit it to 1000 here
          return ResultType.ADD_FULL;
        } else {
          return ResultType.ADD_FAIL;
        }
      }
    } catch (Exception e) {
      return ResultType.ADD_FAIL;
    } finally {
      if (txn.isActive()) {
        _logger.info("Rolling back transaction");
        txn.rollback();
      }
    }
  }
View Full Code Here


   * @param entity
   *            : entity to be persisted
   */
  public static void persistEntity(Entity entity) {
    //logger.info("Grabando entidad");
    Transaction txn = null;
    try {
      txn = datastore.beginTransaction();
      datastore.put(entity);
      txn.commit();
    } catch (IllegalArgumentException e) {
      logger.fatal(e.getMessage());
    } catch (ConcurrentModificationException e) {
      logger.fatal(e.getMessage());
    } catch (DatastoreFailureException e) {
      logger.fatal(e.getMessage());
    } catch (IllegalStateException e) {
      logger.fatal(e.getMessage());
    } finally {
      if (txn.isActive()) {
        txn.rollback();
      }
    }
  }
View Full Code Here

    }
  }
 
  public static void deleteEntity(Key key) {
    //logger.info("Grabando entidad");
    Transaction txn = null;
    try {
      txn = datastore.beginTransaction();
      datastore.delete(key);
      txn.commit();
    } catch (IllegalArgumentException e) {
      logger.fatal(e.getMessage());
    } catch (ConcurrentModificationException e) {
      logger.fatal(e.getMessage());
    } catch (DatastoreFailureException e) {
      logger.fatal(e.getMessage());
    } catch (IllegalStateException e) {
      logger.fatal(e.getMessage());
    } finally {
      if (txn.isActive()) {
        txn.rollback();
      }
    }
  } 
View Full Code Here

    }
  } 
 
  public static void deleteEntitys(Iterable<Key> keys) {
    //logger.info("Grabando entidad");
    Transaction txn = null;
    try {
      txn = datastore.beginTransaction();
      datastore.delete(keys);
      txn.commit();
    } catch (IllegalArgumentException e) {
      logger.fatal(e.getMessage());
    } catch (ConcurrentModificationException e) {
      logger.fatal(e.getMessage());
    } catch (DatastoreFailureException e) {
      logger.fatal(e.getMessage());
    } catch (IllegalStateException e) {
      logger.fatal(e.getMessage());
    } finally {
      if (txn.isActive()) {
        txn.rollback();
      }
    }
  }
View Full Code Here

    }
  }

  @Override
  public boolean deleteObject(GcsFilename filename, long timeoutMillis) throws IOException {
    Transaction tx = DATASTORE.beginTransaction();
    Key key = makeKey(filename);
    try {
      DATASTORE.get(tx, key);
      DATASTORE.delete(tx, key);
    } catch (EntityNotFoundException ex) {
      return false;
    } finally {
      if (tx.isActive()) {
        tx.commit();
      }
    }
    FILES.delete(nameToAppEngineFile(filename));
    return true;
  }
View Full Code Here

      Entity bidirEntity = new Entity(bidirKind, pojoEntity.getKey());
      bidirEntity.setProperty("childVal", "yap");
      ds.put(bidirEntity);

      Transaction txn = EasyMock.createMock(Transaction.class);
      EasyMock.expect(txn.getId()).andReturn("1").times(2);
      txn.commit();
      EasyMock.expectLastCall();
      EasyMock.replay(txn);
      EasyMock.expect(mockDatastore.beginTransaction(EasyMock.isA(TransactionOptions.class))).andReturn(txn);
      // the only get we're going to perform is for the pojo
      EasyMock.expect(mockDatastore.get(txn, pojoEntity.getKey())).andReturn(pojoEntity);
View Full Code Here

    // DatastoreService mock
    pmf.close();
    tearDown();
    DatastoreService mockDatastore = EasyMock.createMock(DatastoreService.class);
    DatastoreServiceFactoryInternal.setDatastoreService(mockDatastore);
    Transaction txn;
    try {
      setUp();

      Entity pojoEntity = new Entity(HasOneToOneJDO.class.getSimpleName());
      ds.put(pojoEntity);

      Entity flightEntity = Flight.newFlightEntity(
          pojoEntity.getKey(), null, "jimmy", "bos", "mia", 5, 4, 33);
      ds.put(flightEntity);

      Entity hasKeyPkEntity = new Entity(HasKeyPkJDO.class.getSimpleName(), pojoEntity.getKey());
      hasKeyPkEntity.setProperty("str", "yar");
      ds.put(hasKeyPkEntity);

      Entity hasParentEntity =
          new Entity(HasOneToOneParentJDO.class.getSimpleName(), pojoEntity.getKey());
      hasParentEntity.setProperty("str", "yap");
      ds.put(hasParentEntity);

      Entity hasParentKeyPkEntity =
          new Entity(HasOneToOneParentKeyPkJDO.class.getSimpleName(), pojoEntity.getKey());
      hasParentKeyPkEntity.setProperty("str", "yag");
      ds.put(hasParentKeyPkEntity);

      // the only get we're going to perform is for the pojo
      txn = EasyMock.createMock(Transaction.class);
      EasyMock.expect(txn.getId()).andReturn("1").times(2);
      txn.commit();
      EasyMock.expectLastCall();
      EasyMock.replay(txn);
      EasyMock.expect(mockDatastore.beginTransaction(EasyMock.isA(TransactionOptions.class))).andReturn(txn);
      EasyMock.expect(mockDatastore.get(txn, pojoEntity.getKey())).andReturn(pojoEntity);
      EasyMock.replay(mockDatastore);
View Full Code Here

    }
  }

  Future<Map<T, Key>> storeResultsLater()
  {
    Transaction transaction = command.datastore.getTransaction();
    final Map<T, Entity> entities = command.datastore.instancesToEntities(instances, parent, batch);
    if (unique)
    {
      checkUniqueKeys(entities.values());
    }
View Full Code Here

    recorder.delete(txn, keys);
    delegate.delete(txn, keys);
  }

  public Transaction beginTransaction() {
    Transaction txn =  delegate.beginTransaction();
    txnIdAnswer.setExpectedTxnId(txn.getId());
    recorder.beginTransaction();
    return new TransactionRecordingImpl(txn, txnRecorder);
  }
View Full Code Here

    recorder.beginTransaction();
    return new TransactionRecordingImpl(txn, txnRecorder);
  }

  public Transaction beginTransaction(TransactionOptions transactionOptions) {
    Transaction txn =  delegate.beginTransaction(transactionOptions);
    txnIdAnswer.setExpectedTxnId(txn.getId());
    recorder.beginTransaction(transactionOptions);
    return new TransactionRecordingImpl(txn, txnRecorder);
  }
View Full Code Here

TOP

Related Classes of com.google.appengine.api.datastore.Transaction

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.