简体   繁体   English

获取sqlite繁忙异常[SQLITE_BUSY]数据库文件被锁定(数据库被锁定)

[英]Getting sqlite busy exception [SQLITE_BUSY] The database file is locked (database is locked)

EDIT :: closing resultset and statements for queries solved the problem for me编辑::关闭结果集和查询语句为我解决了这个问题

I am trying to build a chat app by java and sqlite.我正在尝试通过 java 和 sqlite 构建一个聊天应用程序。 when i try to insert a new record to a table in data base i am facing data base locked error message.当我尝试向数据库中的表插入新记录时,我正面临数据库锁定错误消息。 here i try to add new user when they sign up first time:在这里,我尝试在他们第一次注册时添加新用户:

public void addNewUser(String username,String password)  {
    StringBuilder sb=new StringBuilder("INSERT INTO loginInfo VALUES('");
    sb.append(username);
    sb.append("','");
    sb.append(password);
    sb.append("')");
    try {
        Statement statement = connection.createStatement();

        statement.execute(sb.toString());
        statement.close();
    } catch (SQLException throwables) {
        throwables.printStackTrace();
    }
}

i am first checking if already same username existed in doesUserExist() function:我首先检查 dosUserExist() 函数中是否已经存在相同的用户名:

private void handleSignup(String[] tokens) throws IOException, SQLException {
    if(tokens.length==3){
        String username=tokens[1];
        String password=tokens[2];
        String msg=null;
        if(!dataSource.doesUserExists(username)) {
            msg="signup successful\n";
            this.login=username;
            dataSource.addNewUser(username,password);
        }
        else msg="signup unsuccessful\n";
        this.send(msg);
    }
}

i get this following error message:我收到以下错误消息:

org.sqlite.SQLiteException: [SQLITE_BUSY]  The database file is locked (database is locked)
at org.sqlite.core.DB.newSQLException(DB.java:1010)
at org.sqlite.core.DB.newSQLException(DB.java:1022)
at org.sqlite.core.DB.execute(DB.java:861)
at org.sqlite.core.CoreStatement.exec(CoreStatement.java:80)
at org.sqlite.jdbc3.JDBC3Statement.execute(JDBC3Statement.java:53)

i have closed the statement after execution of all other queries.我在执行所有其他查询后关闭了该语句。 what can i do to solve this bug?我能做些什么来解决这个错误?

You have some other process or thread that is writing in the database and is holding RESERVED lock, or you have open and not closed TRANSACTION .您有一些其他进程或线程正在写入数据库并持有RESERVED锁,或者您已打开但未关闭TRANSACTION

http://www.sqlite.org/draft/matrix/lockingv3.html http://www.sqlite.org/draft/matrix/lockingv3.html

Writing to a database file写入数据库文件

Only one process at a time can hold a RESERVED lock.一次只有一个进程可以持有一个 RESERVED 锁。 But other processes can continue to read the database while the RESERVED lock is held.但是其他进程可以在持有 RESERVED 锁时继续读取数据库。

If the process that wants to write is unable to obtain a RESERVED lock, it must mean that another process already has a RESERVED lock.如果要写入的进程无法获得 RESERVED 锁,则必定意味着另一个进程已经拥有 RESERVED 锁。 In that case, the write attempt fails and returns SQLITE_BUSY.在这种情况下,写入尝试失败并返回 SQLITE_BUSY。

https://sqlite.org/lang_transaction.html https://sqlite.org/lang_transaction.html

Any command that accesses the database (basically, any SQL command, except a few PRAGMA statements) will automatically start a transaction if one is not already in effect.任何访问数据库的命令(基本上,任何 SQL 命令,除了少数 PRAGMA 语句)都将自动启动一个尚未生效的事务。 Automatically started transactions are committed when the last SQL statement finishes.当最后一条 SQL 语句完成时,将提交自动启动的事务。

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

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