@Override public void commit() { logger.finer("commit"); if (tx == null) { return; } tx.commit(); tx = null; logger.finer("done"); }
@Override public void commitTransaction() { datastore.getCurrentTransaction().commit(); }
public static void commit(Objectify objectify) { objectify.getTransaction().commit(); }
/** Commits this transaction with loose commitment protocol. */ @Override public void commit() throws IndoubtException { session.transaction.remove(); if (logs.size() == 0) { return; } try { for (Log log : logs) { if (log.operation() != Operation.GET) { if (transaction.isActive()) { transaction.commit(); } log.state(State.COMMITTED); } } } catch (Exception e) { throw new UncheckedException(e); } logger.fine("Transaction [" + id + "] committed"); }
void commit() { try { txn.commit(); } catch (IllegalArgumentException e) { throw wrapIllegalArgumentException(e); } catch (ConcurrentModificationException e) { throw new NucleusDataStoreException("Concurrent Modification", e); } catch (DatastoreFailureException e) { throw wrapDatastoreFailureException(e); } clear(); }
@Override public List<Key> call() { Transaction tx = dataStore.beginTransaction(); List<Key> keys; try { keys = dataStore.put(tx, shardedValues); tx.commit(); } finally { if (tx.isActive()) { tx.rollback(); } } return keys; } });
/** Allocates lock for the managing entity. */ @Override public void prepare() { List<Log> logs = global.logs(); Object parent = null; for (int i = logs.size() - 1; i >=0; i--) { if (logs.get(i).operation() != Log.Operation.GET) { parent = logs.get(i).entity(); break; } } Lock lock = new Lock(global.id(), KeyFactory.createKeyString( Keys.create(Translation.toKind(parent.getClass()), Entities.keyValue(parent)), Translation.TRANSACTION_KIND, global.id()), new Date()); datastore.put(local, Translation.toEntity(lock, Keys.create( Translation.toKind(entity.getClass()), Entities.keyValue(entity)))); local.commit(); this.lock = lock; }
private void apply(Log.Operation operation, Object entity, Key lock, List<Key> deletes) { Transaction transaction = datastore.beginTransaction(); if (datastore.get(transaction, lock) == null) { logger.info("Entity locked by [" + lock + "] has been applied by another transaction"); transaction.rollback(); return; } if (operation != Log.Operation.DELETE) { datastore.put(transaction, Translation.toEntities(entity)); } else { for (Entity e : Translation.toEntities(entity)) { deletes.add(e.getKey()); } } datastore.delete(transaction, deletes); transaction.commit(); }
public static Key putTempData(TempData data) { DatastoreService ds = DatastoreServiceFactory.getDatastoreService(); Transaction txn = ds.beginTransaction(TransactionOptions.Builder.withXG(true)); try { Class<? extends TempData> type = data.getClass(); String kind = getKind(type); Entity entity = new Entity(kind); for (Map.Entry<String, Object> entry : data.toProperties(ds).entrySet()) { entity.setProperty(entry.getKey(), entry.getValue()); } entity.setProperty(TEMP_DATA_READ_PROPERTY, false); data.prePut(ds); Key key = ds.put(txn, entity); data.postPut(ds); txn.commit(); return key; } catch (Exception e) { throw new IllegalStateException(e); } finally { if (txn.isActive()) { txn.rollback(); } } }
protected static void deleteTempDataInTx(DatastoreService ds, Entity entity, Class<? extends TempData> type) { Transaction txn = ds.beginTransaction(TransactionOptions.Builder.withXG(true)); try { TempData data = type.newInstance(); data.fromProperties(entity.getProperties()); data.preDelete(ds); ds.delete(txn, entity.getKey()); data.postDelete(ds); txn.commit(); } catch (Exception e) { throw new IllegalStateException(e); } finally { if (txn.isActive()) { txn.rollback(); } } }
@Override public boolean deleteObject(GcsFilename filename, long timeoutMillis) throws IOException { ensureInitialized(); Transaction tx = datastore.beginTransaction(); Key key = makeKey(filename); try { datastore.get(tx, key); datastore.delete(tx, key); blobstoreService.delete(getBlobKeyForFilename(filename)); } catch (EntityNotFoundException ex) { return false; } finally { if (tx.isActive()) { tx.commit(); } } return true; }
private void saveEntity(Entity entity) { DatastoreService service = DatastoreServiceFactory.getDatastoreService(); Transaction tx = service.beginTransaction(); try { service.put(tx, entity); tx.commit(); } finally { if (tx.isActive()) { tx.rollback(); } } }
@Test public void testXGTransaction() throws Exception { final int N = 25; // max XG entity groups List<Key> keys = new ArrayList<>(); for (int i = 0; i < N + 1; i++) { keys.add(service.put(new Entity("XG"))); } boolean ok = false; Transaction tx = service.beginTransaction(TransactionOptions.Builder.withXG(true)); try { for (int i = 0; i < N; i++) { service.get(keys.get(i)); } try { service.get(keys.get(N)); fail("Expected IllegalArgumentException"); } catch (IllegalArgumentException e) { // pass } ok = true; } finally { if (ok) { tx.commit(); } else { tx.rollback(); } } }
@Test public void testBasicTxPut() throws Exception { Entity entity = createTestEntity(TRANSACTION_TEST_ENTITY, System.currentTimeMillis()); Transaction tx = service.beginTransaction(); try { service.put(tx, entity); assertStoreDoesNotContain(entity); tx.commit(); assertStoreContains(entity); } catch (Exception e) { tx.rollback(); throw e; } }
@Test(expected = IllegalStateException.class) public void testClosedTx() throws InterruptedException { clearData(kindName); Transaction tx = service.beginTransaction(); Entity newRec = new Entity(kindName); newRec.setProperty("check", "4100331"); newRec.setProperty("stamp", new Date()); service.put(newRec); tx.commit(); service.put(tx, new Entity(kindName)); }
@Test public void testBasicTxDelete() throws Exception { Entity entity = createTestEntity(); service.put(entity); Transaction tx = service.beginTransaction(); try { service.delete(tx, entity.getKey()); assertStoreContains(entity); tx.commit(); assertStoreDoesNotContain(entity); } catch (Exception e) { tx.rollback(); throw e; } }
private GroupParentKeys writeMultipleGroup(boolean allow) throws Exception { GroupParentKeys keys = new GroupParentKeys(); TransactionOptions tos = TransactionOptions.Builder.withXG(allow); Transaction tx = service.beginTransaction(tos); try { Entity parent = new Entity(kindName); parent.setProperty("check", "parent"); parent.setProperty("stamp", new Date()); keys.firstParent = service.put(tx, parent); Entity other = new Entity(otherKind); other.setProperty("check", "other"); other.setProperty("stamp", new Date()); keys.secondParent = service.put(tx, other); tx.commit(); sync(sleepTime); } catch (Exception e) { tx.rollback(); throw e; } sync(sleepTime); return keys; }
@Test public void testOperatingOnClosedTransaction() throws Exception { Transaction tx = service.beginTransaction(); Entity entity = createTestEntity(); service.put(tx, entity); tx.commit(); try { service.get(tx, entity.getKey()); fail("Expected IllegalStateException"); } catch (IllegalStateException ok) { } try { service.delete(tx, entity.getKey()); fail("Expected IllegalStateException"); } catch (IllegalStateException ok) { } try { service.put(tx, entity); fail("Expected IllegalStateException"); } catch (IllegalStateException ok) { } }
@Test public void testSingleDefault() throws EntityNotFoundException, InterruptedException { clearData(kindName); Transaction tx = service.beginTransaction(); Entity newRec = new Entity(kindName); newRec.setProperty("check", "4100331"); newRec.setProperty("step", "added"); Key key = service.put(tx, newRec); tx.commit(); Entity qRec = service.get(key); assertEquals("4100331", qRec.getProperty("check")); tx = service.beginTransaction(); qRec = service.get(key); qRec.setUnindexedProperty("step", "update"); service.put(tx, newRec); tx.rollback(); qRec = service.get(key); assertEquals("added", qRec.getProperty("step")); }