今天项目刚灰度,就发现线上异常:

android.database.sqlite.SQLiteDatabaseLockedException: database is locked (Sqlite code 5), (OS error - 11:Try again)

google了一下这个异常也没有好的解决方案,都说保证单例访问就可以,查看我的代码没有毛病,都是加了锁的,这就见鬼了

只能sqlite翻看源码

从异常上可以看出最后调用nativeExecuteForChangedRowCount方法,找到源码看看android_database_SQLiteConnection.cpp

static jint nativeExecuteForChangedRowCount(JNIEnv* env, jclass clazz,jlong connectionPtr, jlong statementPtr) {SQLiteConnection* connection = reinterpret_cast<SQLiteConnection*>(connectionPtr);sqlite3_stmt* statement = reinterpret_cast<sqlite3_stmt*>(statementPtr);// 会调用executeNonQuery int err = executeNonQuery(env, connection, statement);return err == SQLITE_DONE ? sqlite3_changes(connection->db) : -1;
}

接下来看executeNonQuery代码

static int executeNonQuery(JNIEnv* env, SQLiteConnection* connection, sqlite3_stmt* statement) {int err = sqlite3_step(statement);if (err == SQLITE_ROW) {throw_sqlite3_exception(env,"Queries can be performed using SQLiteDatabase query or rawQuery methods only.");} else if (err != SQLITE_DONE) {throw_sqlite3_exception(env, connection->db);}return err;
}

executeNonQuery 代码会调用throw_sqlite3_exception抛出异常, 找到throw_sqlite3_exception实现throw_sqlite3_exception

void throw_sqlite3_exception(JNIEnv* env, int errcode,const char* sqlite3Message, const char* message) {const char* exceptionClass;switch (errcode) {case SQLITE_IOERR:exceptionClass = "android/database/sqlite/SQLiteDiskIOException";break;case SQLITE_CORRUPT:case SQLITE_NOTADB: // treat "unsupported file format" error as corruption alsoexceptionClass = "android/database/sqlite/SQLiteDatabaseCorruptException";break;case SQLITE_CONSTRAINT:exceptionClass = "android/database/sqlite/SQLiteConstraintException";break;case SQLITE_ABORT:exceptionClass = "android/database/sqlite/SQLiteAbortException";break;case SQLITE_DONE:exceptionClass = "android/database/sqlite/SQLiteDoneException";break;case SQLITE_FULL:exceptionClass = "android/database/sqlite/SQLiteFullException";break;case SQLITE_MISUSE:exceptionClass = "android/database/sqlite/SQLiteMisuseException";break;case SQLITE_PERM:exceptionClass = "android/database/sqlite/SQLiteAccessPermException";break;case SQLITE_BUSY:// 这个就是抛出JAVA数据库锁的异常exceptionClass = "android/database/sqlite/SQLiteDatabaseLockedException";break;case SQLITE_LOCKED:exceptionClass = "android/database/sqlite/SQLiteTableLockedException";break;case SQLITE_READONLY:exceptionClass = "android/database/sqlite/SQLiteReadOnlyDatabaseException";break;case SQLITE_CANTOPEN:exceptionClass = "android/database/sqlite/SQLiteCantOpenDatabaseException";break;case SQLITE_TOOBIG:exceptionClass = "android/database/sqlite/SQLiteBlobTooBigException";break;case SQLITE_RANGE:exceptionClass = "android/database/sqlite/SQLiteBindOrColumnIndexOutOfRangeException";break;case SQLITE_NOMEM:exceptionClass = "android/database/sqlite/SQLiteOutOfMemoryException";break;case SQLITE_MISMATCH:exceptionClass = "android/database/sqlite/SQLiteDatatypeMismatchException";break;case SQLITE_INTERRUPT:exceptionClass = "android/content/OperationCanceledException";break;default:exceptionClass = "android/database/sqlite/SQLiteException";break;}

看到SQLITE_BUSY就是我们对应的JAVA 异常 SQLITE_BUSY什么意思呢,google 一下没找到,只能去官网上看了。

SQLITE_BUSY

官方解析如下

The SQLITE_BUSY result code indicates that the database file could not be written (or in some cases read) because of concurrent activity by some other database connection, usually a database connection in a separate process.

翻译过来就是: SQLITE_BUSY 状态表示当前数据库文件不能写入,因为被其他进程占用

For example, if process A is in the middle of a large write transaction and at the same time process B attempts to start a new write transaction, process B will get back an SQLITE_BUSY result because SQLite only supports one writer at a time. Process B will need to wait for process A to finish its transaction before starting a new transaction. The sqlite3_busy_timeout() and sqlite3_busy_handler() interfaces and the busy_timeout pragma are available to process B to help it deal with SQLITE_BUSY errors.

An SQLITE_BUSY error can occur at any point in a transaction: when the transaction is first started, during any write or update operations, or when the transaction commits. To avoid encountering SQLITE_BUSY errors in the middle of a transaction, the application can use BEGIN IMMEDIATE instead of just BEGIN to start a transaction. The BEGIN IMMEDIATE command might itself return SQLITE_BUSY, but if it succeeds, then SQLite guarantees that no subsequent operations on the same database through the next COMMIT will return SQLITE_BUSY.

所以从JAVA层处理该异常可以 try 一下。或则稍后重试。

SQLiteDatabaseLockedException: database is locked相关推荐

  1. golang sqlite数据库 rows.Close()造成 错误database is locked

    在最近的一个程序中,使用的是sqlite数据库.涉及到多线程对数据库的读写.因为sqlite本身有五个锁状态:unlocked,shared,reserved,pending,exclusive.每个 ...

  2. sqlite database is locked 问题解决方案

    这两天在项目中用大强度大频率的方法测试时遇到sqlite报database is locked的问题, 分析下来原因是sqlite对数据库做修改操作时会做(文件)锁使得其它进程同一时间使用时会报该错误 ...

  3. sqlite3.OperationalError: database is locked

    情景描述 windows上代码文件,设置为共享目录,mount到linux使用.运行python manage.py migrate,出现错误 报错 [root@localhost sqlalchem ...

  4. 使用FMDB多线程訪问数据库,及database is locked的问题

    今天最终攻克了多线程同一时候訪问数据库时,报数据库锁定的问题.错误信息是: Unknown error finalizing or resetting statement (5: database i ...

  5. python database is locked_sqlite遇到database is locked问题的完美解决

    这两天在项目中用大强度大频率的方法测试时遇到sqlite报database is locked的问题, 分析下来原因是sqlite对数据库做修改操作时会做(文件)锁使得其它进程同一时间使用时会报该错误 ...

  6. svn: E200033: database is locked, executing statement 'RELEASE   s0' 问题解决办法

    前几天svn迁移到其他路径之后,今天早上更新代码时,出现了下面的问题: svn: E200033: database is locked, executing statement 'RELEASE   ...

  7. hue集成mysql报错_hue集成hive访问报database is locked

    这个问题这应该是hue默认的SQLite数据库出现错误,你可以使用mysql postgresql等来替换 hue默认使用sqlite作为元数据库,不推荐在生产环境中使用.会经常出现database ...

  8. sqlite遇到database is locked问题的完美解决

    这两天在项目中用大强度大频率的方法测试时遇到sqlite报database is locked的问题, 分析下来原因是sqlite对数据库做修改操作时会做(文件)锁使得其它进程同一时间使用时会报该错误 ...

  9. dpkg: error: dpkg status database is locked by another process

    解压报错 : root@ubuntu:~# dpkg -i zabbix-release_4.2-2+xenial_all.deb dpkg: error: dpkg status database ...

  10. dpkg: error: dpkg status database is locked by another process 解决方法

    dpkg: error: dpkg status database is locked by another process 解决方法 参考文章: (1)dpkg: error: dpkg statu ...

最新文章

  1. matlab图像处理命令(一)
  2. python爬虫案例-python爬虫案例
  3. python读取文件参数_Python 通过配置文件 读取参数进行
  4. leetcode696. 计数二进制子串
  5. 信息学奥赛一本通 提高篇 第5章 矩阵乘法
  6. 从零开始学习OpenWrt完美教程-转
  7. 北理工慕课 嵩天 Python零基础入门 笔记整理
  8. 关于 HSF框架 (一)简单介绍
  9. 程序员背锅救星-docker
  10. 生命在于答疑——git推送本地到库鉴权失败
  11. 学python之前先掌握-成为黑客前,你首先应该掌握的Python技术
  12. Isolation Forest算法原理详解
  13. 基于skynet设计游戏服务端框架
  14. 文字,字体,图像,列表
  15. 区块链共识算法及应用研究
  16. 【学术期刊】2023CCF推荐的A,B,C类英文科技期刊目录最新发布
  17. k8s笔记0--k8s资料大全
  18. 安卓Android Studio Button按钮background不生效无效问题
  19. 《zw版·Halcon-delphi系列原创教程》 zw版-Halcon常用函数Top100中文速查手册
  20. 算法大师孙膑--田忌赛马(转自labuladong)

热门文章

  1. 摄影测量——单片空间后方交会
  2. 映美FP-530K+打印发票的各种经验
  3. python画球面投影_Python中球面投影图像的高程畸变
  4. 使用Python-Flask框架开发Web网站系列课程(四)构建前端
  5. Android,java敏感词,QQ,微信自动过滤组件
  6. Vue 脚手架 环境搭配
  7. PHP Captcha实现图片验证码生成及识别(附源码)
  8. AtCoder Grand Contest 007题解
  9. ROI是什么?电商ROI计算公式及理论及详解
  10. Android开发:为什么你的学习效率如此低,为什么你很迷茫?