delphi使用unidac批量insert into插入sqlite数据库记录出现database is locked错误!

就是写了一个for循环插入数据:
for j := 0 to Num - 1 do  
      begin
         ..........
         UniQuery1.SQL.Add('insert into .......
         UniQuery1.ExecSQL;
         .........
      end;  
错误现象:循环插入10条以内的数据,都不会出错,循环插入48条以上数据就出现:database is locked错误!

CSDN百度了一下:C# 的,还是一头雾水,根据3楼的提示,或许是我对这个表在其他窗体已经Active Open了,导致出错?
SQLite 是一个软件库,实现了自给自足的、无服务器的、零配置的、事务性的 SQL 数据库引擎。SQLite允许多个进程/线程同时进行读操作,但在同一时刻只允许一个线程进行写操作。SQLite在进行写操作时,数据库文件会被锁定,此时任何其他的读/写操作都会被阻塞,如果阻塞超过5秒钟(默认是5秒,可通过重新编译SQLite进行修改),就会抛出描述为“database is locked”的异常。

出现上述现象的原因是SQLite只支持库级锁,不支持并发执行写操作,即使是不同的表,同一时刻也只能进行一个写操作。例如,事务T1在表A新插入一条数据,事务T2在表B中更新一条已存在的数据,这两个操作是不能同时进行的,只能顺序进行。

SQLite尽量延迟了申请X锁,直到数据块真正写盘时才申请X锁,再加上被阻塞的操作有等待时间,所以当SQLite作为客户端嵌入数据库被使用时时,一般情况下不会抛出“database is locked”的异常。但是,在高并发的环境下,还是很有可能抛出异常的。避免这种异常的最简单有效的方法,就是在进行写操作时实现互斥锁,并保证写操作按顺序执行。

这边先说一下database is locked产生的原因:sqlite同一时间只能进行一个写操作,当同时有两个写操作的时候,后执行的只能先等待,如果等待时间超过5秒,就会产生这种错误.同样一个文件正在写入,重复打开数据库操作更容易导致这种问题的发生。

那首先,得避免重复打开数据库,首先引入单例方法与SQLiteOpenHelper类:

--------------------------------------------------------------------------------------------------------------------------

详细看了一下uniDAC文档,找到一个属性:BusyTimeout 可以调用:sqlite3_busy_timeout
代码如下:
 UniConnection1.SpecificOptions.Values['BusyTimeout'] := '10000';
结论:经测试问题解决;

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Product Description Universal Data Access Components (UniDAC) is a library of components that provides direct access to most popular database servers from Delphi, Delphi for .NET and, C++Builder. UniDAC can work with such servers as Oracle, Microsoft SQL Server, MySQL, Interbase, Firebird. The UniDAC library is designed to help programmers develop faster and cleaner cross-database applications. UniDAC is a complete replacement for such standard universal technology as dbExpress. UniDAC is a handy in use and efficient data access solution bringing into play many features specific to each server. Requirements UniDAC is a product joining functionality of such data access solutions as ODAC, SDAC, MyDAC, and IBDAC. Requirements of UniDAC depend only on used providers. Detailed requirements for each provider can be found in the Requirements topic in the UniDAC Help documentation. Installation Compiled versions of UniDAC are installed automatically by the UniDAC Installer for all supported IDEs. Versions of UniDAC with Source Code must be installed manually. For more information, see the Installation topic of the UniDAC Help documentation. It is important to know that UniDAC, ODAC, SDAC, MyDAC, and IBDAC use the common base packages, so product compatibility is provided for the current build only. In the other words, if you upgrade one of the installed providers, it may conflict with older builds of other products. In order to continue using the products simultaneously, you should upgrade all of them at the same time. 7.1.4 29-Sep-17 InterBase data provider Bug with the "Input parameter mismatch" error for procedure is fixed Bug with transactions when working with Firebird 2.5 is fixed 7.1.3 19-Sep-17 The performance of TVirtualQuery is significantly improved Application-defined functions in TVirtualQuery are supported Application-defined collations in TVirtualQuery are supported AutoInc fields in TVirtualTable are supported Bug with assertion failure when updating a record in TVirtualQuery is fixed Bug with compatibility with TkbmMemTable in TVirtualQuery is fixed Bug with complex WHERE clauses that have several string fields in TVirtualQuery is fixed Bug with selecting from a dataset with a single record in TVirtualQuery is fixed Bug with generating SQL for batch updates is fixed Bug with the Locate method for non-Windows platforms in Lazarus is fixed Bug with the FindFirst, FindLast, FindNext, FindPrior methods in Lazarus is fixed Bug with accessing a product help from the IDE menu is fixed Bug with recreating fields when calling Open after Prepare is fixed Bug with an incorrect error message when using the Connect dialog component on mobile platforms is fixed Bug with AV failure when using calculated fields in TClientDataset is fixed Bug with a key violation error when executing batch operations is fixed Bug with Assert when calling the Locate method in the SmartFetch mode is fixed Bug with using DAC components in DataModule for Linux is fixed Cloud data providers BigCommerce provider is added Dynamics CRM provider is added FreshBooks provider is added Magento provider is added MailChimp provider is added NetSuite provider is added QuickBooks provider is added Salesforce provider is added Salesforce Marketing Cloud provider is added SugarCRM provider is added Zoho CRM provider is added Oracle data provider Oracle 12c connection modes (SYSBACKUP, SYSDG, SYSKM) in the Direct mode are supported OS authentication in the Direct mode is supported NChar literal replacement is supported CLOB parameters behavior when UnicodeEnvironment=True is improved Bug with lost chars in a parameter value when ConvertEOL=True is fixed SQLServer data provider Bug with processing varchar fields when AutoTranslate is True in the Direct mode is fixed Bug with processing a field with alias for query with the JOIN statement is fixed Bug with date and time fractional seconds precision when DescribeParams = True is fixed MySQL data provider Azure Database for MySQL is supported JSON data type is supported InterBase data provider Support for Firebird on Android platform is added Support for Firebird 3 packages is added Aliases handling in the RETURNING clause is supported The WireCompression connection parameter for Firebird 3 is supported Bug with using BLOB data type in batch operations is fixed Bug with the OnBackupProgress event handler in TUniDADump is fixed Bug with using batch parameters of the Bytes type is fixed Bug with closing a connection that has an active transaction for Firebird 3 is fixed Bug with recreating a connection on Linux and mobile platforms is fixed PostgreSQL data provider SSPI authentication is supported Processing GUID data type for the TGuidField class is improved SQLite data provider Now the Direct mode is based on the SQLite engine version 3.20.0 Custom SQL aggregate functions are supported Bug with opening an existing database on iOS 64 is fixed Bug with detecting field types is fixed Bug with international symbols in non-Unicode Delphi versions is fixed Bug with low performance of the LoadFromDataSet method of the Loader component is fixed Bug with index backup using the Dump component when the TableNames property is set is fixed DBF data provider The CodePage specific options are added The ConnectMode specific options are added Bug with using CDX indexes is fixed DB2 data provider The DECFLOAT data type is supported 7.0.2 15-May-17 Bug with using TVirtualQuery with TVirtualTable is fixed Bug with processing NULL values in the Loader component is fixed Bug with executing a query after executing a batch command is fixed Bug with executing a batch operation inside an explicitly started transaction is fixed Bug with using the AsGuid property is fixed Oracle data provider Bug with authentication in the Direct mode is fixed MySQL data provider Bug with processing DATETIME fields is fixed SQLServer data provider Bug with the "Invalid column name" error when editing a query with the JOIN statement is fixed InterBase data provider Bug with memory leak when using the SQLMonitor component is fixed PostgreSQL data provider Bug with AV failure after executing the Ping method is fixed SQLite data provider Bug with creating a database in the Direct Mode on Android is fixed

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值