2012-08-06 73 views
0

问题是:ds.put(employee)是否发生在事务中?或者外部事务是否被saveRecord(..)中的事务清除/重写?谷歌应用程序引擎数据存储上的嵌套事务3

  1. 一旦在for循环(假设i == 5)中的某个点处在行datastore.put(..)处引发错误,以前的放置将始发在同一行上是否会回滚?
  2. 那么在saveRecord(..)中发生的情况如何。我想那些不会被回滚。
 

    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService() 
    Transaction txn = datastore.beginTransaction(); 
    try { 
     for (int i=0; 1<10; i++) { 
      Key employeeKey = KeyFactory.createKey("Employee", "Joe"); 
      Entity employee = datastore.get(employeeKey); 
      employee.setProperty("vacationDays", 10); 

      datastore.put(employee); 

      Entity employeeRecord = createRecord("record", employeeKey); 
      saveRecord(employeeRecord); 
     } 
    txn.commit(); 
    } finally { 
     if (txn.isActive()) { 
      txn.rollback(); 
     } 
    } 

    public void saveRecord(Entity entity) { 
     datastore.beginTransaction(); 
     try { 
      // do some logic in here, delete activity and commit txn 
      datastore.put(entity); 
     } finally { 
     if (datastore.getCurrentTransaction().isActive()) { 
      datastore.getCurrentTransaction().rollback(); 
     } 
     } 
    } 

+0

是'ds.beginTransaction()'你的自定义代码吗?每次通话都会返回相同的交易还是新的交易? – 2012-08-06 15:48:42

+0

ds仅仅是对API DataStore对象的引用。 – honzajde 2012-08-06 18:02:54

+0

我编辑了过于模糊的原始示例...请回答最后一次。谢谢。 – honzajde 2012-08-08 12:36:08

回答

1

OK,我假设你正在使用低级别的数据存储API。请注意,getTransaction()不存在。我会假设你的意思是datastoreService.getCurrentTransaction()

DatastoreService.beginTransaction()将返回一个交易,该交易被视为同一线程上的当前交易,直到您再次拨打beginTransaction()。由于您在“外部”事务内的循环中调用beginTransaction(),因此它将打破“外部”代码:循环结束后ds.getCurrentTransaction()不会返回相同的事务。另外,put()隐式使用当前事务。现在,在

public void put(EventPlan eventPlan) { 
    Transaction txn = ds.beginTransaction(); 
    try { 
    for (final Activity activity : eventPlan.getActivities()) { 
     save(activity, getPlanKey(eventPlan)); // PUT 

     // IMPORTANT - also pass transaction and use it 
     // (I assume this is some internal helper method) 
     ds.put(txn, activity, getSubPlanKey(eventPlan)); //subplan's parent is eventPlan 
    } 
    txn.commit(); 
    } finally { 
    if (txn.isActive()) 
     txn.rollback(); 
    } 
} 

到的问题:

所以,首先你必须修复外码事务保存为shown in example

  1. 是的,因为所有的看跌期权,现在是同一事务的一部分(后你修正了代码),并且在错误的情况下你可以打电话给txn.rollback()。不,当然不是。它们是不同交易的一部分。

+0

我编辑了原来太含糊的示例......请回答上一次... – honzajde 2012-09-02 17:43:52

相关问题