gpt4 book ai didi

Android - 何时调用 db.setTransactionSuccessful()?

转载 作者:IT王子 更新时间:2023-10-29 06:20:06 27 4
gpt4 key购买 nike

我见过的 Android SQLite 事务代码示例似乎会在 db.endTransaction() 之前自动调用 db.setTransactionSuccessful()

我想知道这是否真的是最佳实践,或者在调用 db.setTransactionSuccessful() 之前是否应该进行一些条件检查。

在我的例子中,我重写了 ContentProviderbulkInsert() 方法,如果我使用描述的条件检查,我的方法将如下所示......

    @Override
public int bulkInsert(Uri uri, ContentValues[] valuesArray) {

// Open a read / write database to support the transaction.
SQLiteDatabase db = dbHelper.getWritableDatabase();

switch (uriMatcher.match(uri)) {
case BRANDS_SEARCH:

int numInserts = 0;

db.beginTransaction();

for (int i = 0; i < valuesArray.length; i++) {

// Insert the values into the table
long rowId = db.insert(BRAND_NAMES_TABLE, null, valuesArray[i]);

if (rowId > -1) {

// Increment numInserts
numInserts++;

// Construct the URI of the newly inserted row.
Uri insertedId = ContentUris.withAppendedId(CONTENT_URI_BRANDS, rowId);

// Notify any observers of the change in the data set.
getContext().getContentResolver().notifyChange(insertedId, null);

}

}

boolean allInsertAttemptsWereSuccessful = (numInserts == valuesArray.length);

if (allInsertAttemptsWereSuccessful) {
db.setTransactionSuccessful(); //todo - should this be conditional?
}
else {
//todo - ???
}

db.endTransaction();

return numInserts;

default:
//break;
throw new IllegalArgumentException("Unsupported URI: " + uri);
}
}

...这是正确的方法吗?

如果 allInsertAttemptsWereSuccessful == false 我应该采取什么行动??

(我查看了 Android docs ,但提供的信息很少。)

更新 - 新代码...

感谢laalto's answer ,这是我的新(正确)代码...

/**
* Attempts a bulk insert. Outcome will either be all inserts succeeded
* or all inserts failed.
*/
@Override
public int bulkInsert(Uri uri, ContentValues[] valuesArray) {

/*
* Open a read / write database to support the transaction.
*/
SQLiteDatabase db = dbHelper.getWritableDatabase();

switch (uriMatcher.match(uri)) {
case BRANDS_SEARCH:

/*
* Begin the transaction
*/
db.beginTransaction();

try {

for (int i = 0; i < valuesArray.length; i++) {

/*
* Insert the values into the table
*/
long rowId = db.insert(BRAND_NAMES_TABLE, null, valuesArray[i]);

if (rowId > -1) {

/*
* Construct the URI of the newly inserted row.
*/
Uri insertedId = ContentUris.withAppendedId(CONTENT_URI_BRANDS, rowId);

/*
* Notify any observers of the change in the data set.
*/
getContext().getContentResolver().notifyChange(insertedId, null);

}
else {

/*
* Give up (as we need all insert attempts to succeed)
*/
throw new Exception("Could not insert row");
}

}

/*
* All insert attempts succeeded
*/
db.setTransactionSuccessful();

return valuesArray.length;
}
catch(Exception e) {

/*
* If any insert attempt failed, then setTransactionSuccessful() will not be called so no rows will actually be inserted
*/
return 0;

}
finally {

/*
* Always end the transaction
*/
db.endTransaction();
}

default:
//break;
throw new IllegalArgumentException("Unsupported URI: " + uri);
}
}

最佳答案

交易的规范模式:

beginTransaction();
try {
//db operations ...

setTransactionSuccessful();
} finally {
endTransaction();
}

这确保了 endTransaction() 总是被调用(没有留下悬空事务),并且当某些数据库操作发生异常时事务被回滚。如果您出于自己的原因想要中止交易,请不要调用 setTransactionSuccessful() 或抛出异常。

关于Android - 何时调用 db.setTransactionSuccessful()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26756072/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com