简体   繁体   English

保持Sqlite3数据库忙

[英]Keeping Sqlite3 Database Busy

I am using Sqlite3 Database in my project. 我在我的项目中使用Sqlite3数据库。 For testing purpose, I need to keep database busy for a long time . 出于测试目的,我需要让数据库保持很长时间。 How to achieve that ? 怎么实现呢? Is there a command or script I can write a keep database busy. 是否有命令或脚本我可以写一个保持数据库忙。

Can somebody please help ? 有人可以帮忙吗?

The database is busy when there is some active transaction. 当存在一些活动事务时,数据库正忙。

To force some transaction, just execute BEGIN EXCLUSIVE . 要强制执行某些事务,只需执行BEGIN EXCLUSIVE即可 This could be done from your program, or simply by hand from the sqlite3 command-line shell: 这可以从您的程序完成,或者只需从sqlite3命令行shell手动完成:

$ sqlite3 /some/where/mydatabase.db
SQLite version 3.8.5 2014-06-04 14:06:34
Enter ".help" for usage hints.
sqlite> BEGIN EXCLUSIVE;
sqlite> -- the DB is now locked ...
sqlite> COMMIT;

Given that this is for testing purposes , one way is to have a thread do constant inserts into the database. 鉴于这是出于测试目的 ,一种方法是让线程不断插入数据库。 Then you would use another thread to do the reading. 然后你将使用另一个线程来进行阅读。 I can see it being done in the following way (using pseudocode) in junit: 我可以看到它在junit中以下面的方式(使用伪代码)完成:

@Before
public void setup(){
    //start thread for inserting
}

@After
public void destroy(){
    //stop thread for inserting
}

@Test
public void testRead(){
    //do the test for reading
}

The above would keep the database busy for the duration of each test. 以上将使数据库在每次测试期间保持忙碌。 You could have a seperate application that inserts for a defined time limit. 您可以使用单独的应用程序插入定义的时间限制。 This would be done in this way: 这将以这种方式完成:

public static void main(String [] args){
//mark startTime
//while currentTime - startTime < timeLimit (in milliseconds)
//insert into db
}

The part of the question I do not understand is: 我不明白的问题部分是:

  • Why would you want to do this? 你为什么想做这个? - Are you testing transactionality or are you testing database connections? - 您是在测试事务性还是在测试数据库连接?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM