@Override public TransactionDriver beginX() { logger.finer("begin X"); if (!environment.isProduction()) { return this; } TransactionOptions options = TransactionOptions.Builder.withXG(true); tx = datastore().beginTransaction(options); logger.finer("done"); return this; }
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String entityGroup = req.getParameter("eg"); String counter = req.getParameter("c"); String parent = req.getParameter("p"); boolean xg = Boolean.parseBoolean(req.getParameter("xg")); Key parentKey = "2".equals(parent) ? ROOT_2.getKey() : ROOT_1.getKey(); Entity entity = new Entity(entityGroup, parentKey); entity.setProperty("foo", RANDOM.nextInt()); DatastoreService ds = DatastoreServiceFactory.getDatastoreService(); final Transaction tx = ds.beginTransaction(TransactionOptions.Builder.withXG(xg)); try { log.warning("Before put ... " + counter); putEntity(ds, entity); log.warning("After put ... " + counter); tx.commit(); resp.getWriter().write("OK" + counter); } catch (Exception e) { log.warning("Error ... " + e); tx.rollback(); resp.getWriter().write("ERROR" + counter + ":" + e.getClass().getName()); error(counter); } finally { cleanup(counter); } }
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(); } } }
clearData(kindName); List<Entity> es = new ArrayList<>(); TransactionOptions tos = TransactionOptions.Builder.withXG(true); Transaction tx = service.beginTransaction(tos);
final AsyncDatastoreService service = DatastoreServiceFactory.getAsyncDatastoreService(); Transaction tx = waitOnFuture(service.beginTransaction(TransactionOptions.Builder.withXG(true))); Key key, key2; try { tx = waitOnFuture(service.beginTransaction(TransactionOptions.Builder.withXG(true))); try { try {
private GroupParentKeys writeMultipleInList(boolean allow) throws Exception { GroupParentKeys keys = new GroupParentKeys(); List<Entity> es = new ArrayList<>(); 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()); es.add(parent); keys.firstParent = parent.getKey(); Entity other = new Entity(otherKind); other.setProperty("check", "other"); other.setProperty("stamp", new Date()); es.add(other); keys.secondParent = other.getKey(); service.put(tx, es); tx.commit(); sync(sleepTime); } catch (Exception e) { tx.rollback(); throw e; } sync(sleepTime); return keys; }
try { clearData(kindName); TransactionOptions tos = TransactionOptions.Builder.withXG(false); Transaction tx = service.beginTransaction(tos); try {
@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(); } } }
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 testTransactionRollback() throws Exception { clearData(kindName); clearData(otherKind); GroupParentKeys keys = writeMultipleGroup(true); List<Entity> es = readMultipleGroup(keys); TransactionOptions tos = TransactionOptions.Builder.withXG(true); Transaction tx = service.beginTransaction(tos); es.get(0).setProperty("check", "parent-update"); es.get(1).setProperty("check", "other-update"); service.put(tx, es); tx.rollback(); es = readMultipleGroup(keys); assertEquals("parent", es.get(0).getProperty("check")); assertEquals("other", es.get(1).getProperty("check")); }
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(); } } }
/** * Sets whether or not the Objectify instance will start a transaction. If * true, the instance will hold a transaction that must be rolled back or * committed. Uses XG transactions when on the HRD; {@code setBeginTransaction(true)} is equivalent * to {@code setTransactionOptions(TransactionOptions.Builder.withXG(true))}. * There is no overhead for XG transactions on a single entity group, so there is * no good reason to ever have this false. */ public ObjectifyOpts setBeginTransaction(boolean value) { this.txnOpts = value ? TransactionOptions.Builder.withXG(DatastoreIntrospector.SUPPORTS_XG) : null; return this; }
@Test public void testTransactionOptions() { TransactionOptions tos = TransactionOptions.Builder.withXG(true); assertEquals(true, tos.isXG()); tos.clearXG(); assertEquals(false, tos.isXG()); }
@Override public void beginTransaction() { datastore.beginTransaction(TransactionOptions.Builder.withXG(true)); }
/** * Begins transaction and returns it. * If the current application is using High Replication Datastore, this * method returns XG (Cross-Group) transaction. * * @return Current transaction. */ public Transaction beginTransaction() { if (datastoreType == DatastoreType.HIGH_REPLICATION) { return datastore.beginTransaction(TransactionOptions.Builder.withXG(true)); } return datastore.beginTransaction(); }