Register A Callback To Handle SQLITE_BUSY Errors(译)
http://www.sqlite.org/c3ref/busy_handler.html留着自己看的。
Register A Callback To Handle SQLITE_BUSY Errors
注册一个回调函数处理SQLITE_BUSY错误
int sqlite3_busy_handler(sqlite3*, int(*)(void*,int), void*);
This routine sets a callback function that might be invoked whenever an attempt is made to open a database table that another thread or process has locked.
本函数的目的是设置一个回调函数,如果你试图去打开一个被别的线程和进程锁定的数据库表的时候,这个回调函数会被调用。
If the busy callback is NULL, then SQLITE_BUSY or SQLITE_IOERR_BLOCKED is returned immediately upon encountering the lock. If the busy callback is not NULL, then the callback might be invoked with two arguments.
如果回调函数的参数为NULL,那么SQlite系统会在遇到一个锁的时候马上返回SQLITE_BUSY或者SQLITE_IOERR_BLOCKED。如果这个回调函数的参数不为NULL,那么这个回调函数会被调用,它带有2个参数。
The first argument to the busy handler is a copy of the void* pointer which is the third argument to sqlite3_busy_handler(). The second argument to the busy handler callback is the number of times that the busy handler has been invoked for this locking event. If the busy callback returns 0, then no additional attempts are made to access the database and SQLITE_BUSY or SQLITE_IOERR_BLOCKED is returned. If the callback returns non-zero, then another attempt is made to open the database for reading and the cycle repeats.
回调函数中的第一个参数是sqlite3_busy_handler()中的第三个参数的一份拷贝,第二个参数是回调函数因为这个锁定事件而被调用的次数。如果回调函数返回0,那么SQlite系统就不会再尝试访问数据库,SQLITE_BUSY或者SQLITE_IOERR_BLOCKED会被马上返回。如果回调函数返回非0,SQlite系统会再试着访问数据库。
The presence of a busy handler does not guarantee that it will be invoked when there is lock contention. If SQLite determines that invoking the busy handler could result in a deadlock, it will go ahead and return SQLITE_BUSY or SQLITE_IOERR_BLOCKED instead of invoking the busy handler. Consider a scenario where one process is holding a read lock that it is trying to promote to a reserved lock and a second process is holding a reserved lock that it is trying to promote to an exclusive lock. The first process cannot proceed because it is blocked by the second and the second process cannot proceed because it is blocked by the first. If both processes invoke the busy handlers, neither will make any progress. Therefore, SQLite returns SQLITE_BUSY for the first process, hoping that this will induce the first process to release its read lock and allow the second process to proceed.
这个回调函数不能保证在任何遇到锁的情况下都会被调用。如果SQlite系统认为调用回调函数会导致死锁,那么它会直接返回SQLITE_BUSY或者SQLITE_IOERR_BLOCKED。假如,一个进程持有读锁并且想要试着提升为保留锁,另一个线程持有保留锁并且想要提升为排它锁,那么这种情况可能导致死锁,因为第一个线程在等待第二线程释放保留锁,而第二个线程也在等待第一个线程释放读锁。如果他们都调用回调函数的话,他们都不会获得任何进展。因此,SQLite会返回SQLITE_BUSY给第一个线程,它希望第一个线程能释放读锁,这样就能让第二个线程继续运行了。
The default busy callback is NULL.
默认的回调函数是NULL。
The SQLITE_BUSY error is converted to SQLITE_IOERR_BLOCKED when SQLite is in the middle of a large transaction where all the changes will not fit into the in-memory cache. SQLite will already hold a RESERVED lock on the database file, but it needs to promote this lock to EXCLUSIVE so that it can spill cache pages into the database file without harm to concurrent readers. If it is unable to promote the lock, then the in-memory cache will be left in an inconsistent state and so the error code is promoted from the relatively benign SQLITE_BUSY to the more severe SQLITE_IOERR_BLOCKED. This error code promotion forces an automatic rollback of the changes. See the CorruptionFollowingBusyError wiki page for a discussion of why this is important.
如果在大型事务中,改变不能存入内存缓冲区, SQLITE_BUSY错误码会变成SQLITE_IOERR_BLOCKED。还有一种情况,如果SQLite持有一个保留锁,为了不让一些读的事务出现数据的不一致,它必须将自己的锁提升为EXLUSIVE(排它)锁,这样才能将缓存页中的数据安全的写入数据库中。如果SQLite不能完成这种提升,内存缓冲区中的数据将会出现不一致,这是错误码会从相对温和的SQLITE_BUSY变成更严重的SQLITE_IOERR_BLOCKED。SQLITE_IOERR_BLOCKED会强迫这些改变的自动回滚。可以去CorruptionFollowingBusyErrorWiki页看看为什么这很重要。
There can only be a single busy handler defined for each database connection. Setting a new busy handler clears any previously set handler. Note that callingsqlite3_busy_timeout() will also set or clear the busy handler.
对于每一个数据库连接来说,只能注册一个回调函数,注册任何一个新的回调函数都会替换掉以前那个。注意:调用sqlite3_busy_timeout()也会注册或清除回调函数。
The busy callback should not take any actions which modify the database connection that invoked the busy handler. Any such actions result in undefined behavior.
A busy handler must not close the database connection or prepared statement that invoked the busy handler.
任何回调函数都不应该做出修改调用自己的数据库连接的行为,任何这种行为都会导致不可预知的情况。
回调函数必须不能关掉调用自己的数据库连接或者释放掉调用自己的准备语句。
Register A Callback To Handle SQLITE_BUSY Errors(译)的更多相关文章
- [Angular] Handle HTTP Errors in Angular with HttpErrorResponse interface
When communicating with some backend API, data travels over the network using the HTTP protocol. As ...
- [React] Handle React Suspense Errors with an Error Boundary
Error Boundaries are the way you handle errors with React, and Suspense embraces this completely. Le ...
- [译]Vulkan教程(02)概况
[译]Vulkan教程(02)概况 这是我翻译(https://vulkan-tutorial.com)上的Vulkan教程的第2篇. This chapter will start off with ...
- 【译】深入理解python3.4中Asyncio库与Node.js的异步IO机制
转载自http://xidui.github.io/2015/10/29/%E6%B7%B1%E5%85%A5%E7%90%86%E8%A7%A3python3-4-Asyncio%E5%BA%93% ...
- 【译】JavaScript Promise API
原文地址:JavaScript Promise API 在 JavaScript 中,同步的代码更容易书写和 debug,但是有时候出于性能考虑,我们会写一些异步的代码(代替同步代码).思考这样一个场 ...
- callback回调函数【转】
请给作者点赞--> 原文链接 什么是回调函数? 我们绕点远路来回答这个问题. 编程分为两类:系统编程(system programming)和应用编程(application programmi ...
- 【转载】回调函数(callback)是什么?
一个很形象的例子: 你到一个商店买东西,刚好你要的东西没有货,于是你在店员那里留下了你的电话,过了几天店里有货了,店员就打了你的电话,然后你接到电话后就到店里去取了货.在这个例子里,你的电话号码就叫回 ...
- callback回调函数理解 相当于this指针
1.callback函数在微软的官方手册中是这样定义callback函数的:“callback函数是由应用程序定义而由操作系统调用的函数”. 凡是由用户设计而却由windows系统调用的函数,统称 ...
- Laravel API Errors and Exceptions: How to Return Responses
Laravel API Errors and Exceptions: How to Return Responses February 13, 2019 API-based projects are ...
随机推荐
- SQL注入与安全防护---以PHP为例
一.什么是sql注入: 简单来说,当我们从前端的用户表单数据往后台数据库传输时,可能用户表单数据中的某些数据,会跟我们的后台发生“有机”反应,从而导致发生一些数据库的异常操作. 举个例子吧,以简单的用 ...
- 黄聪:C#程序中判断是否处在DEBUG调试状态或者RELEASE发布状态
习惯了用老方式(注释的方式)来对程序进行调试,不过昨天才发现这样调试存在很大的隐患:在工程发布的时候如果忘记把该注释的代码注释掉,而让这些调试信息随工程一起发布,如果是可见的调试信息倒好发现,如果不是 ...
- 黄聪:[C#]如何获取变量的名字,不是值,是名称。返回名字的字符串
找了好久,最后在国外的论坛找到了解决办法,直接贴代码吧. 方法一: public static class MemberInfoGetting { public static string GetMe ...
- 总是有人问我,那你能造出你自己都搬不动的石头吗? 我说不能,但我能写出个我自己都无法 fix 的 bug。
总是有人问我,那你能造出你自己都搬不动的石头吗? 我说不能,但我能写出个我自己都无法 fix 的 bug.
- 蓝桥杯历届试题-垒色子(DP+矩阵快速幂)
一.题目 垒骰子 赌圣atm晚年迷恋上了垒骰子,就是把骰子一个垒在另一个上边,不能歪歪扭扭,要垒成方柱体.经过长期观察,atm 发现了稳定骰子的奥秘:有些数字的面贴着会互相排斥!我们先来规范一下骰子: ...
- Backing up the tail
The tail of the transaction log usually refers to the contents of the database's transaction log tha ...
- 关于phpmailer邮件发送
今天有个需求,要把phpmailer集成到框架里面 所以我去官方下载了 phpmail5.2.6 地址在 https://github.com/PHPMailer/PHPMailer/releases ...
- VS2010开发MFC ActiveX,摄像头拍照上传Webservice(2)
继续记录,第二步开发摄像头拍照功能. 使用vfw.h开发摄像头拍照功能,关于vfw网上有很多文章,很多代码可以参考 参考:http://blog.163.com/huangqiao_8/blog/st ...
- .NET 同步 异步 委托
1.定义委托: using System; using System.Collections.Generic; using System.IO; using System.Linq; using Sy ...
- c#,读取二维码
/// <summary>/// 读取二维码/// 读取失败,返回空字符串/// </summary>/// <param name="filename&quo ...