Tom*_*try 6 sqlite android locking
我有一个应用程序在一个单独的进程中运行带有服务的活动,该进程已启动并绑定到该活动.该服务包含一个处理程序,用于发布延迟后要运行的runnable.
我希望每个组件都能登录到数据库,因此我实现了一个处理数据库访问的内容提供程序,我通过扩展的AsyncTask子类从服务或活动中调用它.
这一切精美的作品在模拟器上,但是当我在调试我的手机上运行它,我得到我的数据库写零星的数据库锁定错误:
UPDATE
我对数据库处理做了一些更改,错误略有改变.
ERROR/Database(15235): Error inserting MY_MESSAGE
ERROR/Database(15235): android.database.sqlite.SQLiteException: error code 5: database is locked
ERROR/Database(15235): at android.database.sqlite.SQLiteStatement.native_execute(Native Method)
ERROR/Database(15235): at android.database.sqlite.SQLiteStatement.execute(SQLiteStatement.java:61)
ERROR/Database(15235): at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1591)
ERROR/Database(15235): at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1435)
ERROR/Database(15235): at mypackagename.DatabaseHelper.insertLogging(DatabaseHelper.java:190)
ERROR/Database(15235): at mypackagename.ContentProvider.insert(ContentProvider.java:139)
ERROR/Database(15235): at android.content.ContentProvider$Transport.insert(ContentProvider.java:198)
ERROR/Database(15235): at android.content.ContentResolver.insert(ContentResolver.java:604)
ERROR/Database(15235): at mypackagename.Activity$LogToDatabase.doInBackground(Activity.java:642)
ERROR/Database(15235): at mypackagename.Activity$LogToDatabase.doInBackground(Activity.java:1)
ERROR/Database(15235): at android.os.AsyncTask$2.call(AsyncTask.java:185)
ERROR/Database(15235): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
ERROR/Database(15235): at java.util.concurrent.FutureTask.run(FutureTask.java:138)
ERROR/Database(15235): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
ERROR/Database(15235): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
ERROR/Database(15235): at java.lang.Thread.run(Thread.java:1019)
Run Code Online (Sandbox Code Playgroud)
之前我没有提供太多细节,因为我认为这是一个不同进程或线程的问题,但现在我认为问题更可能位于调用数据库的代码中.
问题:
1)当我使用ContentProvider时,为什么我会敲锁?
2)为什么这不会出现在等效的API 2.3.3仿真器上?
3)我的代码没有捕获异常的事实是否意味着错误处理得当且我可以忽略它?
4)我在另一个地方读到有人建议调整忙碌超时.我该怎么办?
具有讽刺意味的是,我的调试日志记录导致错误并没有丢失.
如果我无法解决它,我的下一步是将日志消息捆绑在一个列表中,并一次一批地将它们转储出来.
这是通过代码到错误的路径:
活动:
private void logDatabaseMessage(String status, String message)
{
String[] args = {status, message};
LogToDatabase logTask = new LogToDatabase();
logTask.execute(args);
}
private class LogToDatabase extends AsyncTask<String, Integer, Void>
{
@Override
protected Void doInBackground(final String... args)
{
try
{
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
String dateText = dateFormat.format(new Date());
ContentValues loggingValues = new ContentValues();
loggingValues.put(MyContentProvider.LOGGING_DATETIME, dateText);
loggingValues.put(MyContentProvider.LOGGING_STATUS, args[0]);
loggingValues.put(MyContentProvider.LOGGING_MESSAGE, args[1]);
getContentResolver().insert(MyContentProvider.LOGGING_CONTENT_URI, loggingValues);
}
catch (Exception ex)
{
Log.e(TAG, "LogToDatabase.doInBackground threw exception: " + ex.getMessage());
ex.printStackTrace();
}
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
内容提供商:
@Override
public Uri insert(Uri uri, ContentValues values)
{
Uri _uri = null;
long rowID = 0;
try
{
switch (uriMatcher.match(uri))
{
case LOGGING:
rowID = dbHelper.insertLogging(values);
if (rowID == 0)
throw new SQLException("Failed to insert row into " + uri);
_uri = ContentUris.withAppendedId(LOGGING_CONTENT_URI, rowID);
break;
default: throw new SQLException("Failed to insert row into " + uri);
}
if (rowID != 0)
getContext().getContentResolver().notifyChange(_uri, null);
}
catch (Exception ex)
{
Log.e(TAG, LogPrefix + "insert threw exception: " + ex.getMessage());
ex.printStackTrace();
}
return _uri;
}
Run Code Online (Sandbox Code Playgroud)
DatabaseHelper:
public long insertLogging(ContentValues values)
{
long rowID = 0;
try
{
rowID = db.insert(LOGGING_TABLE, null, values);
}
catch (Exception ex)
{
Log.e(TAG, LogPrefix + "ERROR: Failed to insert into logging table: " + ex.getMessage());
ex.printStackTrace();
}
return rowID;
}
Run Code Online (Sandbox Code Playgroud)
dha*_*g23 14
您是否可能使用多个SQLiteDatabase(或可能是SQLiteOpenHelper)实例访问数据库?
您只能与数据库建立一个连接,否则您将收到您遇到的错误.
SQLiteDatabase本身是线程安全的,因此您可以同时访问它.
| 归档时间: |
|
| 查看次数: |
20294 次 |
| 最近记录: |