我有一个应用程序在一个单独的进程中运行带有服务的活动,该进程已启动并绑定到该活动.该服务包含一个处理程序,用于发布延迟后要运行的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)
之前我没有提供太多细节,因为我认为这是一个不同进程或线程的问题,但现在我认为问题更可能位于调用数据库的代码中.
问题:
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; } }
内容提供商:
@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; }
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; }