【问题标题】:What happend when method with transaction invokes another method with transaction in sqlite?当带有事务的方法在sqlite中调用另一个带有事务的方法时会发生什么?
【发布时间】:2013-10-30 16:23:28
【问题描述】:

我在 android 中有一个应用程序,它使用 sqlite 来持久化数据。

如果您从启动另一个事务的其他方法(如示例)调用启动事务的方法是否正确?这两笔交易发生了什么?

@Override
public void saveSurvApplied(MSurvey surv, MSApplied sa) {
    try {
        db.beginTransaction();
        SurveyManager surveyManager = ManagerFactory.getSurveyManager(ctx);
        surveyManager.saveSurveyState(surv); // it begins a transaction

        saDao.save(sa);
        db.setTransactionSuccessful();
    } catch (SQLException ex) {
        Log.e(tag, "Error insertando las encuestas aplicadas", ex);
    } finally {
        db.endTransaction();
    }
}

...

SurveyManagerImpl.java

@Override
public void saveSurveyState(MSurvey survey) {
    try {
        db.beginTransaction();

        for (MNode node : survey.getNodes()) {
            nodeDao.update(node);
            MItem[] items = node.getItems();
            if (items != null) {
                for (MItem item : items) {
                    itemDao.update(item);
                }
            }
        }
        surveyDao.update(survey);
        db.setTransactionSuccessful();
    } catch (SQLException ex) {
        Log.e(tag, "Error actualizando la encuesta", ex);
    } finally {
        db.endTransaction();
    }
}

我可以提供一些参考来了解在 sqlite 中调用嵌套事务时会发生什么吗? 谢谢。

【问题讨论】:

    标签: java android sqlite transactions


    【解决方案1】:

    documentation 说:

    事务可以嵌套。当外部事务结束时,该事务中完成的所有工作以及所有嵌套事务都将被提交或回滚。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-02-19
      • 2021-02-28
      • 2012-12-24
      • 2011-07-03
      • 1970-01-01
      • 2015-11-13
      • 2010-11-20
      相关资源
      最近更新 更多