本文整理汇总了Java中com.googlecode.objectify.ObjectifyService.beginTransaction方法的典型用法代码示例。如果您正苦于以下问题:Java ObjectifyService.beginTransaction方法的具体用法?Java ObjectifyService.beginTransaction怎么用?Java ObjectifyService.beginTransaction使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.googlecode.objectify.ObjectifyService
的用法示例。
在下文中一共展示了ObjectifyService.beginTransaction方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: addTrainNode
import com.googlecode.objectify.ObjectifyService; //导入方法依赖的package包/类
public synchronized Key<TrainNode> addTrainNode(TrainNode tn) {
String functionName = "addTrainNode()";
Key<TrainNode> resKey = null;
Objectify ofy = ObjectifyService.beginTransaction();
try {
Query<TrainNode> q = ofy.query(TrainNode.class).filter("uniqueName", tn.getUniqueName()).ancestor(Dao.getRootEntityTrain());
List<TrainNode> samestation = q.list();
if(samestation.size() > 0) {
if(Utils.distanceBetweenGeoCells(samestation.get(0).getGeoCell(), tn.getGeoCell()) <= 10) {
tn.setGeoCell(samestation.get(0).getGeoCell());
Logger.getLogger(location).log(Level.WARNING, functionName + ": joined/moved station " + tn.getUniqueName() + " for " + tn.getLineKey());
} else {
Logger.getLogger(location).log(Level.SEVERE, functionName + ": did not move station " + tn.getUniqueName() + " for " + tn.getLineKey() + " because distance is too big: " + Utils.distanceBetweenGeoCells(samestation.get(0).getGeoCell(), tn.getGeoCell()));
}
}
resKey = ofy.put(tn);
ofy.getTxn().commit();
} finally {
if (ofy.getTxn().isActive()) {
ofy.getTxn().rollback();
removeLine(Dao.getInstance().getLineByKey(tn.getLineKey(), ObjectifyService.begin()), ObjectifyService.begin());
Logger.getLogger(location).log(Level.SEVERE, functionName + ": Transaction failed for Line: " + tn.getLineKey());
}
}
return resKey;
}
示例2: runJobWithRetries
import com.googlecode.objectify.ObjectifyService; //导入方法依赖的package包/类
/**
* Call job.run() in a transaction and commit the transaction if no exceptions
* occur. If we get a {@link java.util.ConcurrentModificationException}
* or {@link com.google.appinventor.server.storage.ObjectifyException}
* we will retry the job (at most {@code MAX_JOB_RETRIES times}).
* Any other exception will cause the job to fail immediately.
* @param job
* @throws ObjectifyException
*/
@VisibleForTesting
void runJobWithRetries(JobRetryHelper job) throws ObjectifyException {
int tries = 0;
while (tries <= MAX_JOB_RETRIES) {
Objectify datastore = ObjectifyService.beginTransaction();
try {
job.run(datastore);
datastore.getTxn().commit();
break;
} catch (ConcurrentModificationException ex) {
job.onNonFatalError();
LOG.log(Level.WARNING, "Optimistic concurrency failure", ex);
} catch (ObjectifyException oe) {
// maybe this should be a fatal error? I think the only thing
// that creates this exception (other than this method) is uploadToBlobstore
job.onNonFatalError();
} finally {
if (datastore.getTxn().isActive()) {
try {
datastore.getTxn().rollback();
} catch (RuntimeException e) {
LOG.log(Level.WARNING, "Transaction rollback failed", e);
}
}
}
tries++;
}
if (tries > MAX_JOB_RETRIES) {
throw new ObjectifyException("Couldn't commit job after max retries.");
}
}
示例3: getOrCreateCounter
import com.googlecode.objectify.ObjectifyService; //导入方法依赖的package包/类
public static ShardedCounter getOrCreateCounter(String name, int numShards) {
ShardedCounter shardedCounter = new ShardedCounter(name);
Objectify trans = ObjectifyService.beginTransaction();
int tries = 3;
while (true) {
try {
EntityCounter counter = trans.find(new Key<EntityCounter>(
EntityCounter.class, name));
if (counter == null) {
// create new counter
counter = new EntityCounter(name);
trans.put(counter);
trans.getTxn().commit();
shardedCounter.addShards(numShards);
}
break;
} catch (ConcurrentModificationException e) {
if (tries == 0) {
throw e;
}
--tries;
} finally {
if (trans.getTxn().isActive()) {
trans.getTxn().rollback();
}
}
}
return shardedCounter;
}
示例4: createCounter
import com.googlecode.objectify.ObjectifyService; //导入方法依赖的package包/类
public static ShardedCounter createCounter(String name, int numShards) {
ShardedCounter shardedCounter = new ShardedCounter(name);
Objectify trans = ObjectifyService.beginTransaction();
int tries = 3;
while (true) {
try {
EntityCounter counter = trans.find(new Key<EntityCounter>(
EntityCounter.class, name));
if (counter == null) {
// create new counter
counter = new EntityCounter(name);
trans.put(counter);
trans.getTxn().commit();
shardedCounter.addShards(numShards);
} else {
throw new IllegalArgumentException("A counter with name " +
name + " does already exist!");
}
break;
} catch (ConcurrentModificationException e) {
if (tries == 0) {
throw e;
}
--tries;
} finally {
if (trans.getTxn().isActive()) {
trans.getTxn().rollback();
}
}
}
return shardedCounter;
}
示例5: updateTrainNode
import com.googlecode.objectify.ObjectifyService; //导入方法依赖的package包/类
public synchronized void updateTrainNode(TrainNode tn) {
String functionName = "updateTrainNode";
Objectify ofy = ObjectifyService.beginTransaction();
try {
ofy.put(tn);
ofy.getTxn().commit();
} finally {
if (ofy.getTxn().isActive()) {
ofy.getTxn().rollback();
removeLine(Dao.getInstance().getLineByKey(tn.getLineKey(), ObjectifyService.begin()), ObjectifyService.begin());
Logger.getLogger(location).log(Level.SEVERE, functionName + ": Transaction failed for Line: " + tn.getLineKey());
}
}
}
示例6: doUpgrade
import com.googlecode.objectify.ObjectifyService; //导入方法依赖的package包/类
public void doUpgrade(String userId) {
if (!conversionEnabled) // Unless conversion is enabled...
return; // shouldn't really ever happen but...
Objectify datastore = ObjectifyService.begin();
UserData userData = datastore.find(userKey(userId));
if ((userData.upgradedGCS && useGcs) ||
(!userData.upgradedGCS && !useGcs))
return; // All done, another task did it!
List<Long> projectIds = getProjects(userId);
boolean anyFailed = false;
for (long projectId : projectIds) {
for (FileData fd : datastore.query(FileData.class).ancestor(projectKey(projectId))) {
if (fd.isBlob) {
if (useGcs) { // Let's convert by just reading it!
downloadRawFile(userId, projectId, fd.fileName);
}
} else if (isTrue(fd.isGCS)) {
if (!useGcs) { // Let's downgrade by just reading it!
downloadRawFile(userId, projectId, fd.fileName);
}
}
}
}
/*
* If we are running low on time, we may have not moved all files
* so exit now without marking the user as having been finished
*/
if (ApiProxy.getCurrentEnvironment().getRemainingMillis() <= 5000)
return;
/* If anything failed, also return without marking user */
if (anyFailed)
return;
datastore = ObjectifyService.beginTransaction();
userData = datastore.find(userKey(userId));
userData.upgradedGCS = useGcs;
datastore.put(userData);
datastore.getTxn().commit();
}