Getting [SQLITE_BUSY] database file is locked with select statements

sqlite_busy: database is locked nodejs
sqlite database is locked
how to unlock sqlite database in java
database is locked (code 5 sqlite_busy)
sqlite db is locked vtune
sqlite nfs database is locked
sqlite.sqliteexception database is locked xamarin
duplicati the database file is locked database is locked

If I run multiple threads against my web app I get:

java.sql.SQLException: [SQLITE_BUSY]  The database file is locked (database is locked)
    at org.sqlite.DB.newSQLException(DB.java:383)
    at org.sqlite.DB.newSQLException(DB.java:387)
    at org.sqlite.DB.execute(DB.java:339)
    at org.sqlite.PrepStmt.executeQuery(PrepStmt.java:75)
    at org.apache.commons.dbcp.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:96)

I do know that only one thread can write to a sqlite database but I'm only reading from the database. So why do I get this error message ?

BTW: My connection pool looks like this:

<bean class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close" id="dataSource">
    <property name="driverClassName" value="${database.driverClassName}" />
    <property name="url" value="${database.url}" />
    <property name="username" value="${database.username}" />
    <property name="password" value="${database.password}" />
    <property name="initialSize" value="1" />
    <property name="maxActive" value="2" />
    <property name="maxIdle" value="1" />
    <property name="poolPreparedStatements" value="true" />
</bean>

The setup is: Java 1.6, Tomcat 7.0.34, Spring 3.2, Hibernate 3.6.9 and sqlite3 3.7.2

Regards Roger

After some googling I found that it is a bad practice to use multiple connections when connecting to SQLite. See

http://touchlabblog.tumblr.com/post/24474398246/android-sqlite-locking

Set your poolsize maxactive to 1 and try out.

How to Fix SQLite Error Database is Locked, Simple steps to fix SQLite Error Database is Locked including Error Code 5 the SELECT statement is in a pending state, the database will get locked. that SQLITE_LOCKED should not be confused with a SQLITE_BUSY parameter. successfully, and then you can delete the old SQLite database file. When you attempt to do 2 SELECT on the same table and at the same time in a multi-thread application and the SQLite has not been fixed to do the same, the database may get locked. One thing you should keep in mind that SQLITE_LOCKED should not be confused with a SQLITE_BUSY parameter.

There should be only ONE connection with your application. you can use this to ensure.

public class SqliteHelper {
private static Connection c = null;
public static Connection getConn() throws Exception {
    if(c == null){
    Class.forName("org.sqlite.JDBC");
    c = DriverManager.getConnection("jdbc:sqlite:D:/test.db");
    }
    return c;
    }
}

SQLite, [SEVERE] Database query error: [SQLITE_BUSY] The database file is locked this ResultSet in the calling method, otherwise you will get memory leaks. ResultSet res1 = base.query("SELECT id,PTimed,TimeEnd FROM  Begin immediate transaction -> SQLITE_BUSY (database is locked). Hello, I have a hard time finding the cause of a bug in my application. I believe that it's me doing something wrong and not

Note also that this may happen if you accidentally forget to close your connection:

Connection connection;
try {
  Statement statement = connection.createStatement();
  ResultSet resultSet = statement.executeQuery(QUERY);
  if (resultSet.next()) { /* do something */ }
catch (SQLException e) { /* handle exception */ }
finally {
  if (connection != null) {
    try {
      connection.close(); // <-- This is important
    } catch (SQLException e) {
      /* handle exception */
    }
  }
}

While the first database connection may work well once the server is started, subsequent queries may not, depending on how the connection pool is configured.

Getting java.sql.SQLException "SQLITE_BUSY" while writing result , If I run multiple threads against my web app I get: java.sql.SQLException: [​SQLITE_BUSY] The database file is locked (database is locked) at  blablubbabc you did not suggest leaving a connection open after executing your queries didn't you? Of course you can share the same connection for multiple queries (i hope that is what you ment xD) but don't leave a connection open when you are done with whatever you wanted to do as this might lead to file corruption when the connection is still open and the server crashes.

Everytime you establish a connection make sure to close it after the work is done, It worked for me like if you are using

Connection con = null;
PreparedStatement pst = con.prepareStatement("...query... "); 
/*
 do some stuff 
*/
pst.executeQuery();
pst.close();
con.close();

[SQLITE_BUSY] The database file is locked , Hi Anshoo, I am getting below error,could you please advise,i have also noticed java.sql.SQLException: [SQLITE_BUSY] The database file is locked *Trying to write to a table while a SELECT is active on that same table. Nevertheless, this document continues to serve as an authoritative reference to how database file locking works in SQLite version 3. The document only describes locking for the older rollback-mode transaction mechanism. Locking for the newer write-ahead log or WAL mode is described separately. 1.0 File Locking And Concurrency In SQLite Version 3

For me the problem was that I was opening too much Sessions So I made the session field in my DAO class static

[Solved] sqlite database lock problem, [SQLITE_BUSY] The database file is locked (database is locked). Problem; Updated 3 GenericJDBCException: could not execute statement As others have said, one possible reason for this is that another process currently has it open. Another possibility is a stale NFS lock, if the database is on an NFS volume. In that case, a workaround is to replace the database file with a fresh copy that isn't locked on the NFS server (mv database.db original.db; cp original.db database.db).

The top methods to resolve SQLite Error Code 5 “Database is Locked”, The reasons for getting the database lock error are; SQLITE_LOCKED error is distinct from SQLITE_BUSY . SQLITE_BUSY means that another database connection (probably *Trying to CREATE or DROP a table or index while a SELECT statement is still pending. *fcntl(3, F_SETLK call on DB file fails. Getting [SQLITE_BUSY] database file is locked with select statements But I still do not understand why the file is locked although only select statements

SQLite Error Database is Locked- How to resolve?, The database gets locked in a situation when you try to DROP or CREATE an index or table while the SELECT statement is still in a pending state. The main SQLite is meant to be an SQLite database file. $Sqlite3 .db.Sqlite. SQLITE_BUSY means that some connection is BUSY with a write transaction and has locked the database file; presumably, it will be possible to write to the database

How do I prevent SQLite database locks?, SQLite error database is locked is one of the frequent errors in SQLite database. You'll get to know how to resolve this error using both manual and alternate an index or table while SELECT statement is still in a pending state. You need to replace your old database file with the new backup file copy 

Comments
  • Possible duplicate of SQLITE_BUSY The database file is locked (database is locked) in wicket
  • Yes that works. But I still do not understand why the file is locked although only select statements are executed.
  • @rogergl: Try getting a connection with SQLiteOpenHelper#getReadableDatabase(). Link: developer.android.com/reference/android/database/sqlite/…
  • This is also recommended within SQLite documentation as one of several steps to reduce the occurrence of this situation. The first step would be to ensure everything is closed. Also should check that the database supports threading (default is on) FULLMUTEX