tx.commit(); } finally { if (tx.isActive()) { tx.rollback();
@Override public boolean isActive() { return this.raw.isActive(); }
@Override public boolean isActive() { return transaction.isActive(); }
@Override public T call() throws DatastoreException { transaction = datastore.newTransaction(options); try { T value = callable.run(transaction); transaction.commit(); return value; } catch (Exception ex) { transaction.rollback(); throw DatastoreException.propagateUserException(ex); } finally { if (transaction.isActive()) { transaction.rollback(); } if (options != null && options.getModeCase().equals(TransactionOptions.ModeCase.READ_WRITE)) { setPrevTransactionId(transaction.getTransactionId()); } } } }
/** Example of verifying if a transaction is active. */ // [TARGET active()] public Key active() { Datastore datastore = transaction.getDatastore(); // [START active] // create an entity KeyFactory keyFactory = datastore.newKeyFactory().setKind("MyKind"); Key key = datastore.allocateId(keyFactory.newKey()); Entity entity = Entity.newBuilder(key).set("description", "active()").build(); // calling transaction.active() now would return true try { // add the entity and commit transaction.put(entity); transaction.commit(); } finally { // if committing succeeded // then transaction.isActive() will be false if (transaction.isActive()) { // otherwise it's true and we need to rollback transaction.rollback(); } } // [END active] return key; }
/** Example of verifying if a transaction is active. */ // [TARGET isActive()] public Key isActive() { Datastore datastore = transaction.getDatastore(); // [START isActive] // create an entity KeyFactory keyFactory = datastore.newKeyFactory().setKind("MyKind"); Key key = datastore.allocateId(keyFactory.newKey()); Entity entity = Entity.newBuilder(key).set("description", "active()").build(); // calling transaction.active() now would return true try { // add the entity and commit transaction.put(entity); transaction.commit(); } finally { // if committing succeeded // then transaction.active() will be false if (transaction.isActive()) { // otherwise it's true and we need to rollback transaction.rollback(); } } // [END isActive] return key; } }
@Test public void testNewTransactionCommit() { Transaction transaction = DATASTORE.newTransaction(); transaction.add(ENTITY3); Entity entity2 = Entity.newBuilder(ENTITY2).clear().setNull("bla").build(); transaction.update(entity2); transaction.delete(KEY1); transaction.commit(); assertFalse(transaction.isActive()); List<Entity> list = DATASTORE.fetch(KEY1, KEY2, KEY3); assertNull(list.get(0)); assertEquals(entity2, list.get(1)); assertEquals(ENTITY3, list.get(2)); assertEquals(3, list.size()); try { transaction.commit(); fail("Expecting a failure"); } catch (DatastoreException expected) { assertEquals("FAILED_PRECONDITION", expected.getReason()); } try { transaction.rollback(); fail("Expecting a failure"); } catch (DatastoreException expected) { assertEquals("FAILED_PRECONDITION", expected.getReason()); } }
/** * @see Transaction#isActive() */ boolean isActive() { return tx.isActive(); }
@Override public boolean isActive() { return this.transaction.isActive(); }
@Override public boolean isActive() { return this.transaction.isActive(); }
/** * Marks a task entity as done. * * @param id The ID of the task entity as given by {@link Key#id()} * @return true if the task was found, false if not * @throws DatastoreException if the transaction fails */ boolean markDone(long id) { Transaction transaction = datastore.newTransaction(); try { Entity task = transaction.get(keyFactory.newKey(id)); if (task != null) { transaction.put(Entity.newBuilder(task).set("done", true).build()); } transaction.commit(); return task != null; } finally { if (transaction.isActive()) { transaction.rollback(); } } } // [END datastore_update_entity]
@Override public boolean isActive() { return transaction().isActive(); } };
@Override protected Object doGetTransaction() throws TransactionException { Tx tx = getCurrentTX(); if (tx != null && tx.transaction != null && tx.transaction.isActive()) { return tx; } tx = new Tx(); return tx; }
@Override protected Object doGetTransaction() throws TransactionException { Tx tx = getCurrentTX(); if (tx != null && tx.transaction != null && tx.transaction.isActive()) { return tx; } tx = new Tx(); return tx; }
@Override public boolean isActive() { try { return nativeTransaction.isActive(); } catch (DatastoreException exp) { throw DatastoreUtils.wrap(exp); } catch (Exception exp) { throw new EntityManagerException(exp); } }
/** * Rolls back the given transaction, if it is still active. * * @param transaction * the transaction to roll back. */ static void rollbackIfActive(Transaction transaction) { try { if (transaction != null && transaction.isActive()) { transaction.rollback(); } } catch (DatastoreException exp) { throw new EntityManagerException(exp); } }
@Override protected void doCommit(DefaultTransactionStatus defaultTransactionStatus) throws TransactionException { Tx tx = (Tx) defaultTransactionStatus.getTransaction(); try { if (tx.transaction.isActive()) { tx.transaction.commit(); } else { this.logger.debug( "Transaction was not committed because it is no longer active."); } } catch (DatastoreException ex) { throw new TransactionSystemException( "Cloud Datastore transaction failed to commit.", ex); } }
@Override protected void doRollback(DefaultTransactionStatus defaultTransactionStatus) throws TransactionException { Tx tx = (Tx) defaultTransactionStatus.getTransaction(); try { if (tx.transaction.isActive()) { tx.transaction.rollback(); } else { this.logger.debug( "Transaction was not rolled back because it is no longer active."); } } catch (DatastoreException ex) { throw new TransactionSystemException( "Cloud Datastore transaction failed to rollback.", ex); } }
@Override protected void doCommit(DefaultTransactionStatus defaultTransactionStatus) throws TransactionException { Tx tx = (Tx) defaultTransactionStatus.getTransaction(); try { if (tx.transaction.isActive()) { tx.transaction.commit(); } else { this.logger.debug( "Transaction was not committed because it is no longer active."); } } catch (DatastoreException ex) { throw new TransactionSystemException( "Cloud Datastore transaction failed to commit.", ex); } }
@Override protected void doRollback(DefaultTransactionStatus defaultTransactionStatus) throws TransactionException { Tx tx = (Tx) defaultTransactionStatus.getTransaction(); try { if (tx.transaction.isActive()) { tx.transaction.rollback(); } else { this.logger.debug( "Transaction was not rolled back because it is no longer active."); } } catch (DatastoreException ex) { throw new TransactionSystemException( "Cloud Datastore transaction failed to rollback.", ex); } }